diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0bd3bdb0f..57a5a8800 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -25,7 +25,7 @@ def today def display_banner # Cache banners to avoid repeated queries during page render - @banners ||= Banner.published.select("id, content").to_a + @banners ||= Banner.active.select("id, content").to_a return if @banners.empty? safe_content_array = @banners.map { |banner| diff --git a/app/models/banner.rb b/app/models/banner.rb index 592d7ae41..c54e993f8 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -12,4 +12,10 @@ class Banner < ApplicationRecord def name content.truncate(50) end + + scope :active, -> { + where(show: true) + .where("started_at IS NULL OR started_at <= ?", Time.current) + .where("ended_at IS NULL OR ended_at >= ?", Time.current) + } end diff --git a/db/migrate/20260405143000_add_timeto_banner.rb b/db/migrate/20260405143000_add_timeto_banner.rb new file mode 100644 index 000000000..627ea1350 --- /dev/null +++ b/db/migrate/20260405143000_add_timeto_banner.rb @@ -0,0 +1,6 @@ +class AddTimetoBanner < ActiveRecord::Migration[8.1] + def change + add_column :banners, :started_at, :datetime + add_column :banners, :ended_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 3014b674b..583f98727 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_03_12_120000) do +ActiveRecord::Schema[8.1].define(version: 2026_04_05_143000) do create_table "action_text_mentions", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.bigint "action_text_rich_text_id", null: false t.datetime "created_at", null: false @@ -213,8 +213,10 @@ t.text "content" t.datetime "created_at", precision: nil, null: false t.integer "created_by_id" + t.datetime "ended_at" t.boolean "published", default: false, null: false t.boolean "show" + t.datetime "started_at" t.datetime "updated_at", precision: nil, null: false t.integer "updated_by_id" t.index ["created_by_id"], name: "index_banners_on_created_by_id"