Module: ActiveRecordPostgresEarthdistance::ActsAsGeolocated::ClassMethods
- Defined in:
- lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb
Instance Method Summary collapse
- #acts_as_geolocated(options = {}) ⇒ Object
- #order_by_distance(lat, lng, order = "ASC") ⇒ Object
- #within_box(radius, lat, lng) ⇒ Object
- #within_radius(radius, lat, lng) ⇒ Object
Instance Method Details
#acts_as_geolocated(options = {}) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 6 def acts_as_geolocated( = {}) cattr_accessor :latitude_column, :longitude_column, :through_table self.latitude_column = [:lat] || (column_names.include?("lat") ? "lat" : "latitude") self.longitude_column = [:lng] || (column_names.include?("lng") ? "lng" : "longitude") self.through_table = [:through] end |
#order_by_distance(lat, lng, order = "ASC") ⇒ Object
35 36 37 38 |
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 35 def order_by_distance(lat, lng, order = "ASC") earth_distance = Utils.earth_distance(through_table_klass, lat, lng) joins(through_table).order("#{earth_distance.to_sql} #{order}") end |
#within_box(radius, lat, lng) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 14 def within_box(radius, lat, lng) 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) ⇒ Object
29 30 31 32 33 |
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 29 def within_radius(radius, lat, lng) 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 |