diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2016-01-23 08:42:40 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2016-01-23 08:44:16 -0700 |
commit | 67c1719012506c3387df067961252b5df50a97ce (patch) | |
tree | 98daecc48f5df488d8e2147e51a71d4b07d2cd6f /activerecord/test | |
parent | 8de32bb25295787b68d307c74e50462cd99e6ecc (diff) | |
download | rails-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/test')
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/models/book.rb | 1 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
3 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 7c930de97b..babacd1ee9 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -411,4 +411,14 @@ class EnumTest < ActiveRecord::TestCase assert book.proposed?, "expected fixture to default to proposed status" assert book.in_english?, "expected fixture to default to english language" end + + test "uses default value from database on initialization" do + book = Book.new + assert book.proposed? + end + + test "uses default value from database on initialization when using custom mapping" do + book = Book.new + assert book.hard? + end end diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb index 1927191393..e43e5c3901 100644 --- a/activerecord/test/models/book.rb +++ b/activerecord/test/models/book.rb @@ -14,6 +14,7 @@ class Book < ActiveRecord::Base enum author_visibility: [:visible, :invisible], _prefix: true enum illustrator_visibility: [:visible, :invisible], _prefix: true enum font_size: [:small, :medium, :large], _prefix: :with, _suffix: true + enum cover: { hard: 'hard', soft: 'soft' } def published! super diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 025184f63a..2e1e584156 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -104,6 +104,7 @@ ActiveRecord::Schema.define do t.column :author_visibility, :integer, default: 0 t.column :illustrator_visibility, :integer, default: 0 t.column :font_size, :integer, default: 0 + t.column :cover, :string, default: 'hard' end create_table :booleans, force: true do |t| |