From 7b741457e5618d6d7cd229554bbf9e67fc7c00ff Mon Sep 17 00:00:00 2001 From: Andres Howard Date: Mon, 30 Aug 2021 21:00:52 +0000 Subject: [PATCH] Use reflection_class in TableRow for fixtures With models that use STI, Fixtures now load based on the refelction class. This allows to resolve the enums for each specific class instead of just resolving those of the base class. --- activerecord/CHANGELOG.md | 8 ++++++++ activerecord/lib/active_record/fixture_set/table_row.rb | 2 +- activerecord/test/cases/fixtures_test.rb | 6 ++++++ activerecord/test/fixtures/parrots.yml | 4 ++++ activerecord/test/models/parrot.rb | 1 + activerecord/test/schema/schema.rb | 1 + 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 003dfdc52e..15ae3a3103 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,11 @@ +* Load STI Models in fixtures + + Data from Fixtures now loads based on the specific class for models with + Single Table Inheritance. This affects enums defined in subclasses, previously + the value of these fields was not parsed and remained `nil` + + *Andres Howard* + * `#authenticate` returns false when the password is blank instead of raising an error. *Muhammad Muhammad Ibrahim* diff --git a/activerecord/lib/active_record/fixture_set/table_row.rb b/activerecord/lib/active_record/fixture_set/table_row.rb index c96c0fed62..6e5c720e63 100644 --- a/activerecord/lib/active_record/fixture_set/table_row.rb +++ b/activerecord/lib/active_record/fixture_set/table_row.rb @@ -126,7 +126,7 @@ module ActiveRecord end def resolve_enums - model_class.defined_enums.each do |name, values| + reflection_class.defined_enums.each do |name, values| if @row.include?(name) @row[name] = values.fetch(@row[name], @row[name]) end diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index a3e089a6ee..67b37c81d9 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -1331,6 +1331,12 @@ class FoxyFixturesTest < ActiveRecord::TestCase assert_equal pirates(:blackbeard), dead_parrots(:deadbird).killer end + def test_resolves_enums_in_sti_subclasses + assert_predicate parrots(:george), :australian? + assert_predicate parrots(:louis), :african? + assert_predicate parrots(:frederick), :african? + end + def test_namespaced_models assert_includes admin_accounts(:signals37).users, admin_users(:david) assert_equal 2, admin_accounts(:signals37).users.size diff --git a/activerecord/test/fixtures/parrots.yml b/activerecord/test/fixtures/parrots.yml index 4f0a090e03..bf4296a03e 100644 --- a/activerecord/test/fixtures/parrots.yml +++ b/activerecord/test/fixtures/parrots.yml @@ -8,15 +8,18 @@ george: name: "Curious George" treasures: diamond, sapphire parrot_sti_class: LiveParrot + breed: australian louis: name: "King Louis" treasures: [diamond, sapphire] parrot_sti_class: LiveParrot + breed: african frederick: name: $LABEL parrot_sti_class: LiveParrot + breed: african polly: id: 4 @@ -28,6 +31,7 @@ polly: DEFAULTS: &DEFAULTS treasures: sapphire, ruby parrot_sti_class: LiveParrot + breed: australian davey: *DEFAULTS diff --git a/activerecord/test/models/parrot.rb b/activerecord/test/models/parrot.rb index addeb3eea8..888b66323d 100644 --- a/activerecord/test/models/parrot.rb +++ b/activerecord/test/models/parrot.rb @@ -29,6 +29,7 @@ class Parrot < ActiveRecord::Base end class LiveParrot < Parrot + enum breed: { african: 0, australian: 1 } end class DeadParrot < Parrot diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 056d59dcac..c564ea5d82 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -734,6 +734,7 @@ ActiveRecord::Schema.define do disable_referential_integrity do create_table :parrots, force: :cascade do |t| t.string :name + t.integer :breed, default: 0 t.string :color t.string :parrot_sti_class t.integer :killer_id