module ActiveRecordPostgresEarthdistance::ActsAsGeolocated::ClassMethods
Public Instance Methods
acts_as_geolocated(options = {})
click to toggle source
# File lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb, line 8 def acts_as_geolocated(options = {}) begin if table_exists? cattr_accessor :latitude_column, :longitude_column, :through_table, :distance_unit self.latitude_column = options[:lat] || (column_names.include?("lat") ? "lat" : "latitude") self.longitude_column = options[:lng] || (column_names.include?("lng") ? "lng" : "longitude") self.through_table = options[:through] self.distance_unit = options[:distance_unit] else puts "[WARNING] table #{table_name} doesn't exist, acts_as_geolocated won't work. Skip this warning if you are running db migration" end rescue ActiveRecord::NoDatabaseError rescue PG::ConnectionBad end end
order_by_distance(lat, lng, order = "ASC")
click to toggle source
# File lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb, line 48 def order_by_distance(lat, lng, order = "ASC") earth_distance = Utils.earth_distance(through_table_klass, lat, lng) joins(through_table).order(Arel.sql("#{earth_distance.to_sql} #{order}")) end
through_table_klass()
click to toggle source
# File lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb, line 53 def through_table_klass if through_table.present? reflections[through_table.to_s].klass else self end end
within_box(radius, lat, lng)
click to toggle source
# File lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb, line 25 def within_box(radius, lat, lng) radius = radius.try(:*, MILES_TO_METERS_FACTOR) if distance_unit === :miles earth_box = Arel::Nodes::NamedFunction.new( "earth_box", [Utils.ll_to_earth_coords(lat, lng), Utils.quote_value(radius)] ) joins(through_table) .where( Arel::Nodes::InfixOperation.new( "<@", Utils.ll_to_earth_columns(through_table_klass), earth_box ) ) end
within_radius(radius, lat, lng)
click to toggle source
# File lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb, line 41 def within_radius(radius, lat, lng) radius = radius.try(:*, MILES_TO_METERS_FACTOR) if distance_unit === :miles earth_distance = Utils.earth_distance(through_table_klass, lat, lng) within_box(radius, lat, lng) .where(Arel::Nodes::InfixOperation.new("<=", earth_distance, Utils.quote_value(radius))) end