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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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") ⇒ Object



48
49
50
51
# 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_klassObject



53
54
55
56
57
58
59
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
# 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