Class: GovukPublishingComponents::Presenters::DevolvedNationsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/devolved_nations_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_assigns) ⇒ DevolvedNationsHelper

Returns a new instance of DevolvedNationsHelper.



6
7
8
9
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 6

def initialize(local_assigns)
  @national_applicability = local_assigns[:national_applicability]
  @content_type = local_assigns[:content_type] || "publication"
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



4
5
6
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 4

def content_type
  @content_type
end

#national_applicabilityObject (readonly)

Returns the value of attribute national_applicability.



4
5
6
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 4

def national_applicability
  @national_applicability
end

Instance Method Details

#alternative_content_text(name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 43

def alternative_content_text(name)
  nation = I18n.t("components.devolved_nations.#{name}.start")

  if I18n.exists?("components.devolved_nations.type.#{@content_type}")
    I18n.t("components.devolved_nations.type.#{@content_type}", nation:)
  else
    I18n.t("components.devolved_nations.type.publication", nation:)
  end
end

#applicable_nations_title_text(use_english_translation = nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 11

def applicable_nations_title_text(use_english_translation = nil)
  nation_keys = @national_applicability
              .select { |_, v| v[:applicable] == true }
              .map { |k, _| k }
  nation_keys.each_with_index.map { |k, i| name_for_position(k, i, nation_keys.count, use_english_translation) }.join
end

#ga4_applicable_nations_title_text(remove_connector_word = nil) ⇒ Object



29
30
31
32
33
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 29

def ga4_applicable_nations_title_text(remove_connector_word = nil)
  return applicable_nations_title_text(true).gsub(" and", ",") if remove_connector_word

  applicable_nations_title_text(true)
end

#name_for_position(nation, position, total, use_english_translation) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 18

def name_for_position(nation, position, total, use_english_translation)
  key = if position.zero?
          :start
        elsif position == (total - 1)
          :end
        else
          :middle
        end
  I18n.t("components.devolved_nations.#{nation}.#{key}", locale: use_english_translation ? :en : nil)
end

#nations_with_urlsObject



35
36
37
38
39
40
41
# File 'lib/govuk_publishing_components/presenters/devolved_nations_helper.rb', line 35

def nations_with_urls
  @national_applicability
    .select do |_, v|
      v[:alternative_url]
      .present?
    end
end