New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow fixtures YAML files to set the model class in the file itself #20574
Conversation
Using ERB within yaml to set metadata feels surprising. How about using a special key to set fixture metadata? _fixture:
model_class: User
david:
name: David |
@jeremy That is close to my first thought. IMO, it seems good. Should I update the RP? |
@repinel yes, please do. |
f57b3b3
to
d69d507
Compare
Closing and reopening to run tests. |
@@ -370,6 +374,11 @@ def except(adapter_names_to_exclude) | |||
t.integer :ideal_reference_id | |||
end | |||
|
|||
create_table :joke_comments, force: true do |t| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it necessary to create new tables to test this feature? Can't we reuse some tables that already exist? We try to not add more global models for single test scenarios like this.
I think |
# you cannot use +set_fixture_class+, e.g., when running | ||
# <tt>rake db:fixtures:load</tt>. | ||
# | ||
# To load the fixtures file `accounts.yml` as the `User` model, use: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's give the full path here: test/fixtures/accounts.yml
.
d69d507
to
0167801
Compare
@senny I updated the code. Please review, thanks! |
@@ -790,7 +814,7 @@ class FixtureError < StandardError #:nodoc: | |||
class FormatError < FixtureError #:nodoc: | |||
end | |||
|
|||
attr_reader :model_class, :fixture | |||
attr_reader :fixture, :model_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this one. I'll revert the line.
0167801
to
77b0a6b
Compare
def model_class | ||
return @model_class if @model_class | ||
|
||
rows.delete_if do |fixture_name, row| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is based on the fact that model_class
will be called before each
, otherwise you'd get a fixture for _fixture
. We should probably change it to behave consistently regardless of call order. Meaning that we have to treat the _fixture
key special that it won't ever be part of rows.
This could look something like this (pseudocode):
def model_class
raw_content["_fixture"]
# ...
end
def rows
raw_content.except "_fixture"
end
def raw_content
# read yaml-file and cache contents
end
Currently, `set_fixture_class` is only available using the `TestFixtures` concern and it is ignored for `rake db:fixtures:load`. Using the correct model class, it is possible for the fixture load to also load the associations from the YAML files (e.g., `:belongs_to` and `:has_many`).
77b0a6b
to
2acec46
Compare
@senny I re-implemented the |
r? @senny |
Allow fixtures YAML files to set the model class in the file itself Conflicts: activerecord/CHANGELOG.md
@senny Looks awesome! Thanks for reviewing it |
Currently,
set_fixture_class
is only available using theTestFixtures
concern and it is ignored forrake db:fixtures:load
. Using the correct model class, it is possible for the fixture load to also load the associations from the YAML files (e.g.,:belongs_to
and:has_many
).If the PR goes forward, should we consider deprecating
set_fixture_class
?Fixes #9516.