Files
nairobi/app/models/banner.rb
Javi Martín 0b83be6837 Apply banner default colors to dev seeds
Banners created through the admin form were getting the default color.
However, banners created by other means (like the `db:dev_seed` rake
task) were not getting these default values.

This feature was originally implemented when we were using Rails 4.
With Rails 5, we can provide default values to all new banners and
simplify the code at the same time thanks to its `attribute` method.

Now, when creating a new banner, instead of getting a blank space, we
get an empty line with the banner's default background color, which most
users won't know what it's about until they fill in the banner's title.
So we're not displaying the content of the banner when it's empty,
thanks to the `:empty` CSS pseudoclass.
2020-08-12 17:16:11 +02:00

28 lines
977 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 <= ?", Time.current).where("post_ended_at >= ?", Time.current) }
scope :with_inactive, -> { where("post_started_at > ? or post_ended_at < ?", Time.current, Time.current) }
scope :in_section, ->(section_name) { joins(:web_sections, :sections).where("web_sections.name ilike ?", section_name) }
end