Module: Geokit::ActsAsMappable::ClassMethods
- Defined in:
- lib/geokit-rails/acts_as_mappable/class_methods.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
41 42 43 44 45 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 41 def beyond(distance, = {}) [:beyond] = distance #geo_scope(options) where(distance_conditions()) end |
#by_distance(options = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 63 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
77 78 79 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 77 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.
124 125 126 127 128 129 130 131 132 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 124 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
82 83 84 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 82 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.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 8 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
54 55 56 57 58 59 60 61 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 54 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
48 49 50 51 52 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 48 def in_range(range, = {}) [:range] = range #geo_scope(options) where(distance_conditions()) end |
#with_latlng ⇒ Object
73 74 75 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 73 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
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/geokit-rails/acts_as_mappable/class_methods.rb', line 29 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 |