Module: Whereabouts::ClassMethods

Defined in:
lib/whereabouts_methods.rb

Instance Method Summary collapse

Instance Method Details

#has_whereabouts(klass = :address, *args) ⇒ Object

Accepts a symbol that will define the inherited type of Address. Defaults to the parent class.



7
8
9
10
11
12
13
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
# File 'lib/whereabouts_methods.rb', line 7

def has_whereabouts klass=:address, *args
  options = args.extract_options!
  # extend Address with class name if not defined.
  unless klass == :address || Object.const_defined?(klass.to_s.camelize)
    create_address_class(klass.to_s.camelize)
  end

  # Set the has_one relationship and accepts_nested_attributes_for.  
  has_one klass, :as => :addressable, :dependent => :destroy
  accepts_nested_attributes_for klass

  # Define a singleton on the class that returns an array
  # that includes the address fields to validate presence of 
  # or an empty array
  if options[:validate] && options[:validate].is_a?(Array)
    validators = options[:validate]
    set_validators(klass, validators)
  else
    validators = []
  end
  define_singleton_method validate_singleton_for(klass) do validators end

  # Check for geocode in options and confirm geocoder is defined.
  # Also defines a singleton to return a boolean about geocoding.
  if options[:geocode] && options[:geocode] == true && defined?(Geocoder)
    geocode = true
    set_geocoding(klass)
  else
    geocode = false
  end
  define_singleton_method geocode_singleton_for(klass) do geocode end
end