Add announcements to the top of the application template

This commit is contained in:
Karina Kwiatek 2020-04-19 21:38:21 +01:00
parent 473f2cdcc5
commit b6d6c1fded
4 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -25,6 +25,7 @@
= csrf_meta_tags
%body#version1
= render 'layouts/header'
= render 'shared/announcements'
= yield
= render 'shared/locales'
- if Rails.env.development?

View File

@ -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