Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix issue introduced by YARD's global #log method #1734

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thewatts
Copy link

@thewatts thewatts commented Feb 6, 2025

The YARD gem adds a global #log method, which becomes a problem if a Factory needs to declare a log attribute.

Ex:

FactoryBot.define :event do
  # This would originally call out to YARD's global log method,
  # instead of defining the attribute on the factory
  log
end

This commit adds FactoryBot::DefinitionProxy#log to ensure that if defining a log attribute, it'll correctly get passed to the necessary methods through method_missing, and therefore added as an attribute to the factory.


I realize that this is a bit of a workaround for something that FactoryBot has no control over. And it feels a little off (as other gems could technically pollute the global namespace in the same way). But given the ubiquitous usage of YARD - thought this solution may be an easy fix.

Let me know if you'd prefer me to reach out to the YARD team to see if they can make an adjustment on their end.

Additionally - this was a gnarly thing to track down. We've been using the Lookbook gem, and adding that gem caused our specs to fail, because the log attribute in on the record was always being assigned nil in the Factory. It took quite a bit of digging to find that the issue was actually YARD, and that it was their log method.

We have a workaround in place - but certainly didn't want someone else to have to wrestle through figuring out what happened.

At the very least - this PR can serve as a discoverable piece of information.


Thanks for all your hard work on this amazing gem/resource! 👏

[The YARD gem adds a global `#log`
method](https://github.com/lsegal/yard/blob/efb33b5411f54ae491c10ce5cd227fa49b985fde/lib/yard/globals.rb#L20-L22), which becomes a problem if a
Factory needs to declare a `log` attribute.

Ex:

```ruby
FactoryBot.define :event do
  # This would originally call out to YARD's global log method,
  # instead of defining the attribute on the factory
  log
end
```

This commit adds `FactoryBot::DefinitionProxy#log` to ensure that if
defining a `log` attribute, it'll correctly get passed to the necessary
methods through `method_missing`, and therefore added as an attribute to
the factory.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant