Module: Enumerize::ActiveRecordSupport::InstanceMethods
- Defined in:
- lib/enumerize/activerecord.rb
Instance Method Summary collapse
-
#becomes(klass) ⇒ Object
Support multiple enumerized attributes.
- #reload(options = nil) ⇒ Object
- #write_attribute(attr_name, value, *options) ⇒ Object
Instance Method Details
#becomes(klass) ⇒ Object
Support multiple enumerized attributes
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/enumerize/activerecord.rb', line 53 def becomes(klass) became = super klass.enumerized_attributes.each do |attr| # Rescue when column associated to the enum does not exist. begin became.send("#{attr.name}=", send(attr.name)) rescue ActiveModel::MissingAttributeError end end became end |
#reload(options = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/enumerize/activerecord.rb', line 66 def reload( = nil) reloaded = super reloaded.class.enumerized_attributes.each do |attr| begin # Checks first if the enumerized attribute is in ActiveRecord::Store store_attr, _ = reloaded.class.stored_attributes.detect do |_store_attr, keys| keys.include?(attr.name) end if store_attr.present? reloaded.send("#{attr.name}=", reloaded.send(store_attr).with_indifferent_access[attr.name]) else reloaded.send("#{attr.name}=", reloaded[attr.name]) end rescue ActiveModel::MissingAttributeError end end reloaded end |
#write_attribute(attr_name, value, *options) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/enumerize/activerecord.rb', line 44 def write_attribute(attr_name, value, *) if self.class.enumerized_attributes[attr_name] _enumerized_values_for_validation[attr_name.to_s] = value end super end |