Class: GovukWebBanners::GlobalBanner

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_web_banners/global_banner.rb

Constant Summary collapse

"../../config/govuk_web_banners/global_banners.yml".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_visibleObject (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_dateObject (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_pathsObject (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

#nameObject (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_arrowsObject (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_dateObject (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

#textObject (readonly)

Returns the value of attribute text.



19
20
21
# File 'lib/govuk_web_banners/global_banner.rb', line 19

def text
  @text
end

#titleObject (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_hrefObject (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_bannersObject



9
10
11
# File 'lib/govuk_web_banners/global_banner.rb', line 9

def self.active_banners
  all_banners.select(&:active?)
end

.all_bannersObject



13
14
15
16
17
# File 'lib/govuk_web_banners/global_banner.rb', line 13

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

.for_path(path) ⇒ Object



5
6
7
# File 'lib/govuk_web_banners/global_banner.rb', line 5

def self.for_path(path)
  active_banners.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.

Returns:

  • (Boolean)


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

#versionObject



42
43
44
# File 'lib/govuk_web_banners/global_banner.rb', line 42

def version
  start_date.getutc.to_i
end