Module: ActiveRecordPostgresEarthdistance::ActsAsGeolocated::ClassMethods

Defined in:
lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb

Instance Method Summary collapse

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(options = {})
  if table_exists?
    cattr_accessor :latitude_column, :longitude_column
    self.latitude_column = options[:lat] || (column_names.include?("lat") ? "lat" : "latitude")
    self.longitude_column = options[:lng] || (column_names.include?("lng") ? "lng" : "longitude")
  end
end

#order_by_distance(lat, lng, order = "ASC") ⇒ Object



24
25
26
27
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 24

def order_by_distance(lat, lng, order = "ASC")
  earth_distance = Arel::Nodes::NamedFunction.new('earth_distance', [ll_to_earth_columns, ll_to_earth_coords(lat, lng)])
  order("#{earth_distance.to_sql} #{order.to_s}")
end

#within_box(radius, lat, lng) ⇒ Object



14
15
16
17
# 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', [ll_to_earth_coords(lat, lng), radius])
  where Arel::Nodes::InfixOperation.new('<@', ll_to_earth_columns, earth_box)
end

#within_radius(radius, lat, lng) ⇒ Object



19
20
21
22
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 19

def within_radius(radius, lat, lng)
  earth_distance = Arel::Nodes::NamedFunction.new('earth_distance', [ll_to_earth_columns, ll_to_earth_coords(lat, lng)])
  within_box(radius, lat, lng).where(Arel::Nodes::InfixOperation.new('<=', earth_distance, radius))
end