Module: ActiveRecordPostgresEarthdistance::ActsAsGeolocated::ClassMethods

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

Constant Summary collapse

MILES_TO_METERS_FACTOR =
1609.344

Instance Method Summary collapse

Instance Method Details

#acts_as_geolocated(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 7

def acts_as_geolocated(options = {})
  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
end

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



44
45
46
47
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 44

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

#through_table_klassObject



49
50
51
52
53
54
55
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 49

def through_table_klass
  if through_table.present?
    reflections[through_table.to_s].klass
  else
    self
  end
end

#within_box(radius, lat, lng) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 21

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) ⇒ Object



37
38
39
40
41
42
# File 'lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb', line 37

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