Module: Enumeration::ClassMethods
- Defined in:
- lib/enumerations.rb
Instance Method Summary collapse
-
#enumeration(name, options = {}) ⇒ Object
create an enumeration for the symbol
name
.
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, = {}) [:foreign_key] ||= "#{name}_id".to_sym [:class_name] ||= name.to_s.camelize # getter for belongs_to define_method name do [:class_name].constantize.find(send([:foreign_key])) end # setter for belongs_to define_method "#{name}=" do |other| send("#{[:foreign_key]}=", other.id) end end |