Add Event#location= and make location an accessible attribute.

Add long aliases for lat and lng to Location.
This commit is contained in:
Akinori MUSHA 2014-09-19 21:16:35 +09:00
parent edcd80f228
commit 1406f37d48
3 changed files with 24 additions and 3 deletions

View file

@ -65,8 +65,10 @@ module Agents
private
def handle_payload(payload)
if payload[:latitude].present? && payload[:longitude].present?
create_event payload: payload, lat: payload[:latitude].to_f, lng: payload[:longitude].to_f
location = Location.new(payload)
if location.present?
create_event payload: payload, location: location
end
end
end

View file

@ -7,7 +7,7 @@ class Event < ActiveRecord::Base
include JSONSerializedField
include LiquidDroppable
attr_accessible :lat, :lng, :payload, :user_id, :user, :expires_at
attr_accessible :lat, :lng, :location, :payload, :user_id, :user, :expires_at
acts_as_mappable
@ -53,6 +53,19 @@ class Event < ActiveRecord::Base
speed: payload[:speed].presence)
end
def location=(location)
case location
when nil
self.lat = self.lng = nil
return
when Location
else
location = Location.new(location)
end
self.lat, self.lng = location.lat, location.lng
location
end
# Emit this event again, as a new Event.
def reemit!
agent.create_event :payload => payload, :lat => lat, :lng => lng

View file

@ -34,6 +34,9 @@ class Location
}
end
alias latitude lat
alias latitude= lat=
def lng=(value)
self[:lng] = floatify(value) { |f|
if f.abs <= 180
@ -44,6 +47,9 @@ class Location
}
end
alias longitude lng
alias longitude= lng=
def radius=(value)
self[:radius] = floatify(value) { |f| f if f >= 0 }
end