Module: Enumeration::ClassMethods

Defined in:
lib/enumerations.rb

Instance Method Summary collapse

Instance Method Details

#enumeration(name, options = {}) ⇒ Object

create an enumeration for the symbol name. Options include foreign key attribute and Class name



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/enumerations.rb', line 34

def enumeration(name, options = {})
  options[:foreign_key] ||= "#{name}_id".to_sym
  options[:class_name] ||= name.to_s.camelize
  
  # getter for belongs_to
  define_method name do
    options[:class_name].constantize.find(send(options[:foreign_key]))
  end
  
  # setter for belongs_to 
  define_method "#{name}=" do |other|
    send("#{options[:foreign_key]}=", other.id)
  end
end