Add and apply Rails/WhereRange rubocop rule

This rule was added in rubocop-rails 2.25.0. Applying it allows us to
simplify the code a little bit. For example, now there's no need to
specify the `proposals` table in proposal scopes, which was actually
causing a bug in the `Legislation::Proposal` model, which was using the
`proposals` table instead of the `legislation_proposals` table (but,
since we don't use this scope, it didn't affect the application).
This commit is contained in:
Javi Martín
2024-07-02 17:00:29 +02:00
parent 8e2bd12c7e
commit fb0c087f95
12 changed files with 28 additions and 27 deletions

View File

@@ -517,6 +517,9 @@ Rails/WhereNot:
Rails/WhereNotWithMultipleConditions:
Enabled: true
Rails/WhereRange:
Enabled: true
RSpec/AroundBlock:
Enabled: true

View File

@@ -4,8 +4,8 @@ module ScoreCalculator
period = [1, [max_period, resource_age(resource)].min].max
votes_total = resource.votes_for.where("created_at >= ?", period.days.ago).count
votes_up = resource.get_upvotes.where("created_at >= ?", period.days.ago).count
votes_total = resource.votes_for.where(created_at: period.days.ago..).count
votes_up = resource.get_upvotes.where(created_at: period.days.ago..).count
votes_down = votes_total - votes_up
votes_score = votes_up - votes_down

View File

@@ -96,7 +96,7 @@ class Budget
scope :incompatible, -> { where(incompatible: true) }
scope :winners, -> { selected.compatible.where(winner: true) }
scope :unselected, -> { not_unfeasible.where(selected: false) }
scope :last_week, -> { where("created_at >= ?", 7.days.ago) }
scope :last_week, -> { where(created_at: 7.days.ago..) }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :sort_by_created_at, -> { reorder(created_at: :desc) }
scope :sort_by_ballot_lines, -> { order(:"budget_ballot_lines.created_at") }

View File

@@ -39,8 +39,8 @@ class Dashboard::Action < ApplicationRecord
def self.active_for(proposal)
published_at = proposal.published_at&.to_date || Date.current
active.where("required_supports <= ?", proposal.cached_votes_up)
.where("day_offset <= ?", (Date.current - published_at).to_i)
active.where(required_supports: ..proposal.cached_votes_up)
.where(day_offset: ..(Date.current - published_at).to_i)
.by_proposal(proposal)
end
@@ -105,12 +105,12 @@ class Dashboard::Action < ApplicationRecord
def self.calculate_actions(proposal_votes, day_offset, proposal)
Dashboard::Action.active
.where("required_supports <= ?", proposal_votes)
.where("day_offset <= ?", day_offset)
.where(required_supports: ..proposal_votes)
.where(day_offset: ..day_offset)
.by_published_proposal(proposal.published?)
end
def self.calculate_votes(proposal, date)
Vote.where(votable: proposal).where("created_at <= ?", date).count
Vote.where(votable: proposal).where(created_at: ..date).count
end
end

View File

@@ -42,7 +42,7 @@ class Debate < ApplicationRecord
scope :sort_by_relevance, -> { all }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :sort_by_recommendations, -> { order(cached_votes_total: :desc) }
scope :last_week, -> { where("created_at >= ?", 7.days.ago) }
scope :last_week, -> { where(created_at: 7.days.ago..) }
scope :featured, -> { where.not(featured_at: nil) }
scope :public_for_api, -> { all }

View File

@@ -60,8 +60,8 @@ class Legislation::Process < ApplicationRecord
class << self; undef :open; end
scope :open, -> { where("start_date <= ? and end_date >= ?", Date.current, Date.current) }
scope :active, -> { where("end_date >= ?", Date.current) }
scope :past, -> { where("end_date < ?", Date.current) }
scope :active, -> { where(end_date: Date.current..) }
scope :past, -> { where(end_date: ...Date.current) }
scope :published, -> { where(published: true) }

View File

@@ -48,7 +48,7 @@ class Legislation::Proposal < ApplicationRecord
scope :sort_by_id, -> { reorder(id: :asc) }
scope :sort_by_supports, -> { reorder(cached_votes_score: :desc) }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago) }
scope :last_week, -> { where(created_at: 7.days.ago..) }
scope :selected, -> { where(selected: true) }
scope :winners, -> { selected.sort_by_confidence_score }

View File

@@ -14,7 +14,7 @@ class Milestone < ApplicationRecord
validates_translation :description, presence: true, unless: -> { status_id.present? }
scope :order_by_publication_date, -> { order(publication_date: :asc, created_at: :asc) }
scope :published, -> { where("publication_date <= ?", Date.current.end_of_day) }
scope :published, -> { where(publication_date: ..Date.current.end_of_day) }
scope :with_status, -> { where.not(status_id: nil) }
def self.title_max_length

View File

@@ -44,7 +44,7 @@ class Poll < ApplicationRecord
scope :for, ->(element) { where(related: element) }
scope :current, -> { where("starts_at <= :time and ends_at >= :time", time: Time.current) }
scope :expired, -> { where("ends_at < ?", Time.current) }
scope :expired, -> { where(ends_at: ...Time.current) }
scope :recounting, -> { where(ends_at: (RECOUNT_DURATION.ago)...Time.current) }
scope :published, -> { where(published: true) }
scope :by_geozone_id, ->(geozone_id) { where(geozones: { id: geozone_id }.joins(:geozones)) }

View File

@@ -75,13 +75,13 @@ class Proposal < ApplicationRecord
scope :sort_by_archival_date, -> { archived.sort_by_confidence_score }
scope :sort_by_recommendations, -> { order(cached_votes_up: :desc) }
scope :archived, -> { where("proposals.created_at <= ?", Setting.archived_proposals_date_limit) }
scope :archived, -> { where(created_at: ..Setting.archived_proposals_date_limit) }
scope :not_archived, -> { where("proposals.created_at > ?", Setting.archived_proposals_date_limit) }
scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago) }
scope :last_week, -> { where(created_at: 7.days.ago..) }
scope :retired, -> { where.not(retired_at: nil) }
scope :not_retired, -> { where(retired_at: nil) }
scope :successful, -> { where("cached_votes_up >= ?", Proposal.votes_needed_for_success) }
scope :unsuccessful, -> { where("cached_votes_up < ?", Proposal.votes_needed_for_success) }
scope :successful, -> { where(cached_votes_up: Proposal.votes_needed_for_success..) }
scope :unsuccessful, -> { where(cached_votes_up: ...Proposal.votes_needed_for_success) }
scope :public_for_api, -> { all }
scope :selected, -> { where(selected: true) }
scope :not_selected, -> { where(selected: false) }

View File

@@ -233,13 +233,13 @@ class User < ApplicationRecord
def full_restore
ActiveRecord::Base.transaction do
Debate.restore_all debates.where("hidden_at >= ?", hidden_at)
Comment.restore_all comments.where("hidden_at >= ?", hidden_at)
Legislation::Proposal.restore_all legislation_proposals.only_hidden.where("hidden_at >= ?", hidden_at)
Proposal.restore_all proposals.where("hidden_at >= ?", hidden_at)
Budget::Investment.restore_all budget_investments.where("hidden_at >= ?", hidden_at)
Debate.restore_all debates.where(hidden_at: hidden_at..)
Comment.restore_all comments.where(hidden_at: hidden_at..)
Legislation::Proposal.restore_all legislation_proposals.only_hidden.where(hidden_at: hidden_at..)
Proposal.restore_all proposals.where(hidden_at: hidden_at..)
Budget::Investment.restore_all budget_investments.where(hidden_at: hidden_at..)
ProposalNotification.restore_all(
ProposalNotification.only_hidden.where("hidden_at >= ?", hidden_at).where(author_id: id)
ProposalNotification.only_hidden.where(hidden_at: hidden_at..).where(author_id: id)
)
restore

View File

@@ -2,9 +2,7 @@ namespace :files do
desc "Removes cached attachments which weren't deleted for some reason"
task remove_old_cached_attachments: :environment do
Tenant.run_on_each do
ActiveStorage::Blob.unattached
.where("active_storage_blobs.created_at <= ?", 1.day.ago)
.find_each(&:purge_later)
ActiveStorage::Blob.unattached.where(created_at: ..1.day.ago).find_each(&:purge_later)
end
end
end