Module: Geokit::ActsAsMappable::ClassMethods
- Defined in:
- lib/geokit-rails3/acts_as_mappable.rb,
lib/geokit-rails3/acts_as_mappable.old.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)
- #closest(options = {}) ⇒ Object (also: #nearest)
-
#count(*args) ⇒ Object
Extends the existing count method by: - If a mappable instance exists in the options and the distance column exists in the conditions, substitutes the distance sql for the distance column – this saves having to write the gory SQL.
-
#count_beyond(distance, options = {}) ⇒ Object
(also: #count_outside)
Counts beyond a distance radius.
-
#count_by_range(range, options = {}) ⇒ Object
Counts according to a range.
-
#count_within(distance, options = {}) ⇒ Object
(also: #count_inside)
counts within a distance radius.
-
#count_within_bounds(bounds, options = {}) ⇒ Object
Finds within rectangular bounds (sw,ne).
-
#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
-
#find(*args) ⇒ Object
Extends the existing find method in potentially two ways: - If a mappable instance exists in the options, adds a distance column.
-
#find_beyond(distance, options = {}) ⇒ Object
(also: #find_outside)
Finds beyond a distance radius.
-
#find_by_range(range, options = {}) ⇒ Object
Finds according to a range.
-
#find_closest(options = {}) ⇒ Object
(also: #find_nearest)
Finds the closest to the origin.
-
#find_farthest(options = {}) ⇒ Object
Finds the farthest from the origin.
-
#find_within(distance, options = {}) ⇒ Object
(also: #find_inside)
Finds within a distance radius.
-
#find_within_bounds(bounds, options = {}) ⇒ Object
Finds within rectangular bounds (sw,ne).
- #geo_scope(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.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 66 def adapter @adapter ||= begin require File.join(File.dirname(__FILE__), '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
83 84 85 86 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 83 def beyond(distance, = {}) [:beyond] = distance geo_scope() end |
#closest(options = {}) ⇒ Object Also known as: nearest
99 100 101 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 99 def closest( = {}) geo_scope().order("#{distance_column_name} asc").limit(1) end |
#count(*args) ⇒ Object
Extends the existing count method by:
-
If a mappable instance exists in the options and the distance column exists in the conditions, substitutes the distance sql for the distance column – this saves having to write the gory SQL.
116 117 118 119 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 116 def count(*args) prepare_for_find_or_count(:count, args) super(*args) end |
#count_beyond(distance, options = {}) ⇒ Object Also known as: count_outside
Counts beyond a distance radius.
166 167 168 169 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 166 def count_beyond(distance, ={}) [:beyond] = distance count() end |
#count_by_range(range, options = {}) ⇒ Object
Counts according to a range. Accepts inclusive or exclusive ranges.
173 174 175 176 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 173 def count_by_range(range, ={}) [:range] = range count() end |
#count_within(distance, options = {}) ⇒ Object Also known as: count_inside
counts within a distance radius.
159 160 161 162 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 159 def count_within(distance, ={}) [:within] = distance count() end |
#count_within_bounds(bounds, options = {}) ⇒ Object
Finds within rectangular bounds (sw,ne).
179 180 181 182 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 179 def count_within_bounds(bounds, ={}) [:bounds] = bounds count() 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.
149 150 151 152 153 154 155 156 157 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 149 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
104 105 106 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 104 def farthest( = {}) geo_scope().order("#{distance_column_name} desc").limit(1) end |
#find(*args) ⇒ Object
Extends the existing find method in potentially two ways:
-
If a mappable instance exists in the options, adds a distance column.
-
If a mappable instance exists in the options and the distance column exists in the conditions, substitutes the distance sql for the distance column – this saves having to write the gory SQL.
107 108 109 110 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 107 def find(*args) prepare_for_find_or_count(:find, args) super(*args) end |
#find_beyond(distance, options = {}) ⇒ Object Also known as: find_outside
Finds beyond a distance radius.
129 130 131 132 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 129 def find_beyond(distance, ={}) [:beyond] = distance find(:all, ) end |
#find_by_range(range, options = {}) ⇒ Object
Finds according to a range. Accepts inclusive or exclusive ranges.
136 137 138 139 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 136 def find_by_range(range, ={}) [:range] = range find(:all, ) end |
#find_closest(options = {}) ⇒ Object Also known as: find_nearest
Finds the closest to the origin.
142 143 144 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 142 def find_closest(={}) find(:nearest, ) end |
#find_farthest(options = {}) ⇒ Object
Finds the farthest from the origin.
148 149 150 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 148 def find_farthest(={}) find(:farthest, ) end |
#find_within(distance, options = {}) ⇒ Object Also known as: find_inside
Finds within a distance radius.
122 123 124 125 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 122 def find_within(distance, ={}) [:within] = distance find(:all, ) end |
#find_within_bounds(bounds, options = {}) ⇒ Object
Finds within rectangular bounds (sw,ne).
153 154 155 156 |
# File 'lib/geokit-rails3/acts_as_mappable.old.rb', line 153 def find_within_bounds(bounds, ={}) [:bounds] = bounds find(:all, ) end |
#geo_scope(options = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 108 def geo_scope( = {}) arel = self.is_a?(ActiveRecord::Relation) ? self : self.scoped origin = () units = () formula = () bounds = () if origin || bounds bounds = formulate_bounds_from_distance(, origin, units) unless bounds if origin @distance_formula = distance_sql(origin, units, formula) if arel.select_values.blank? star_select = Arel::SqlLiteral.new(arel.quoted_table_name + '.*') arel = arel.select(star_select) end distance_select = Arel::SqlLiteral.new("#{@distance_formula} AS #{distance_column_name}") arel = arel.select(distance_select) end if bounds bound_conditions = bound_conditions(bounds) arel = arel.where(bound_conditions) if bound_conditions end distance_conditions = distance_conditions() arel = arel.where(distance_conditions) if distance_conditions if origin arel = substitute_distance_in_where_values(arel, origin, units, formula) end end arel end |
#in_bounds(bounds, options = {}) ⇒ Object
94 95 96 97 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 94 def in_bounds(bounds, = {}) [:bounds] = bounds geo_scope() end |
#in_range(range, options = {}) ⇒ Object
89 90 91 92 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 89 def in_range(range, = {}) [:range] = range geo_scope() end |
#within(distance, options = {}) ⇒ Object Also known as: inside
77 78 79 80 |
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 77 def within(distance, = {}) [:within] = distance geo_scope() end |