Sha256: 9697074b491740a1a9e4656b3b0383848997b4ba8fb0c84b957c61db78d66151
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
module ActiveRecordPostgresEarthdistance module ActsAsGeolocated extend ActiveSupport::Concern included do end module ClassMethods def acts_as_geolocated(options = {}) 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 def within_radius radius, lat, lng where(["ll_to_earth(#{self.latitude_column}, #{self.longitude_column}) <@ earth_box(ll_to_earth(?, ?), ?)" + "AND earth_distance(ll_to_earth(#{self.latitude_column}, #{self.longitude_column}), ll_to_earth(?, ?)) <= ?", lat, lng, radius, lat, lng, radius]) end def order_by_distance lat, lng, order: "ASC" order("earth_distance(ll_to_earth(#{self.latitude_column}, #{self.longitude_column}), ll_to_earth(#{lat}, #{lng})) #{order}") end end end end ActiveRecord::Base.send :include, ActiveRecordPostgresEarthdistance::ActsAsGeolocated
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activerecord-postgres-earthdistance-0.3.0 | lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb |