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

Instance Method Details

#adapterObject

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, options = {})
  options[:beyond] = distance
  #geo_scope(options)
  where(distance_conditions(options))
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(options = {})
  origin  = extract_origin_from_options(options)
  units   = extract_units_from_options(options)
  formula = extract_formula_from_options(options)
  bounds  = extract_bounds_from_options(options)
  distance_column_name = distance_sql(origin, units, formula)
  #geo_scope(options).order("#{distance_column_name} asc")
  order("#{distance_column_name} #{options[: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(options = {})
  by_distance(options).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(options = {})
  by_distance({:reverse => true}.merge(options)).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, options = {})
  options[:bounds] = bounds
  #geo_scope(options)
  #where(distance_conditions(options))
  bounds  = extract_bounds_from_options(options)
  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, options = {})
  options[:range] = range
  #geo_scope(options)
  where(distance_conditions(options))
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, options = {})
  options[:within] = distance
  #geo_scope(options)
  where(distance_conditions(options))
end