From b6d6c1fded439aae8ef69b993516debaeb5ec12c Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 19 Apr 2020 21:38:21 +0100 Subject: [PATCH] Add announcements to the top of the application template --- app/controllers/application_controller.rb | 5 +++++ app/models/announcement.rb | 10 ++++++++++ app/views/layouts/application.html.haml | 1 + app/views/shared/_announcements.haml | 6 ++++++ 4 files changed, 22 insertions(+) create mode 100644 app/views/shared/_announcements.haml 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