Class: GovukWebBanners::GlobalBanner
- Inherits:
-
Object
- Object
- GovukWebBanners::GlobalBanner
- Defined in:
- lib/govuk_web_banners/global_banner.rb
Constant Summary collapse
- BANNER_CONFIG_FILE =
"../../config/govuk_web_banners/global_banners.yml".freeze
Instance Attribute Summary collapse
-
#always_visible ⇒ Object
readonly
Returns the value of attribute always_visible.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#exclude_paths ⇒ Object
readonly
Returns the value of attribute exclude_paths.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#show_arrows ⇒ Object
readonly
Returns the value of attribute show_arrows.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#title_href ⇒ Object
readonly
Returns the value of attribute title_href.
Class Method Summary collapse
Instance Method Summary collapse
-
#active? ⇒ Boolean
NB: .between? is inclusive.
-
#initialize(attributes:) ⇒ GlobalBanner
constructor
A new instance of GlobalBanner.
- #version ⇒ Object
Constructor Details
#initialize(attributes:) ⇒ GlobalBanner
Returns a new instance of GlobalBanner.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/govuk_web_banners/global_banner.rb', line 21 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" @always_visible = attributes["always_visible"] == "true" @exclude_paths = attributes["exclude_paths"] || [] @exclude_paths << title_href if title_href&.start_with?("/") end |
Instance Attribute Details
#always_visible ⇒ Object (readonly)
Returns the value of attribute always_visible.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def always_visible @always_visible end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def end_date @end_date end |
#exclude_paths ⇒ Object (readonly)
Returns the value of attribute exclude_paths.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def exclude_paths @exclude_paths end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def name @name end |
#show_arrows ⇒ Object (readonly)
Returns the value of attribute show_arrows.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def show_arrows @show_arrows end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def start_date @start_date end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def text @text end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def title @title end |
#title_href ⇒ Object (readonly)
Returns the value of attribute title_href.
19 20 21 |
# File 'lib/govuk_web_banners/global_banner.rb', line 19 def title_href @title_href end |
Class Method Details
.active_banners ⇒ Object
9 10 11 |
# File 'lib/govuk_web_banners/global_banner.rb', line 9 def self. .select(&:active?) end |
.all_banners ⇒ Object
13 14 15 16 17 |
# File 'lib/govuk_web_banners/global_banner.rb', line 13 def self. = Rails.root.join(__dir__, BANNER_CONFIG_FILE) = YAML.load_file() ["global_banners"].map { |attributes| GlobalBanner.new(attributes:) } end |
.for_path(path) ⇒ Object
5 6 7 |
# File 'lib/govuk_web_banners/global_banner.rb', line 5 def self.for_path(path) .reject { |b| b.exclude_paths.include?(path) } end |
Instance Method Details
#active? ⇒ Boolean
NB: .between? is inclusive. To make it exclude the end date, we set the end range as
1 second earlier.
38 39 40 |
# File 'lib/govuk_web_banners/global_banner.rb', line 38 def active? Time.zone.now.between?(start_date, end_date - 1.second) end |
#version ⇒ Object
42 43 44 |
# File 'lib/govuk_web_banners/global_banner.rb', line 42 def version start_date.getutc.to_i end |