From 5620e62f3f1920e70199a870a0a3c4feaedb9f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Jan 2014 20:32:52 -0200 Subject: Store the enum values in the DEFINED_ENUM constant This will make simpler to compare if the values changed in the save_changed_attribute method. --- activerecord/lib/active_record/enum.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 0990a4a871..77a5fe9ae0 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -63,10 +63,10 @@ module ActiveRecord # # Conversation.where("status <> ?", Conversation.statuses[:archived]) module Enum - DEFINED_ENUMS = [] # :nodoc: + DEFINED_ENUMS = {} # :nodoc: - def enum_attribute?(attr_name) # :nodoc: - DEFINED_ENUMS.include?(attr_name.to_sym) + def enum_mapping_for(attr_name) # :nodoc: + DEFINED_ENUMS[attr_name.to_sym] end def enum(definitions) @@ -76,8 +76,6 @@ module ActiveRecord enum_values = ActiveSupport::HashWithIndifferentAccess.new name = name.to_sym - DEFINED_ENUMS.unshift name - # def self.statuses statuses end klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values } @@ -115,6 +113,8 @@ module ActiveRecord # def active!() update! status: :active end define_method("#{value}!") { update! name => value } end + + DEFINED_ENUMS[name] = enum_values end end end @@ -125,18 +125,18 @@ module ActiveRecord mod = Module.new do private def save_changed_attribute(attr_name, value) - if self.class.enum_attribute?(attr_name) + if (mapping = self.class.enum_mapping_for(attr_name)) if attribute_changed?(attr_name) old = changed_attributes[attr_name] - if self.class.public_send(attr_name.pluralize)[old] == value + if mapping[old] == value changed_attributes.delete(attr_name) end else old = clone_attribute_value(:read_attribute, attr_name) if old != value - changed_attributes[attr_name] = self.class.public_send(attr_name.pluralize).key old + changed_attributes[attr_name] = mapping.key old end end else -- cgit v1.2.3