Skip to content
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

Merged
merged 1 commit into from Sep 30, 2015

Conversation

repinel
Copy link
Member

@repinel repinel commented Jun 16, 2015

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).

If the PR goes forward, should we consider deprecating set_fixture_class?

Fixes #9516.

@repinel
Copy link
Member Author

repinel commented Jun 16, 2015

@senny This would be the idea that we were discussing on #9516.

/cc @jeremy

@repinel
Copy link
Member Author

repinel commented Jul 10, 2015

@senny @jeremy Any thoughts on this?

@jeremy
Copy link
Member

jeremy commented Aug 13, 2015

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

@repinel
Copy link
Member Author

repinel commented Aug 13, 2015

@jeremy That is close to my first thought. IMO, it seems good.

Should I update the RP?

@senny
Copy link
Member

senny commented Aug 13, 2015

@repinel yes, please do.

@repinel
Copy link
Member Author

repinel commented Aug 13, 2015

Closing and reopening to run tests.

@repinel repinel closed this Aug 13, 2015
@repinel repinel reopened this Aug 13, 2015
@@ -370,6 +374,11 @@ def except(adapter_names_to_exclude)
t.integer :ideal_reference_id
end

create_table :joke_comments, force: true do |t|
Copy link
Member

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.

@senny
Copy link
Member

senny commented Aug 14, 2015

I think set_fixture_class should have priority over _fixture:model_class. This way you could configure the class inside the .yml file and still overwrite that inside a test case.

# 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:
Copy link
Member

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.

@repinel
Copy link
Member Author

repinel commented Aug 17, 2015

@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Member Author

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.

def model_class
return @model_class if @model_class

rows.delete_if do |fixture_name, row|
Copy link
Member

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`).
@repinel
Copy link
Member Author

repinel commented Sep 11, 2015

@senny I re-implemented the AR::FixtureSet::File#model_class method as suggested 😃

@rafaelfranca
Copy link
Member

r? @senny

@senny senny merged commit 2acec46 into rails:master Sep 30, 2015
senny added a commit that referenced this pull request Sep 30, 2015
Allow fixtures YAML files to set the model class in the file itself

Conflicts:
	activerecord/CHANGELOG.md
@senny
Copy link
Member

senny commented Sep 30, 2015

🎉

@repinel thanks for your work and patience 💛. Finally found the time to finish this up. I made some minor changes directly in the merge commit (591a0bb)

@repinel
Copy link
Member Author

repinel commented Sep 30, 2015

@senny Looks awesome! Thanks for reviewing it ❤️

@repinel repinel deleted the fix-db-fixtures-load branch September 30, 2015 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rake db:fixtures:load - can't set fixture_class_name.
4 participants