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

Expose which keys were overridden by the user #1649

Open
mike-burns opened this issue May 10, 2024 · 2 comments · May be fixed by #1695
Open

Expose which keys were overridden by the user #1649

mike-burns opened this issue May 10, 2024 · 2 comments · May be fixed by #1695
Labels

Comments

@mike-burns
Copy link
Contributor

mike-burns commented May 10, 2024

The context, as passed to the various hooks, does not tell us whether a key was overridden by the user when building/creating/stubbing the object. This means we can't know whether a value is the default, or intentionally set.

Let's expose this on the context. It could be as simple as a list of keys set by the user.

@Zooip
Copy link

Zooip commented May 16, 2024

The list of keys is already exposed in the evaluator using the private undocumented method __override_names__ : https://github.com/thoughtbot/factory_bot/blob/main/lib/factory_bot/evaluator.rb#L50-L52
It would make sense to me to make this method part of the public stable API.

I personnaly use it to "sync" associations' associations like so :

transient do
  school do
    if __override_names__.include?(:exam)
      exam.school
    elsif __override_names__.include?(:student)
      student.school
    else
      association :school
    end
  end
end

exam { association :exam, school: school }
student { association :student, school: school }

So you technically already can use this in the callbacks but it is undocumented and not part of the public API

after(:build) do |instance, evaluator|
  evaluator.__override_names_
end

@CodeMeister CodeMeister linked a pull request Sep 8, 2024 that will close this issue
@CodeMeister
Copy link

I've submitted a pull request that will make it easier to check which keys were overridden by the user, and which by the factory: #1695. let me know if this works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants