From 788bb40e3887e2718f07be769b80818d653638f0 Mon Sep 17 00:00:00 2001
From: Godfrey Chan <godfreykfc@gmail.com>
Date: Fri, 3 Jan 2014 09:31:01 -0800
Subject: Building new records with enum scopes now works as expected

Previously, this would give an `ArgumentError`:

   class Issue < ActiveRecord::Base
     enum :status, [:open, :finished]
   end

   Issue.open.build # => ArgumentError: '0' is not a valid status
   Issue.open.create # => ArgumentError: '0' is not a valid status

PR #13542 muted the error, but the issue remains. This commit fixes
the issue by allowing the enum value to be written directly via the
setter:

   Issue.new.status = 0 # This now sets status to :open

Assigning a value directly via the setter like this is not part of the
documented public API, so users should not rely on this behavior.

Closes #13530.
---
 activerecord/test/cases/enum_test.rb | 13 +++++++------
 activerecord/test/schema/schema.rb   |  4 ----
 2 files changed, 7 insertions(+), 10 deletions(-)

(limited to 'activerecord/test')

diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 47e3dfc3ad..c09e58fbf1 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -79,12 +79,13 @@ class EnumTest < ActiveRecord::TestCase
     assert_equal 2, Book::STATUS[:published]
   end
 
-  test "first_or_initialize with enums' scopes" do
-    class Issue < ActiveRecord::Base
-      enum status: [:open, :closed]
-    end
+  test "building new objects with enum scopes" do
+    assert Book.written.build.written?
+    assert Book.read.build.read?
+  end
 
-    assert Issue.open.empty?
-    assert Issue.open.first_or_initialize
+  test "creating new objects with enum scopes" do
+    assert Book.written.create.written?
+    assert Book.read.create.read?
   end
 end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 9f504801af..ac546fc296 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -327,10 +327,6 @@ ActiveRecord::Schema.define do
     t.string     :color
   end
 
-  create_table :issues, force: true do |t|
-    t.integer :status
-  end
-
   create_table :items, force: true do |t|
     t.column :name, :string
   end
-- 
cgit v1.2.3