Skip to content

Commit 7b74145

Browse files
Andres Howardahowardm
authored andcommitted
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.
1 parent df35d93 commit 7b74145

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Load STI Models in fixtures
2+
3+
Data from Fixtures now loads based on the specific class for models with
4+
Single Table Inheritance. This affects enums defined in subclasses, previously
5+
the value of these fields was not parsed and remained `nil`
6+
7+
*Andres Howard*
8+
19
* `#authenticate` returns false when the password is blank instead of raising an error.
210

311
*Muhammad Muhammad Ibrahim*

activerecord/lib/active_record/fixture_set/table_row.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def generate_primary_key
126126
end
127127

128128
def resolve_enums
129-
model_class.defined_enums.each do |name, values|
129+
reflection_class.defined_enums.each do |name, values|
130130
if @row.include?(name)
131131
@row[name] = values.fetch(@row[name], @row[name])
132132
end

activerecord/test/cases/fixtures_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,12 @@ def test_supports_sti_with_respective_files
13311331
assert_equal pirates(:blackbeard), dead_parrots(:deadbird).killer
13321332
end
13331333

1334+
def test_resolves_enums_in_sti_subclasses
1335+
assert_predicate parrots(:george), :australian?
1336+
assert_predicate parrots(:louis), :african?
1337+
assert_predicate parrots(:frederick), :african?
1338+
end
1339+
13341340
def test_namespaced_models
13351341
assert_includes admin_accounts(:signals37).users, admin_users(:david)
13361342
assert_equal 2, admin_accounts(:signals37).users.size

activerecord/test/fixtures/parrots.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ george:
88
name: "Curious George"
99
treasures: diamond, sapphire
1010
parrot_sti_class: LiveParrot
11+
breed: australian
1112

1213
louis:
1314
name: "King Louis"
1415
treasures: [diamond, sapphire]
1516
parrot_sti_class: LiveParrot
17+
breed: african
1618

1719
frederick:
1820
name: $LABEL
1921
parrot_sti_class: LiveParrot
22+
breed: african
2023

2124
polly:
2225
id: 4
@@ -28,6 +31,7 @@ polly:
2831
DEFAULTS: &DEFAULTS
2932
treasures: sapphire, ruby
3033
parrot_sti_class: LiveParrot
34+
breed: australian
3135

3236
davey:
3337
*DEFAULTS

activerecord/test/models/parrot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def self.delete_all(*)
2929
end
3030

3131
class LiveParrot < Parrot
32+
enum breed: { african: 0, australian: 1 }
3233
end
3334

3435
class DeadParrot < Parrot

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@
734734
disable_referential_integrity do
735735
create_table :parrots, force: :cascade do |t|
736736
t.string :name
737+
t.integer :breed, default: 0
737738
t.string :color
738739
t.string :parrot_sti_class
739740
t.integer :killer_id

0 commit comments

Comments
 (0)