Use ranges instead of comparisons in SQL queries
These cases aren't covered by the `Rails/WhereRange` rubocop rule, but
IMHO using ranges makes them more consistent. Besides, they generate SQL
which is more consistent with what Rails usually generates. For example,
`Poll.where("starts_at <= :time and ends_at >= :time", time:
Time.current)` generates:
```
SELECT \"polls\".\"id\", (...) WHERE \"polls\".\"hidden_at\" IS NULL AND
(starts_at <= '2024-07-(...)' and ends_at >= '2024-07-(...)')
```
And `Poll.where(starts_at: ..Time.current, ends_at: Time.current..)`
generates:
```
SELECT \"polls\".\"id\", (...) WHERE \"polls\".\"hidden_at\" IS NULL AND
\"polls\".\"starts_at\" <= '2024-07-(...)' AND \"polls\".\"ends_at\" >=
'2024-07-(...)'"
```
Note that the `not_archived` scope in proposals slightly changes, since
we were using `>` and now we use the equivalent of `>=`. However, since
the `created_at` field is a time, this will only mean that a proposal
will be archived about one microsecond later.
For consistency, we're also changing the `archived` scope, so a proposal
is never archived and not archived at the same time (not even for a
microsecond).
This commit is contained in:
@@ -19,7 +19,7 @@ class Banner < ApplicationRecord
|
|||||||
has_many :sections
|
has_many :sections
|
||||||
has_many :web_sections, through: :sections
|
has_many :web_sections, through: :sections
|
||||||
|
|
||||||
scope :with_active, -> { where("post_started_at <= :date and post_ended_at >= :date", date: Date.current) }
|
scope :with_active, -> { where(post_started_at: ..Date.current, post_ended_at: Date.current..) }
|
||||||
scope :with_inactive, -> { where.not(id: with_active) }
|
scope :with_inactive, -> { where.not(id: with_active) }
|
||||||
scope :in_section, ->(section_name) do
|
scope :in_section, ->(section_name) do
|
||||||
joins(:web_sections, :sections).where("web_sections.name ilike ?", section_name)
|
joins(:web_sections, :sections).where("web_sections.name ilike ?", section_name)
|
||||||
|
|||||||
@@ -78,8 +78,8 @@ class Budget
|
|||||||
scope :without_valuator, -> { without_valuator_group.where(valuator_assignments_count: 0) }
|
scope :without_valuator, -> { without_valuator_group.where(valuator_assignments_count: 0) }
|
||||||
scope :under_valuation, -> { valuation_open.valuating.with_admin }
|
scope :under_valuation, -> { valuation_open.valuating.with_admin }
|
||||||
scope :managed, -> { valuation_open.where(valuator_assignments_count: 0).with_admin }
|
scope :managed, -> { valuation_open.where(valuator_assignments_count: 0).with_admin }
|
||||||
scope :with_valuator_assignments, -> { where("valuator_assignments_count > 0") }
|
scope :with_valuator_assignments, -> { where(valuator_assignments_count: 1..) }
|
||||||
scope :with_group_assignments, -> { where("valuator_group_assignments_count > 0") }
|
scope :with_group_assignments, -> { where(valuator_group_assignments_count: 1..) }
|
||||||
scope :with_valuation_assignments, -> { with_valuator_assignments.or(with_group_assignments) }
|
scope :with_valuation_assignments, -> { with_valuator_assignments.or(with_group_assignments) }
|
||||||
scope :valuating, -> { valuation_open.with_valuation_assignments }
|
scope :valuating, -> { valuation_open.with_valuation_assignments }
|
||||||
scope :visible_to_valuators, -> { where(visible_to_valuators: true) }
|
scope :visible_to_valuators, -> { where(visible_to_valuators: true) }
|
||||||
@@ -90,7 +90,7 @@ class Budget
|
|||||||
scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") }
|
scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") }
|
||||||
scope :undecided, -> { where(feasibility: "undecided") }
|
scope :undecided, -> { where(feasibility: "undecided") }
|
||||||
|
|
||||||
scope :with_supports, -> { where("cached_votes_up > 0") }
|
scope :with_supports, -> { where(cached_votes_up: 1..) }
|
||||||
scope :selected, -> { feasible.where(selected: true) }
|
scope :selected, -> { feasible.where(selected: true) }
|
||||||
scope :compatible, -> { where(incompatible: false) }
|
scope :compatible, -> { where(incompatible: false) }
|
||||||
scope :incompatible, -> { where(incompatible: true) }
|
scope :incompatible, -> { where(incompatible: true) }
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ class Budget::Stats
|
|||||||
end
|
end
|
||||||
|
|
||||||
def balloters
|
def balloters
|
||||||
@balloters ||= budget.ballots.where("ballot_lines_count > ?", 0).distinct.pluck(:user_id).compact
|
@balloters ||= budget.ballots.where(ballot_lines_count: 1..).distinct.pluck(:user_id).compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def poll_ballot_voters
|
def poll_ballot_voters
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module Flaggable
|
|||||||
|
|
||||||
included do
|
included do
|
||||||
has_many :flags, as: :flaggable, inverse_of: :flaggable
|
has_many :flags, as: :flaggable, inverse_of: :flaggable
|
||||||
scope :flagged, -> { where("flags_count > 0") }
|
scope :flagged, -> { where(flags_count: 1..) }
|
||||||
scope :pending_flag_review, -> { flagged.where(ignored_flag_at: nil, hidden_at: nil) }
|
scope :pending_flag_review, -> { flagged.where(ignored_flag_at: nil, hidden_at: nil) }
|
||||||
scope :with_ignored_flag, -> { flagged.where.not(ignored_flag_at: nil).where(hidden_at: nil) }
|
scope :with_ignored_flag, -> { flagged.where.not(ignored_flag_at: nil).where(hidden_at: nil) }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ module Verification
|
|||||||
residence_or_phone_unverified.where(verified_at: nil, level_two_verified_at: nil)
|
residence_or_phone_unverified.where(verified_at: nil, level_two_verified_at: nil)
|
||||||
end
|
end
|
||||||
scope :incomplete_verification, -> do
|
scope :incomplete_verification, -> do
|
||||||
residence_unverified.where("failed_census_calls_count > ?", 0)
|
residence_unverified.where(failed_census_calls_count: 1..)
|
||||||
.or(residence_verified.phone_not_fully_confirmed)
|
.or(residence_verified.phone_not_fully_confirmed)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -59,8 +59,8 @@ class Legislation::Process < ApplicationRecord
|
|||||||
validates :font_color, format: { allow_blank: true, with: ->(*) { CSS_HEX_COLOR }}
|
validates :font_color, format: { allow_blank: true, with: ->(*) { CSS_HEX_COLOR }}
|
||||||
|
|
||||||
class << self; undef :open; end
|
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 :active, -> { where(end_date: Date.current..) }
|
||||||
|
scope :open, -> { active.where(start_date: ..Date.current) }
|
||||||
scope :past, -> { where(end_date: ...Date.current) }
|
scope :past, -> { where(end_date: ...Date.current) }
|
||||||
|
|
||||||
scope :published, -> { where(published: true) }
|
scope :published, -> { where(published: true) }
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class Poll < ApplicationRecord
|
|||||||
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
|
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
|
||||||
|
|
||||||
scope :for, ->(element) { where(related: element) }
|
scope :for, ->(element) { where(related: element) }
|
||||||
scope :current, -> { where("starts_at <= :time and ends_at >= :time", time: Time.current) }
|
scope :current, -> { where(starts_at: ..Time.current, ends_at: 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 :recounting, -> { where(ends_at: (RECOUNT_DURATION.ago)...Time.current) }
|
||||||
scope :published, -> { where(published: true) }
|
scope :published, -> { where(published: true) }
|
||||||
|
|||||||
@@ -75,8 +75,8 @@ class Proposal < ApplicationRecord
|
|||||||
scope :sort_by_archival_date, -> { archived.sort_by_confidence_score }
|
scope :sort_by_archival_date, -> { archived.sort_by_confidence_score }
|
||||||
scope :sort_by_recommendations, -> { order(cached_votes_up: :desc) }
|
scope :sort_by_recommendations, -> { order(cached_votes_up: :desc) }
|
||||||
|
|
||||||
scope :archived, -> { where(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 :not_archived, -> { where(created_at: Setting.archived_proposals_date_limit..) }
|
||||||
scope :last_week, -> { where(created_at: 7.days.ago..) }
|
scope :last_week, -> { where(created_at: 7.days.ago..) }
|
||||||
scope :retired, -> { where.not(retired_at: nil) }
|
scope :retired, -> { where.not(retired_at: nil) }
|
||||||
scope :not_retired, -> { where(retired_at: nil) }
|
scope :not_retired, -> { where(retired_at: nil) }
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ class User < ApplicationRecord
|
|||||||
scope :moderators, -> { joins(:moderator) }
|
scope :moderators, -> { joins(:moderator) }
|
||||||
scope :organizations, -> { joins(:organization) }
|
scope :organizations, -> { joins(:organization) }
|
||||||
scope :sdg_managers, -> { joins(:sdg_manager) }
|
scope :sdg_managers, -> { joins(:sdg_manager) }
|
||||||
scope :officials, -> { where("official_level > 0") }
|
scope :officials, -> { where(official_level: 1..) }
|
||||||
scope :male, -> { where(gender: "male") }
|
scope :male, -> { where(gender: "male") }
|
||||||
scope :female, -> { where(gender: "female") }
|
scope :female, -> { where(gender: "female") }
|
||||||
scope :newsletter, -> { where(newsletter: true) }
|
scope :newsletter, -> { where(newsletter: true) }
|
||||||
|
|||||||
Reference in New Issue
Block a user