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
- #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
-
#geokit_finder_adapter ⇒ Object
A proxy to an instance of a finder adapter, inferred from the connection’s adapter.
- #in_bounds(bounds, options = {}) ⇒ Object
- #in_range(range, options = {}) ⇒ Object
- #with_latlng ⇒ Object
- #within(distance, options = {}) ⇒ Object (also: #inside)
Instance Method Details
#beyond(distance, options = {}) ⇒ Object Also known as: outside
130 131 132 133 134 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 130 def beyond(distance, = {}) [:beyond] = distance #geo_scope(options) where(distance_conditions()) end |
#by_distance(options = {}) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 152 def by_distance( = {}) origin = () units = () formula = () distance_column_name = distance_sql(origin, units, formula) with_latlng.order( Arel.sql(distance_column_name).send([:reverse] ? 'desc' : 'asc') ) end |
#closest(options = {}) ⇒ Object Also known as: nearest
166 167 168 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 166 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.
213 214 215 216 217 218 219 220 221 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 213 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
171 172 173 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 171 def farthest( = {}) by_distance({:reverse => true}.merge()).limit(1) end |
#geokit_finder_adapter ⇒ Object
A proxy to an instance of a finder adapter, inferred from the connection’s adapter.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 97 def geokit_finder_adapter @geokit_finder_adapter ||= begin unless Adapters.const_defined?(connection.adapter_name.camelcase) filename = connection.adapter_name.downcase require File.join("geokit-rails", "adapters", filename) end klass = Adapters.const_get(connection.adapter_name.camelcase) if klass.class == Module # For some reason Mysql2 adapter was defined in Adapters.constants but was Module instead of a Class filename = connection.adapter_name.downcase require File.join("geokit-rails", "adapters", filename) # Re-init the klass after require klass = Adapters.const_get(connection.adapter_name.camelcase) end klass.load(self) unless klass.loaded || skip_loading klass.new(self) rescue LoadError raise UnsupportedAdapter, "`#{connection.adapter_name.downcase}` is not a supported adapter." end end |
#in_bounds(bounds, options = {}) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 143 def in_bounds(bounds, = {}) inclusive = .delete(:inclusive) || false [:bounds] = bounds #geo_scope(options) #where(distance_conditions(options)) bounds = () where(bound_conditions(bounds, inclusive)) end |
#in_range(range, options = {}) ⇒ Object
137 138 139 140 141 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 137 def in_range(range, = {}) [:range] = range #geo_scope(options) where(distance_conditions()) end |
#with_latlng ⇒ Object
162 163 164 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 162 def with_latlng where("#{qualified_lat_column_name} IS NOT NULL AND #{qualified_lng_column_name} IS NOT NULL") end |
#within(distance, options = {}) ⇒ Object Also known as: inside
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/geokit-rails/acts_as_mappable.rb', line 118 def within(distance, = {}) [:within] = distance # Add bounding box to speed up SQL request. bounds = formulate_bounds_from_distance( , normalize_point_to_lat_lng([:origin]), [:units] || default_units) with_latlng.where(bound_conditions(bounds)). where(distance_conditions()) end |