aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-01-23 08:42:40 -0700
committerSean Griffin <sean@seantheprogrammer.com>2016-01-23 08:44:16 -0700
commit67c1719012506c3387df067961252b5df50a97ce (patch)
tree98daecc48f5df488d8e2147e51a71d4b07d2cd6f /activerecord/lib
parent8de32bb25295787b68d307c74e50462cd99e6ecc (diff)
downloadrails-67c1719012506c3387df067961252b5df50a97ce.tar.gz
rails-67c1719012506c3387df067961252b5df50a97ce.tar.bz2
rails-67c1719012506c3387df067961252b5df50a97ce.zip
Use the database type to deserialize enum
This fixes incorrect assumptions made by e991c7b that we can assume the DB is already casting the value for us. The enum type needs additional information to perform casting, and needs a subtype. I've opted not to call `super` in `cast`, as we have a known set of types which we accept there, and the subtype likely doesn't accept them (symbol -> integer doesn't make sense) Close #23190
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/enum.rb11
1 files changed, 7 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb
index 8655f68308..b942fa6273 100644
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ -105,9 +105,10 @@ module ActiveRecord
end
class EnumType < Type::Value # :nodoc:
- def initialize(name, mapping)
+ def initialize(name, mapping, subtype)
@name = name
@mapping = mapping
+ @subtype = subtype
end
def cast(value)
@@ -124,7 +125,7 @@ module ActiveRecord
def deserialize(value)
return if value.nil?
- mapping.key(value)
+ mapping.key(subtype.deserialize(value))
end
def serialize(value)
@@ -139,7 +140,7 @@ module ActiveRecord
protected
- attr_reader :name, :mapping
+ attr_reader :name, :mapping, :subtype
end
def enum(definitions)
@@ -158,7 +159,9 @@ module ActiveRecord
detect_enum_conflict!(name, name)
detect_enum_conflict!(name, "#{name}=")
- attribute name, EnumType.new(name, enum_values)
+ decorate_attribute_type(name, :enum) do |subtype|
+ EnumType.new(name, enum_values, subtype)
+ end
_enum_methods_module.module_eval do
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index