Module: HasDistance::Distance::Glue

Extended by:
ActiveSupport::Concern
Defined in:
lib/has_distance/distance.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

KMS_PER_MILE =

ClassMethods

1.609
NMS_PER_MILE =
0.868976242
EARTH_RADIUS_IN_MILES =
3963.19
EARTH_RADIUS_IN_KMS =
EARTH_RADIUS_IN_MILES * KMS_PER_MILE
EARTH_RADIUS_IN_NMS =
EARTH_RADIUS_IN_MILES * NMS_PER_MILE

Instance Method Summary collapse

Instance Method Details

#nearby(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/has_distance/distance.rb', line 47

def nearby(options={})
  _config = self.distance_config

  _local_latitude   = self.send(_config.lat_name)
  _local_longitude  = self.send(_config.lng_name)
  _distance_name    = _config.column_name

  _distance = options.delete(:distance)
  _distance = _distance ? _distance : _config.distance
  _limit = options.delete(:limit) || _config.limit

  _lat = _deg2rad(_local_latitude)
  _lng = _deg2rad(_local_longitude)

  _sql = _sphere_distance_sql(_lat, _lng, _units_sphere_multiplier(_config.units))
  _sql << " AS #{_config.column_name}"

  _klass = self.class
  _arreflector = _klass.select("*, #{_sql}").group('id').
    having("#{_distance_name} <= #{_distance}")

  if !_limit.nil?
    _arreflector.limit(_limit)
  else
    _arreflector
  end
end