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 "Prefer all_(day|week|month|quarter|year) over range of date/time" rule #319

Merged
merged 1 commit into from
Jul 1, 2022

Conversation

koic
Copy link
Member

@koic koic commented Jun 25, 2022

Follow up rubocop/rubocop-rails#730.

This PR adds new "Prefer all_(day|week|month|quarter|year) over range of date/time" rule.

# bad
date.beginning_of_day..date.end_of_day
date.beginning_of_week..date.end_of_week
date.beginning_of_month..date.end_of_month
date.beginning_of_quarter..date.end_of_quarter
date.beginning_of_year..date.end_of_year

# good
date.all_day
date.all_week
date.all_month
date.all_quarter
date.all_year

…e/time" rule

Follow up rubocop/rubocop-rails#730.

This PR adds new "Prefer `all_(day|week|month|quarter|year)` over range of date/time" rule.

```ruby
# bad
date.beginning_of_day..date.end_of_day
date.beginning_of_week..date.end_of_week
date.beginning_of_month..date.end_of_month
date.beginning_of_quarter..date.end_of_quarter
date.beginning_of_year..date.end_of_year

# good
date.all_day
date.all_week
date.all_month
date.all_quarter
date.all_year
```
@pirj
Copy link
Member

pirj commented Jun 27, 2022

I ran rg --no-ignore --hidden "\.all_(day|week|year|quarter|month){1}" over real-world-rspec and found quite a few usages:

canvas-lms/lib/dates_overridable.rb
226:      hash[:all_day] = assignment.all_day
227:      hash[:all_day_date] = assignment.all_day_date # ?????

# a dozen more usages in the same repo, but there's `def all_day`, so it's not the Rails one


loomio/vue/src/components/poll/meeting/form.vue
19:          {text: @$t('common.all_day'), value: null}

mastodon/app/helpers/admin/announcements_helper.rb
5:    if announcement.all_day?

mastodon/app/models/announcement.rb
89:    self.all_day = false if starts_at.blank? || ends_at.blank?


# Only two real usages:

gitlabhq/spec/models/user_custom_attribute_spec.rb
31:      subject { UserCustomAttribute.by_updated_at(Date.today.all_day) }

gitlabhq/app/models/user_custom_attribute.rb
36:      by_key('blocked_at').by_updated_at(Date.yesterday.all_day)

I love the API, really. But should we recommend something that is not widespread? Maybe start with the cop and see if this good practice spreads?

@koic
Copy link
Member Author

koic commented Jun 28, 2022

These are APIs that are fully compatible with bad cases and are a smart solution to avoid redundant writing.
So it can start with cop, but I think it's useful to mention it in the style guide as well. While it may be less known than I think, but it's useful to know this API.

Especially seen in the totaling up of the data of Rails applications, rather this may not be seen much in OSS :-)

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Agree with your points.
Thanks!

README.adoc Show resolved Hide resolved
@koic koic merged commit 3d59910 into rubocop:master Jul 1, 2022
@koic koic deleted the add_new_date_time_range_rule branch July 1, 2022 23:11
koic added a commit to rubocop/rubocop-rails that referenced this pull request Jul 2, 2022
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