aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-21 20:32:52 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-21 20:32:52 -0200
commit5620e62f3f1920e70199a870a0a3c4feaedb9f98 (patch)
tree489fc50ffee737d661440d736d3f0a27792ce3ae
parenta0520fceff9148ebfbb2e09745ba1416bceef2bf (diff)
downloadrails-5620e62f3f1920e70199a870a0a3c4feaedb9f98.tar.gz
rails-5620e62f3f1920e70199a870a0a3c4feaedb9f98.tar.bz2
rails-5620e62f3f1920e70199a870a0a3c4feaedb9f98.zip
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.
-rw-r--r--activerecord/lib/active_record/enum.rb16
1 files 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