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

Add new Rails/RelativeDateGrammar cop #1106

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

Conversation

aeroastro
Copy link

@aeroastro aeroastro commented Sep 6, 2023

This PR adds new Rails/RelativeDateGrammar cop.

It checks whether the word orders of relative dates
are grammatically easy to understand.
This check includes detecting undefined methods on Date(Time) objects.

# bad
tomorrow = Time.current.since(1.day)

# good
tomorrow = 1.day.since(Time.current)

Although the resulting value is the same,

  • 1.day.since(Time.current) is grammatically easier to understand.
  • 1.day.since(Time.current) is a natural extension of 1.day.since.

As the receiver and the argument get more complicated, this difference
becomes larger. This cop forces more understandable style, and thus,
assists easier development.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@aeroastro aeroastro force-pushed the new_add_rails_relative_date_grammar_cop branch from 3d013be to 1557770 Compare September 6, 2023 01:11
@aeroastro
Copy link
Author

The corresponding update to Rails Style Guide is the following PR.
rubocop/rails-style-guide#345

As the guide consistently uses instances of ActiveSupport::Duration as receivers,
IMHO, this is nothing more than verbalizing an implicit convention.

@aeroastro aeroastro force-pushed the new_add_rails_relative_date_grammar_cop branch from 1557770 to 8e0dec9 Compare September 7, 2023 02:30
This PR adds new `Rails/RelativeDateGrammar` cop.

It checks whether the word orders of relative dates
are grammatically easy to understand.
This check includes detecting undefined methods on Date(Time) objects.

```ruby
# bad
tomorrow = Time.current.since(1.day)

# good
tomorrow = 1.day.since(Time.current)
```
@aeroastro aeroastro force-pushed the new_add_rails_relative_date_grammar_cop branch from 8e0dec9 to 873ad73 Compare September 7, 2023 02:59
@aeroastro
Copy link
Author

Reflecting review at Rails Style Guide, I have added the following changes.

  • Use Use instead of Prefer to enforce the cop rule
  • Use ago and since, which are actually implemented methods in (Date)Time, in the test.
  • Improve description for the Cop to include detection of undefined methods.

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.

None yet

1 participant