Module: Geokit::ActsAsMappable::ClassMethods
- Defined in:
- lib/geokit-rails/acts_as_mappable.rb
Overview
Class methods included in models when acts_as_mappable
is called
Instance Method Summary collapse
-
#adapter ⇒ Object
A proxy to an instance of a finder adapter, inferred from the connection’s adapter.
- #beyond(distance, options = {}) ⇒ Object (also: #outside)
- #by_distance(options = {}) ⇒ Object
- #closest(options = {}) ⇒ Object (also: #nearest)
-
#distance_sql(origin, units = default_units, formula = default_formula) ⇒ Object
Returns the distance calculation to be used as a display column or a condition.
- #farthest(options = {}) ⇒ Object
- #in_bounds(bounds, options = {}) ⇒ Object
- #in_range(range, options = {}) ⇒ Object
- #within(distance, options = {}) ⇒ Object (also: #inside)
Instance Method Details
#adapter ⇒ Object
A proxy to an instance of a finder adapter, inferred from the connection’s adapter.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 94 def adapter @adapter ||= begin require File.join('geokit-rails', 'adapters', connection.adapter_name.downcase) klass = Adapters.const_get(connection.adapter_name.camelcase) klass.load(self) unless klass.loaded klass.new(self) rescue LoadError raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter." end end |
#beyond(distance, options = {}) ⇒ Object Also known as: outside
112 113 114 115 116 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 112 def beyond(distance, = {}) [:beyond] = distance #geo_scope(options) where(distance_conditions()) end |
#by_distance(options = {}) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 133 def by_distance( = {}) origin = () units = () formula = () bounds = () distance_column_name = distance_sql(origin, units, formula) #geo_scope(options).order("#{distance_column_name} asc") order("#{distance_column_name} #{[:reverse] ? 'DESC' : 'ASC'}") end |
#closest(options = {}) ⇒ Object Also known as: nearest
143 144 145 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 143 def closest( = {}) by_distance().limit(1) end |
#distance_sql(origin, units = default_units, formula = default_formula) ⇒ Object
Returns the distance calculation to be used as a display column or a condition. This is provide for anyone wanting access to the raw SQL.
190 191 192 193 194 195 196 197 198 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 190 def distance_sql(origin, units=default_units, formula=default_formula) case formula when :sphere sql = sphere_distance_sql(origin, units) when :flat sql = flat_distance_sql(origin, units) end sql end |
#farthest(options = {}) ⇒ Object
148 149 150 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 148 def farthest( = {}) by_distance({:reverse => true}.merge()).limit(1) end |
#in_bounds(bounds, options = {}) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 125 def in_bounds(bounds, = {}) [:bounds] = bounds #geo_scope(options) #where(distance_conditions(options)) bounds = () where(bound_conditions(bounds)) end |
#in_range(range, options = {}) ⇒ Object
119 120 121 122 123 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 119 def in_range(range, = {}) [:range] = range #geo_scope(options) where(distance_conditions()) end |
#within(distance, options = {}) ⇒ Object Also known as: inside
105 106 107 108 109 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 105 def within(distance, = {}) [:within] = distance #geo_scope(options) where(distance_conditions()) end |