Files
nairobi/app/models/banner.rb
Javi Martín 2abe9f27b5 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).
2024-07-05 17:24:56 +02:00

28 lines
915 B
Ruby

class Banner < ApplicationRecord
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
attribute :background_color, default: "#e7f2fc"
attribute :font_color, default: "#222222"
translates :title, touch: true
translates :description, touch: true
include Globalizable
validates_translation :title, presence: true, length: { minimum: 2 }
validates_translation :description, presence: true
validates :target_url, presence: true
validates :post_started_at, presence: true
validates :post_ended_at, presence: true
has_many :sections
has_many :web_sections, through: :sections
scope :with_active, -> { where(post_started_at: ..Date.current, post_ended_at: Date.current..) }
scope :with_inactive, -> { where.not(id: with_active) }
scope :in_section, ->(section_name) do
joins(:web_sections, :sections).where("web_sections.name ilike ?", section_name)
end
end