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

Instance Method Details

#adapterObject

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, options = {})
  options[:beyond] = distance
  geo_scope(options)
end

#closest(options = {}) ⇒ Object Also known as: nearest



99
100
101
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 99

def closest(options = {})
  geo_scope(options).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, options={})
  options[:beyond] = distance
  count(options)
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, options={})
  options[:range] = range
  count(options)
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, options={})
  options[:within] = distance
  count(options)
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, options={})
  options[:bounds] = bounds
  count(options)
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(options = {})
  geo_scope(options).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, options={})
  options[:beyond] = distance
  find(:all, options)
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, options={})
  options[:range] = range
  find(:all, options)
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(options={})
  find(:nearest, options)
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(options={})
  find(:farthest, options)
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, options={})
  options[:within] = distance
  find(:all, options)
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, options={})
  options[:bounds] = bounds
  find(:all, options)
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(options = {})
  arel = self.is_a?(ActiveRecord::Relation) ? self : self.scoped

  origin  = extract_origin_from_options(options)
  units   = extract_units_from_options(options)
  formula = extract_formula_from_options(options)
  bounds  = extract_bounds_from_options(options)

  if origin || bounds
    bounds = formulate_bounds_from_distance(options, 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(options)
    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, options = {})
  options[:bounds] = bounds
  geo_scope(options)
end

#in_range(range, options = {}) ⇒ Object



89
90
91
92
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 89

def in_range(range, options = {})
  options[:range] = range
  geo_scope(options)
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, options = {})
  options[:within] = distance
  geo_scope(options)
end