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

Use match? in FactoryBot#aliases_for to save allocations #1457

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/factory_bot/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class << self

def self.aliases_for(attribute)
aliases.map { |(pattern, replace)|
if pattern.match(attribute.to_s)
Copy link

Choose a reason for hiding this comment

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

Why are we removing to_s? Do we know why we were calling it here in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about this part either, I tried it with symbol and string attributes and it both worked. But maybe there could be scenarios where attribute is another ruby object, that implements to_s? Don't know enough details about factory_bot there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Tracing back through the callers it seems like attribute here is always a symbol, and Regexp#match seems to work fine with a symbol (maybe that wasn't always true?), so change seems good to me.

if pattern.match?(attribute)
attribute.to_s.sub(pattern, replace).to_sym
end
}.compact << attribute
Expand Down