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

Cop idea: prefer symbol proc to if: and unless: filter lambdas #335

Open
vlad-pisanov opened this issue Apr 6, 2023 · 2 comments
Open

Comments

@vlad-pisanov
Copy link

Model hooks and controller filters that receive if: and unless: lambdas can be written more succinctly using symbols if the lambda consists of a single method call. This cop would enforce either style consistently based on configuration.

# bad
before_save    :log, if: -> { name_changed? }
around_destroy :baz, if: -> (user) { user.new? }
after_action   :foo, unless: -> { self.bar? }

# good
before_save    :log, if: :name_changed?
around_destroy :baz, if: :new?
after_action   :foo, unless: :bar?
@koic
Copy link
Member

koic commented Apr 7, 2023

I think this can be proposed to the style guide first. If accepted by the style guide, it could be implemented in RuobCop Rails. So, I'll transfer this issue to the style guide repo.

@koic koic transferred this issue from rubocop/rubocop-rails Apr 7, 2023
@pirj
Copy link
Member

pirj commented Apr 10, 2023

A PR is welcome!

Related: https://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks

I would be interested in how it's used in the wild, say, real-world-rails repo.

With a single predicate - fine, but what is your take on multiple ones, e.g. if: [:fresh?, :exciting?]?

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

No branches or pull requests

3 participants