thoughtbot / thoughtbot/factory_bot

Question/Request: ability to access original arguments from within attribute blocks

Open
#1,741 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

I'm trying to figure out how to solve an issue with circular dependencies. For example, I want to create a factory that accepts one of three associations. Whichever association is not provided would be created and associated with the one that was provided.

# pseudo example
factory :person do
  parent { create :parent }
  child { create :child, parent: parent }
  something { create :something, parent: parent }
end

# usage
create(:person, parent: parent)
create(:person, child: child)
create(:person, something: something)

The problem I'm running into is not knowing which of these values was passed originally in the call to create. It's also valid to pass nil in order to instantiate the model with a missing association.

I've tried doing this in after(:build), but I have no way of knowing if the association was nil or if it was created because it wasn't specified.

If there was some way to access the original arguments, then I could know which one was provided. Is there already a way to do this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points. Start by reproducing the three factory calls and compare omitted associations with associations explicitly passed as nil; done should mean attribute blocks can distinguish those original arguments without breaking existing factory behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.