diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-09-24 11:50:11 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-09-24 14:06:59 -0600 |
commit | 8e633e505880755e7e366ccec2210bbe2b5436e7 (patch) | |
tree | 3df446051d47c42cd6081a1449a27d57f9179f88 /activerecord/lib/active_record/enum.rb | |
parent | adfb823af52d368fa4d88731a9809a314ad884ad (diff) | |
download | rails-8e633e505880755e7e366ccec2210bbe2b5436e7.tar.gz rails-8e633e505880755e7e366ccec2210bbe2b5436e7.tar.bz2 rails-8e633e505880755e7e366ccec2210bbe2b5436e7.zip |
Clean up the implementation of AR::Dirty
This moves a bit more of the logic required for dirty checking into the
attribute objects. I had hoped to remove the `with_value_from_database`
stuff, but unfortunately just calling `dup` on the attribute objects
isn't enough, since the values might contain deeply nested data
structures. I think this can be cleaned up further.
This makes most dirty checking become lazy, and reduces the number of
object allocations and amount of CPU time when assigning a value. This
opens the door (but doesn't quite finish) to improving the performance
of writes to a place comparable to 4.1
Diffstat (limited to 'activerecord/lib/active_record/enum.rb')
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index a79bf0366b..10b5fcab24 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -118,7 +118,7 @@ module ActiveRecord elsif mapping.has_value?(value) mapping.key(value) else - raise ArgumentError, "'#{value}' is not a valid #{name}" + assert_valid_value(value) end end @@ -131,6 +131,12 @@ module ActiveRecord mapping.fetch(value, value) end + def assert_valid_value(value) + unless value.blank? || mapping.has_key?(value) || mapping.has_value?(value) + raise ArgumentError, "'#{value}' is not a valid #{name}" + end + end + protected attr_reader :name, :mapping |