vrfs-774: added testing for populating user lat/lng when creating a connection
This commit is contained in:
parent
f164a750c3
commit
d071ea9381
|
|
@ -21,6 +21,7 @@ module JamRuby
|
|||
validates :as_musician, :inclusion => {:in => [true, false]}
|
||||
validate :can_join_music_session, :if => :joining_session?
|
||||
after_save :require_at_least_one_track_when_in_session, :if => :joining_session?
|
||||
after_create :did_create
|
||||
|
||||
include AASM
|
||||
IDLE_STATE = :idle
|
||||
|
|
@ -112,6 +113,10 @@ module JamRuby
|
|||
return self.music_session.users.exists?(user)
|
||||
end
|
||||
|
||||
def did_create
|
||||
self.user.update_lat_lng(self.ip_address) if self.user && self.ip_address
|
||||
end
|
||||
|
||||
private
|
||||
def require_at_least_one_track_when_in_session
|
||||
if tracks.count == 0
|
||||
|
|
|
|||
|
|
@ -984,31 +984,31 @@ module JamRuby
|
|||
end
|
||||
|
||||
def update_lat_lng(ip_addy=nil)
|
||||
yn = false
|
||||
if provides_location? # ip_addy argument ignored in this case
|
||||
return false unless ip_addy.nil? # do nothing if attempting to set latlng from an ip address
|
||||
query = { :city => self.city }
|
||||
query[:region] = self.state unless self.state.blank?
|
||||
query[:country] = self.country unless self.country.blank?
|
||||
if geo = MaxMindGeo.where(query).limit(1).first
|
||||
if geo.lat && geo.lng && (self.lat != geo.lat || self.lng != geo.lng)
|
||||
self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
|
||||
yn = true
|
||||
return true
|
||||
end
|
||||
end
|
||||
elsif ip_addy
|
||||
if geo = MaxMindGeo.ip_lookup(ip_addy)
|
||||
if self.lat != geo.lat || self.lng != geo.lng
|
||||
self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
|
||||
yn = true
|
||||
return true
|
||||
end
|
||||
end
|
||||
else
|
||||
if self.lat || self.lng
|
||||
self.update_attributes({ :lat => nil, :lng => nil })
|
||||
yn = true
|
||||
return true
|
||||
end
|
||||
end
|
||||
yn
|
||||
false
|
||||
end
|
||||
|
||||
# devise compatibility
|
||||
|
|
|
|||
|
|
@ -33,4 +33,18 @@ describe Connection do
|
|||
connection.destroyed?.should be_true
|
||||
end
|
||||
|
||||
it 'updates user lat/lng' do
|
||||
uu = FactoryGirl.create(:user)
|
||||
uu.lat.should == nil
|
||||
msess = FactoryGirl.create(:music_session, :creator => uu)
|
||||
geocode = FactoryGirl.create(:geocoder)
|
||||
connection = FactoryGirl.create(:connection,
|
||||
:user => uu,
|
||||
:music_session => msess,
|
||||
:ip_address => "1.1.1.1",
|
||||
:client_id => "1")
|
||||
user.lat.should == geocode.lat
|
||||
user.lng.should == geocode.lng
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue