Module: Geokit::ActsAsMappable::Glue::ClassMethods

Defined in:
lib/geokit-rails3/acts_as_mappable.rb,
lib/geokit-rails3/acts_as_mappable.old.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#acts_as_mappable(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/geokit-rails3/acts_as_mappable.rb', line 14

def acts_as_mappable(options = {})
  metaclass = (class << self; self; end)

  include Geokit::ActsAsMappable

  cattr_accessor :through
  self.through = options[:through]

  if reflection = Geokit::ActsAsMappable.end_of_reflection_chain(self.through, self)
    metaclass.instance_eval do
      [ :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name ].each do |method_name|
        define_method method_name do
          reflection.klass.send(method_name)
        end
      end
    end
  else
    cattr_accessor :distance_column_name, :default_units, :default_formula, :lat_column_name, :lng_column_name, :qualified_lat_column_name, :qualified_lng_column_name

    self.distance_column_name = options[:distance_column_name]  || 'distance'
    self.default_units = options[:default_units] || Geokit::default_units
    self.default_formula = options[:default_formula] || Geokit::default_formula
    self.lat_column_name = options[:lat_column_name] || 'lat'
    self.lng_column_name = options[:lng_column_name] || 'lng'
    self.qualified_lat_column_name = "#{table_name}.#{lat_column_name}"
    self.qualified_lng_column_name = "#{table_name}.#{lng_column_name}"

    if options.include?(:auto_geocode) && options[:auto_geocode]
      # if the form auto_geocode=>true is used, let the defaults take over by suppling an empty hash
      options[:auto_geocode] = {} if options[:auto_geocode] == true
      cattr_accessor :auto_geocode_field, :auto_geocode_error_message
      self.auto_geocode_field = options[:auto_geocode][:field] || 'address'
      self.auto_geocode_error_message = options[:auto_geocode][:error_message] || 'could not locate address'

      # set the actual callback here
      before_validation :auto_geocode_address, :on => :create
    end
  end
end