2011-09-15 21:41:26 +00:00
|
|
|
class Thing < ActiveRecord::Base
|
2011-06-25 09:38:00 +00:00
|
|
|
include Geokit::Geocoders
|
2012-01-27 21:11:44 +00:00
|
|
|
validates_uniqueness_of :city_id, :allow_nil => true
|
2011-05-22 14:27:36 +00:00
|
|
|
validates_presence_of :lat, :lng
|
2011-02-23 20:50:59 +00:00
|
|
|
belongs_to :user
|
2011-05-22 14:35:46 +00:00
|
|
|
has_many :reminders
|
2011-02-23 20:50:59 +00:00
|
|
|
|
2012-01-14 22:31:01 +00:00
|
|
|
def self.find_closest(lat, lng, limit=10)
|
2011-04-04 05:49:23 +00:00
|
|
|
query = <<-SQL
|
2012-03-31 12:47:25 +00:00
|
|
|
SELECT *, (3959 * ACOS(COS(RADIANS(?)) * COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS(?)) + SIN(RADIANS(?)) * SIN(RADIANS(lat)))) AS distance
|
2011-09-15 21:41:26 +00:00
|
|
|
FROM things
|
2011-04-04 05:49:23 +00:00
|
|
|
ORDER BY distance
|
|
|
|
LIMIT ?
|
|
|
|
SQL
|
2011-09-15 21:41:26 +00:00
|
|
|
find_by_sql([query, lat.to_f, lng.to_f, lat.to_f, limit.to_i])
|
2011-02-23 20:50:59 +00:00
|
|
|
end
|
2011-03-17 18:46:06 +00:00
|
|
|
|
2011-06-25 09:38:00 +00:00
|
|
|
def reverse_geocode
|
|
|
|
@reverse_geocode ||= MultiGeocoder.reverse_geocode([lat, lng])
|
|
|
|
end
|
|
|
|
|
|
|
|
def street_number
|
|
|
|
reverse_geocode.street_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def street_name
|
|
|
|
reverse_geocode.street_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def street_address
|
|
|
|
reverse_geocode.street_address
|
|
|
|
end
|
|
|
|
|
|
|
|
def city
|
|
|
|
reverse_geocode.city
|
|
|
|
end
|
|
|
|
|
|
|
|
def state
|
|
|
|
reverse_geocode.state
|
2011-03-17 18:46:06 +00:00
|
|
|
end
|
|
|
|
|
2011-06-25 09:38:00 +00:00
|
|
|
def zip
|
|
|
|
reverse_geocode.zip
|
|
|
|
end
|
|
|
|
|
|
|
|
def country_code
|
|
|
|
reverse_geocode.country_code
|
|
|
|
end
|
|
|
|
|
|
|
|
def country
|
|
|
|
reverse_geocode.country
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_address
|
|
|
|
reverse_geocode.full_address
|
|
|
|
end
|
|
|
|
|
|
|
|
def adopted?
|
|
|
|
!user_id.nil?
|
|
|
|
end
|
2011-02-23 20:50:59 +00:00
|
|
|
end
|