From 792c66f868b27cfd4b71c541202caae4bee1d172 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 11 Jan 2014 12:57:09 +0100 Subject: use enum labels as form values. Achieved by `_before_type_cast`. Closes #13650, #13672 This is an alternate implementation to solve #13650. Currently form fields contain the enum value (eg. "1"). This breaks because the setter `enum=` expects the label (eg. "active"). ActiveRecord::Enum allows you to use labels in your application but store numbers. We should make sure that all parts after AR are dealing with labels and not the underlying mapping to a number. This patch defines `_before_type_cast` on every enum column to return the label. This method is later used to fetch the value to display in form fields. I deliberately copied the implementation of the enum getter instead of delegating to it. This allows you to overwrite the getter and for example return a `Value Object` but have it still work for form fields. --- activerecord/test/cases/enum_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index c09e58fbf1..0075df8b8d 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -88,4 +88,8 @@ class EnumTest < ActiveRecord::TestCase assert Book.written.create.written? assert Book.read.create.read? end + + test "_before_type_cast returns the enum label (required for form fields)" do + assert_equal "proposed", @book.status_before_type_cast + end end -- cgit v1.2.3 From e95031f55dd54945abebe6e9f8a12e428ada4146 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Mon, 13 Jan 2014 15:04:23 +0100 Subject: fix bug in becomes! when changing from base to subclass. Closes #13272. --- activerecord/test/cases/inheritance_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index cb0d374ef7..7fd7d42354 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -128,6 +128,17 @@ class InheritanceTest < ActiveRecord::TestCase assert_kind_of Cabbage, cabbage end + def test_alt_becomes_bang_resets_inheritance_type_column + vegetable = Vegetable.create!(name: "Red Pepper") + assert_nil vegetable.custom_type + + cabbage = vegetable.becomes!(Cabbage) + assert_equal "Cabbage", cabbage.custom_type + + vegetable = cabbage.becomes!(Vegetable) + assert_nil cabbage.custom_type + end + def test_inheritance_find_all companies = Company.all.merge!(:order => 'id').to_a assert_kind_of Firm, companies[0], "37signals should be a firm" -- cgit v1.2.3 From d220e8add9071ffcc12bfe8cf78c995ef53cb6d4 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 13 Jan 2014 15:02:28 -0800 Subject: recover from test runs that leave the database in a bad state --- activerecord/test/cases/hot_compatibility_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/hot_compatibility_test.rb b/activerecord/test/cases/hot_compatibility_test.rb index 96e581ab4c..367d04a154 100644 --- a/activerecord/test/cases/hot_compatibility_test.rb +++ b/activerecord/test/cases/hot_compatibility_test.rb @@ -5,7 +5,7 @@ class HotCompatibilityTest < ActiveRecord::TestCase setup do @klass = Class.new(ActiveRecord::Base) do - connection.create_table :hot_compatibilities do |t| + connection.create_table :hot_compatibilities, force: true do |t| t.string :foo t.string :bar end -- cgit v1.2.3 From f1764a2de17bba07bad3ec0d88de152d8279d3bb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 13 Jan 2014 16:15:20 -0800 Subject: unscope should remove bind values associated with the where --- activerecord/test/cases/relations_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 031da8e6d6..a0896f7f8d 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1524,6 +1524,15 @@ class RelationTest < ActiveRecord::TestCase assert merged.to_sql.include?("bbq") end + def test_unscope_removes_binds + left = Post.where(id: Arel::Nodes::BindParam.new('?')) + column = Post.columns_hash['id'] + left.bind_values += [[column, 20]] + + relation = left.unscope(where: :id) + assert_equal [], relation.bind_values + end + def test_merging_removes_rhs_bind_parameters left = Post.where(id: Arel::Nodes::BindParam.new('?')) column = Post.columns_hash['id'] -- cgit v1.2.3