From 1406f37d48e4d02be082a4fb62c2b02180ca1ca9 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 19 Sep 2014 21:16:35 +0900 Subject: [PATCH] Add Event#location= and make location an accessible attribute. Add long aliases for lat and lng to Location. --- app/models/agents/user_location_agent.rb | 6 ++++-- app/models/event.rb | 15 ++++++++++++++- lib/location.rb | 6 ++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/models/agents/user_location_agent.rb b/app/models/agents/user_location_agent.rb index 2575e800..e3164131 100644 --- a/app/models/agents/user_location_agent.rb +++ b/app/models/agents/user_location_agent.rb @@ -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 diff --git a/app/models/event.rb b/app/models/event.rb index 178252f3..a0a83c3f 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/lib/location.rb b/lib/location.rb index 74a4de51..6f7a6634 100644 --- a/lib/location.rb +++ b/lib/location.rb @@ -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