mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
df35d93adf
commit
7b741457e5
6 changed files with 21 additions and 1 deletions
|
@ -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.
|
* `#authenticate` returns false when the password is blank instead of raising an error.
|
||||||
|
|
||||||
*Muhammad Muhammad Ibrahim*
|
*Muhammad Muhammad Ibrahim*
|
||||||
|
|
|
@ -126,7 +126,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolve_enums
|
def resolve_enums
|
||||||
model_class.defined_enums.each do |name, values|
|
reflection_class.defined_enums.each do |name, values|
|
||||||
if @row.include?(name)
|
if @row.include?(name)
|
||||||
@row[name] = values.fetch(@row[name], @row[name])
|
@row[name] = values.fetch(@row[name], @row[name])
|
||||||
end
|
end
|
||||||
|
|
|
@ -1331,6 +1331,12 @@ class FoxyFixturesTest < ActiveRecord::TestCase
|
||||||
assert_equal pirates(:blackbeard), dead_parrots(:deadbird).killer
|
assert_equal pirates(:blackbeard), dead_parrots(:deadbird).killer
|
||||||
end
|
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
|
def test_namespaced_models
|
||||||
assert_includes admin_accounts(:signals37).users, admin_users(:david)
|
assert_includes admin_accounts(:signals37).users, admin_users(:david)
|
||||||
assert_equal 2, admin_accounts(:signals37).users.size
|
assert_equal 2, admin_accounts(:signals37).users.size
|
||||||
|
|
4
activerecord/test/fixtures/parrots.yml
vendored
4
activerecord/test/fixtures/parrots.yml
vendored
|
@ -8,15 +8,18 @@ george:
|
||||||
name: "Curious George"
|
name: "Curious George"
|
||||||
treasures: diamond, sapphire
|
treasures: diamond, sapphire
|
||||||
parrot_sti_class: LiveParrot
|
parrot_sti_class: LiveParrot
|
||||||
|
breed: australian
|
||||||
|
|
||||||
louis:
|
louis:
|
||||||
name: "King Louis"
|
name: "King Louis"
|
||||||
treasures: [diamond, sapphire]
|
treasures: [diamond, sapphire]
|
||||||
parrot_sti_class: LiveParrot
|
parrot_sti_class: LiveParrot
|
||||||
|
breed: african
|
||||||
|
|
||||||
frederick:
|
frederick:
|
||||||
name: $LABEL
|
name: $LABEL
|
||||||
parrot_sti_class: LiveParrot
|
parrot_sti_class: LiveParrot
|
||||||
|
breed: african
|
||||||
|
|
||||||
polly:
|
polly:
|
||||||
id: 4
|
id: 4
|
||||||
|
@ -28,6 +31,7 @@ polly:
|
||||||
DEFAULTS: &DEFAULTS
|
DEFAULTS: &DEFAULTS
|
||||||
treasures: sapphire, ruby
|
treasures: sapphire, ruby
|
||||||
parrot_sti_class: LiveParrot
|
parrot_sti_class: LiveParrot
|
||||||
|
breed: australian
|
||||||
|
|
||||||
davey:
|
davey:
|
||||||
*DEFAULTS
|
*DEFAULTS
|
||||||
|
|
|
@ -29,6 +29,7 @@ class Parrot < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class LiveParrot < Parrot
|
class LiveParrot < Parrot
|
||||||
|
enum breed: { african: 0, australian: 1 }
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeadParrot < Parrot
|
class DeadParrot < Parrot
|
||||||
|
|
|
@ -734,6 +734,7 @@ ActiveRecord::Schema.define do
|
||||||
disable_referential_integrity do
|
disable_referential_integrity do
|
||||||
create_table :parrots, force: :cascade do |t|
|
create_table :parrots, force: :cascade do |t|
|
||||||
t.string :name
|
t.string :name
|
||||||
|
t.integer :breed, default: 0
|
||||||
t.string :color
|
t.string :color
|
||||||
t.string :parrot_sti_class
|
t.string :parrot_sti_class
|
||||||
t.integer :killer_id
|
t.integer :killer_id
|
||||||
|
|
Loading…
Reference in a new issue