-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from alphagov/global-banner
Add global banner handling
- Loading branch information
Showing
30 changed files
with
726 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<% global_banners = GovukWebBanners::GlobalBanner.for_path(request.path) %> | ||
<% if global_banners.any? %> | ||
<%= render "govuk_publishing_components/components/global_banner", { | ||
title: global_banners.first.title, | ||
title_path: global_banners.first.title_href, | ||
text: global_banners.first.text, | ||
permanent: global_banners.first.permanent, | ||
banner_version: global_banners.first.version, | ||
} %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Check README.md for how to format this file | ||
global_banners: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
require "govuk_publishing_components" | ||
|
||
require "govuk_web_banners/global_banner" | ||
require "govuk_web_banners/emergency_banner" | ||
require "govuk_web_banners/engine" | ||
require "govuk_web_banners/recruitment_banner" | ||
require "govuk_web_banners/version" | ||
|
||
module GovukWebBanners | ||
TIME_ZONE = "London".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module GovukWebBanners | ||
class GlobalBanner | ||
BANNER_CONFIG_FILE = "../../config/govuk_web_banners/global_banners.yml".freeze | ||
|
||
def self.for_path(path) | ||
active_banners.reject { |b| b.exclude_paths.include?(path) } | ||
end | ||
|
||
def self.active_banners | ||
all_banners.select(&:active?) | ||
end | ||
|
||
def self.all_banners | ||
global_banners_file_path = Rails.root.join(__dir__, BANNER_CONFIG_FILE) | ||
global_banners_data = YAML.load_file(global_banners_file_path) | ||
global_banners_data["global_banners"].map { |attributes| GlobalBanner.new(attributes:) } | ||
end | ||
|
||
attr_reader :name, :title, :title_href, :text, :start_date, :end_date, :show_arrows, :permanent, :exclude_paths | ||
|
||
def initialize(attributes:) | ||
@name = attributes["name"] | ||
|
||
@title = attributes["title"] | ||
@title_href = attributes["title_href"] | ||
@text = attributes["text"] | ||
|
||
@start_date = attributes["start_date"] ? ActiveSupport::TimeZone[GovukWebBanners::TIME_ZONE].parse(attributes["start_date"]) : nil | ||
@end_date = attributes["end_date"] ? ActiveSupport::TimeZone[GovukWebBanners::TIME_ZONE].parse(attributes["end_date"]) : Time.now + 10.years | ||
@show_arrows = attributes["show_arrows"] == "true" | ||
@permanent = attributes["permanent"] == "true" | ||
@exclude_paths = attributes["exclude_paths"] || [] | ||
@exclude_paths << title_href if title_href&.start_with?("/") | ||
end | ||
|
||
# NB: .between? is inclusive. To make it exclude the end date, we set the end range as | ||
# 1 second earlier. | ||
def active? | ||
Time.zone.now.between?(start_date, end_date - 1.second) | ||
end | ||
|
||
def version | ||
start_date.getutc.to_i | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.