disable lat lng and distance in queries for users

This commit is contained in:
Scott Comer 2014-05-17 14:55:26 -05:00
parent e0df9c7721
commit 9eaa34b07f
8 changed files with 94 additions and 85 deletions

View File

@ -154,3 +154,4 @@ fix_connection_fields.sql
session_ratings.sql
scheduled_sessions.sql
add_last_jam_user_fields.sql
remove_lat_lng_user_fields.sql

View File

@ -0,0 +1,2 @@
alter table users drop column lat;
alter table users drop column lng;

View File

@ -134,7 +134,7 @@ module JamRuby
end
def did_create
self.user.update_lat_lng(self.ip_address) if self.user && self.ip_address
# self.user.update_lat_lng(self.ip_address) if self.user && self.ip_address
end
def report_add_participant

View File

@ -88,37 +88,38 @@ module JamRuby
end
end
end
User.find_each { |usr| usr.update_lat_lng }
# User.find_each { |usr| usr.update_lat_lng }
Band.find_each { |bnd| bnd.update_lat_lng }
end
def self.where_latlng(relation, params, current_user=nil)
if 0 < (distance = params[:distance].to_i)
latlng = []
if location_city = params[:city]
if geo = self.where(:city => params[:city]).limit(1).first
latlng = [geo.lat, geo.lng]
end
elsif current_user
if current_user.lat.nil? || current_user.lng.nil?
if params[:remote_ip] && (geo = self.ip_lookup(params[:remote_ip]))
geo.lat = nil if geo.lat = 0
geo.lng = nil if geo.lng = 0
latlng = [geo.lat, geo.lng] if geo.lat && geo.lng
end
else
latlng = [current_user.lat, current_user.lng]
end
elsif params[:remote_ip] && (geo = self.ip_lookup(params[:remote_ip]))
geo.lat = nil if geo.lat = 0
geo.lng = nil if geo.lng = 0
latlng = [geo.lat, geo.lng] if geo.lat && geo.lng
end
if latlng.present?
relation = relation.where(['lat IS NOT NULL AND lng IS NOT NULL'])
.within(distance, :origin => latlng)
end
end
# todo scott this needs to consider score instead
# if 0 < (distance = params[:distance].to_i)
# latlng = []
# if location_city = params[:city]
# if geo = self.where(:city => params[:city]).limit(1).first
# latlng = [geo.lat, geo.lng]
# end
# elsif current_user
# if current_user.lat.nil? || current_user.lng.nil?
# if params[:remote_ip] && (geo = self.ip_lookup(params[:remote_ip]))
# geo.lat = nil if geo.lat = 0
# geo.lng = nil if geo.lng = 0
# latlng = [geo.lat, geo.lng] if geo.lat && geo.lng
# end
# else
# latlng = [current_user.lat, current_user.lng]
# end
# elsif params[:remote_ip] && (geo = self.ip_lookup(params[:remote_ip]))
# geo.lat = nil if geo.lat = 0
# geo.lng = nil if geo.lng = 0
# latlng = [geo.lat, geo.lng] if geo.lat && geo.lng
# end
# if latlng.present?
# relation = relation.where(['lat IS NOT NULL AND lng IS NOT NULL'])
# .within(distance, :origin => latlng)
# end
# end
relation
end

View File

@ -274,9 +274,9 @@ module JamRuby
end
def self.new_musicians(usr, since_date=Time.now - 1.week, max_count=50, radius=M_MILES_DEFAULT)
# todo scott turn this into score .within(radius, :origin => [usr.lat, usr.lng])
rel = User.musicians
.where(['created_at >= ? AND users.id != ?', since_date, usr.id])
.within(radius, :origin => [usr.lat, usr.lng])
.order('created_at DESC')
.limit(max_count)
objs = rel.all.to_a
@ -295,7 +295,7 @@ module JamRuby
.where(['bgenres.genre_id = ? AND bands.id IS NOT NULL', genre])
end
rel = MaxMindGeo.where_latlng(rel, params, current_user)
# todo scott this needs some scoring filter rel = MaxMindGeo.where_latlng(rel, params, current_user)
sel_str = 'bands.*'
case ordering = self.order_param(params)

View File

@ -12,9 +12,9 @@ module JamRuby
include Geokit::ActsAsMappable::Glue unless defined?(acts_as_mappable)
acts_as_mappable
after_save :check_lat_lng
# after_save :check_lat_lng
attr_accessible :first_name, :last_name, :email, :city, :password, :password_confirmation, :state, :country, :birth_date, :subscribe_email, :terms_of_service, :original_fpfile, :cropped_fpfile, :cropped_large_fpfile, :cropped_s3_path, :cropped_large_s3_path, :photo_url, :large_photo_url, :crop_selection, :lat, :lng
attr_accessible :first_name, :last_name, :email, :city, :password, :password_confirmation, :state, :country, :birth_date, :subscribe_email, :terms_of_service, :original_fpfile, :cropped_fpfile, :cropped_large_fpfile, :cropped_s3_path, :cropped_large_s3_path, :photo_url, :large_photo_url, :crop_selection
# updating_password corresponds to a lost_password
attr_accessor :updating_password, :updating_email, :updated_email, :update_email_confirmation_url, :administratively_created, :current_password, :setting_password, :confirm_current_password, :updating_avatar, :updating_progression_field, :mods_json
@ -140,8 +140,8 @@ module JamRuby
scope :musicians, where(:musician => true)
scope :fans, where(:musician => false)
scope :geocoded_users, where(['lat IS NOT NULL AND lng IS NOT NULL'])
scope :musicians_geocoded, musicians.geocoded_users
# todo scott someone with locidispid - scope :geocoded_users, where(['lat IS NOT NULL AND lng IS NOT NULL'])
# todo scott geocoded_users that are musicians - scope :musicians_geocoded, musicians.geocoded_users
scope :email_opt_in, where(:subscribe_email => true)
def user_progression_fields
@ -1103,55 +1103,56 @@ module JamRuby
!self.city.blank? && (!self.state.blank? || !self.country.blank?)
end
def check_lat_lng
if (city_changed? || state_changed? || country_changed?) && !lat_changed? && !lng_changed?
update_lat_lng
end
end
# def check_lat_lng
# if (city_changed? || state_changed? || country_changed?) && !lat_changed? && !lng_changed?
# update_lat_lng
# end
# end
def update_lat_lng(ip_addy=nil)
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
geo.lat = nil if geo.lat = 0
geo.lng = nil if geo.lng = 0
if geo.lat && geo.lng && (self.lat != geo.lat || self.lng != geo.lng)
self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
return true
end
end
elsif ip_addy
if geo = MaxMindGeo.ip_lookup(ip_addy)
geo.lat = nil if geo.lat = 0
geo.lng = nil if geo.lng = 0
if self.lat != geo.lat || self.lng != geo.lng
self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
return true
end
end
else
if self.lat || self.lng
self.update_attributes({ :lat => nil, :lng => nil })
return true
end
end
false
end
# def update_lat_lng(ip_addy=nil)
# 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
# geo.lat = nil if geo.lat = 0
# geo.lng = nil if geo.lng = 0
# if geo.lat && geo.lng && (self.lat != geo.lat || self.lng != geo.lng)
# self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
# return true
# end
# end
# elsif ip_addy
# if geo = MaxMindGeo.ip_lookup(ip_addy)
# geo.lat = nil if geo.lat = 0
# geo.lng = nil if geo.lng = 0
# if self.lat != geo.lat || self.lng != geo.lng
# self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
# return true
# end
# end
# else
# if self.lat || self.lng
# self.update_attributes({ :lat => nil, :lng => nil })
# return true
# end
# end
# false
# end
def current_city(ip_addy=nil)
unless self.city
if self.lat && self.lng
# todo this is really dumb, you can't compare lat lng for equality
return MaxMindGeo.where(['lat = ? AND lng = ?',self.lat,self.lng]).limit(1).first.try(:city)
elsif ip_addy
return MaxMindGeo.ip_lookup(ip_addy).try(:city)
end
else
return self.city
end
# unless self.city
# if self.lat && self.lng
# # todo this is really dumb, you can't compare lat lng for equality
# return MaxMindGeo.where(['lat = ? AND lng = ?',self.lat,self.lng]).limit(1).first.try(:city)
# elsif ip_addy
# return MaxMindGeo.ip_lookup(ip_addy).try(:city)
# end
# else
# return self.city
# end
self.city
end
def update_addr_loc(connection, reason)
@ -1171,11 +1172,12 @@ module JamRuby
def self.deliver_new_musician_notifications(since_date=nil)
since_date ||= Time.now-1.week
self.geocoded_users.find_each do |usr|
Search.new_musicians(usr, since_date) do |new_nearby|
UserMailer.new_musicians(usr, new_nearby).deliver
end
end
# todo scott return musicians with locidispid not null
# self.geocoded_users.find_each do |usr|
# Search.new_musicians(usr, since_date) do |new_nearby|
# UserMailer.new_musicians(usr, new_nearby).deliver
# end
# end
end
def facebook_invite!

View File

@ -276,6 +276,7 @@ describe 'Musician search' do
context 'new users' do
it "find nearby" do
pending 'todo scott fix this test so it does something'
# create new user outside 500 from Apex to ensure its excluded from results
FactoryGirl.create(:user, {city: "Austin", state: "TX", country: "US"})
User.geocoded_users.find_each do |usr|
@ -288,6 +289,7 @@ describe 'Musician search' do
end
it "sends new musician email" do
pending 'todo scott fix this test so it does something'
# create new user outside 500 from Apex to ensure its excluded from results
FactoryGirl.create(:user, {city: "Austin", state: "TX", country: "US"})
User.geocoded_users.find_each do |usr|

View File

@ -35,6 +35,7 @@ describe "Musician Search API", :type => :api do
context 'location filtering' do
it "gets no musicians for out of range locations" do
pending 'todo scott this needs to be rewritten using scores'
get_query({:city => 'San Francisco', :distance => 100})
good_response
expect((json['musicians'] || []).count).to be 0