Add announcements to the top of the application template
This commit is contained in:
parent
473f2cdcc5
commit
b6d6c1fded
|
@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
|
||||||
before_action :configure_permitted_parameters, if: :devise_controller?
|
before_action :configure_permitted_parameters, if: :devise_controller?
|
||||||
before_action :check_locale
|
before_action :check_locale
|
||||||
before_action :banned?
|
before_action :banned?
|
||||||
|
before_action :find_active_announcements
|
||||||
|
|
||||||
# check if user wants to read
|
# check if user wants to read
|
||||||
def check_locale
|
def check_locale
|
||||||
|
@ -50,6 +51,10 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_active_announcements
|
||||||
|
@active_announcements ||= Announcement.find_active
|
||||||
|
end
|
||||||
|
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -6,6 +6,16 @@ class Announcement < ApplicationRecord
|
||||||
validates :link_href, presence: true, if: -> { link_text.present? }
|
validates :link_href, presence: true, if: -> { link_text.present? }
|
||||||
validate :starts_at, :validate_date_range
|
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?
|
def link_present?
|
||||||
link_text.present?
|
link_text.present?
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
= csrf_meta_tags
|
= csrf_meta_tags
|
||||||
%body#version1
|
%body#version1
|
||||||
= render 'layouts/header'
|
= render 'layouts/header'
|
||||||
|
= render 'shared/announcements'
|
||||||
= yield
|
= yield
|
||||||
= render 'shared/locales'
|
= render 'shared/locales'
|
||||||
- if Rails.env.development?
|
- if Rails.env.development?
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue