Skip to content

Conversation

@r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Nov 19, 2025

Since there has been a long-standing desire to do so, I've made FactoryBot/AssociationStyle capable of handling inline associations.

Specifically, the need for inline association support has increased recently because autocorrection that generates inline association code has been newly added in the following change:

Simply put, code like the following, which was not considered an offense until now, will now be regarded as an offense:
(In addition, it supports autocorrect except for complex situations.)

# bad
factory :post do
  user { association :user }
end

# good
factory :post do
  association :user
end

For this change, I decided not to support EnforcedStyle: inline. This is because I personally have no desire to recommend the inline style even in situations where it can be substituted with other styles. Logically, it is feasible, but I believe it should only be supported when a clear use case emerges in the future.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@r7kamura r7kamura force-pushed the AssociationStyle-inline branch from e71d9a5 to 4084523 Compare November 19, 2025 05:02
@r7kamura
Copy link
Contributor Author

Other changes to the same cop are proceeding in parallel, and it's clear they will conflict, so I've put this change on hold in a draft state. Since this change is larger in scope, I believe it would be smoother to address the smaller, earlier changes first, and then handle this one.

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.

Redundant blocks for factory and association

1 participant