Class: GovukWebBanners::RecruitmentBanner

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

Constant Summary collapse

"../../config/govuk_web_banners/recruitment_banners.yml".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes:) ⇒ RecruitmentBanner

Returns a new instance of RecruitmentBanner.



23
24
25
26
27
28
29
30
31
32
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 23

def initialize(attributes:)
  @name = attributes["name"]
  @suggestion_text = attributes["suggestion_text"]
  @suggestion_link_text = attributes["suggestion_link_text"]
  @survey_url = attributes["survey_url"]
  @page_paths = attributes["page_paths"]
  @start_date = attributes["start_date"] ? ActiveSupport::TimeZone[GovukWebBanners::TIME_ZONE].parse(attributes["start_date"]) : Time.at(0)
  @end_date = attributes["end_date"] ? ActiveSupport::TimeZone[GovukWebBanners::TIME_ZONE].parse(attributes["end_date"]) : Time.now + 10.years
  @image = attributes["image"]
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def end_date
  @end_date
end

#imageObject (readonly)

Returns the value of attribute image.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def image
  @image
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def name
  @name
end

#page_pathsObject (readonly)

Returns the value of attribute page_paths.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def page_paths
  @page_paths
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def start_date
  @start_date
end

Returns the value of attribute suggestion_link_text.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def suggestion_link_text
  @suggestion_link_text
end

#suggestion_textObject (readonly)

Returns the value of attribute suggestion_text.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def suggestion_text
  @suggestion_text
end

#survey_urlObject (readonly)

Returns the value of attribute survey_url.



21
22
23
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 21

def survey_url
  @survey_url
end

Class Method Details

.active_bannersObject



11
12
13
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 11

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

.all_bannersObject



15
16
17
18
19
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 15

def self.all_banners
  recruitment_banners_urls_file_path = Rails.root.join(__dir__, BANNER_CONFIG_FILE)
  recruitment_banners_data = YAML.load_file(recruitment_banners_urls_file_path)
  recruitment_banners_data["banners"].map { |attributes| RecruitmentBanner.new(attributes:) }
end

.for_path(path) ⇒ Object



5
6
7
8
9
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 5

def self.for_path(path)
  active_banners.find do |banner|
    return banner if banner.page_paths.include?(path)
  end
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)


36
37
38
# File 'lib/govuk_web_banners/recruitment_banner.rb', line 36

def active?
  Time.zone.now.between?(start_date, end_date - 1.second)
end