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

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, options = {})
  options[:beyond] = distance
  #geo_scope(options)
  where(distance_conditions(options))
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(options = {})
  origin  = extract_origin_from_options(options)
  units   = extract_units_from_options(options)
  formula = extract_formula_from_options(options)
  distance_column_name = distance_sql(origin, units, formula)
  with_latlng.order(
    Arel.sql(distance_column_name).send(options[: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(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.



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(options = {})
  by_distance({:reverse => true}.merge(options)).limit(1)
end

#geokit_finder_adapterObject

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, options = {})
  inclusive = options.delete(:inclusive) || false
  options[:bounds] = bounds
  #geo_scope(options)
  #where(distance_conditions(options))
  bounds  = extract_bounds_from_options(options)
  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, options = {})
  options[:range] = range
  #geo_scope(options)
  where(distance_conditions(options))
end

#with_latlngObject



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, options = {})
  options[:within] = distance
  # Add bounding box to speed up SQL request.
  bounds = formulate_bounds_from_distance(
    options,
    normalize_point_to_lat_lng(options[:origin]),
    options[:units] || default_units)
  with_latlng.where(bound_conditions(bounds)).
    where(distance_conditions(options))
end