diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ccc1e3f9..e5c9919b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? before_action :check_locale before_action :banned? + before_action :find_active_announcements # check if user wants to read def check_locale @@ -50,6 +51,10 @@ class ApplicationController < ActionController::Base end end + def find_active_announcements + @active_announcements ||= Announcement.find_active + end + include ApplicationHelper protected diff --git a/app/models/announcement.rb b/app/models/announcement.rb index f2a7684f..3014a117 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -6,6 +6,16 @@ class Announcement < ApplicationRecord validates :link_href, presence: true, if: -> { link_text.present? } validate :starts_at, :validate_date_range + def self.find_active + Rails.cache.fetch "announcement_active", expires_in: 1.minute do + where "starts_at <= :now AND ends_at > :now", now: Time.current + end + end + + def active? + Time.now.utc >= starts_at && Time.now.utc < ends_at + end + def link_present? link_text.present? end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 12f5e08b..d40a87d8 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -25,6 +25,7 @@ = csrf_meta_tags %body#version1 = render 'layouts/header' + = render 'shared/announcements' = yield = render 'shared/locales' - if Rails.env.development? diff --git a/app/views/shared/_announcements.haml b/app/views/shared/_announcements.haml new file mode 100644 index 00000000..6885694d --- /dev/null +++ b/app/views/shared/_announcements.haml @@ -0,0 +1,6 @@ +.container.announcements + - @active_announcements.each do |announcement| + .alert.alert-announcement + %p= announcement.content + - if announcement.link_present? + %a.alert-link{ href: announcement.link_href }= announcement.link_text