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

Preserve original callback execution order #313

Merged
merged 1 commit into from
Jul 30, 2020

Conversation

eugeneius
Copy link
Contributor

@eugeneius eugeneius commented Jul 30, 2020

This cop registers an offense when a callback appears after another callback that will be executed later in the record's lifecycle.

The auto-correct logic currently always moves the offending callback to before the first callback in the class. This does eventually produce the correct result, but it's not very efficient, and can reorder callbacks of the same type, which changes the order in which they're executed.

By moving the offending callback above the preceding one, the sort becomes stable and the original callback execution order is preserved.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.


### Changes

* [#312](https://github.com/rubocop-hq/rubocop-rails/pull/312): Mark `Rails/MailerName` as unsafe for auto-correct. ([@eugeneius][])
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 added this in the wrong place in #312. 😩

This cop registers an offense when a callback appears after another
callback that will be executed later in the record's lifecycle.

The auto-correct logic currently always moves the offending callback to
before the first callback in the class. This does eventually produce the
correct result, but it's not very efficient, and can reorder callbacks
of the same type, which changes the order in which they're executed.

By moving the offending callback above the preceding one, the sort
becomes stable and the original callback execution order is preserved.
CALLBACKS_ORDER_MAP = Hash[
CALLBACKS_IN_ORDER.map.with_index { |name, index| [name, index] }
].freeze
CALLBACKS_ORDER_MAP = CALLBACKS_IN_ORDER.each_with_index.to_h.freeze
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change isn't necessary to fix the problem, but I thought it was a nice simplification. 🙂

I'll happily take it out if it's not welcome, though.

Copy link
Member

Choose a reason for hiding this comment

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

Cool 👍

@koic koic merged commit 30d433f into rubocop:master Jul 30, 2020
@koic
Copy link
Member

koic commented Jul 30, 2020

Great! Thank you always 😃

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

Successfully merging this pull request may close these issues.

2 participants