Class: GovukPublishingComponents::Presenters::SinglePageNotificationButtonHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_assigns) ⇒ SinglePageNotificationButtonHelper

Returns a new instance of SinglePageNotificationButtonHelper.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 6

def initialize(local_assigns)
  @local_assigns = local_assigns
  @data_attributes = @local_assigns[:data_attributes] || {}
  @js_enhancement = @local_assigns[:js_enhancement] || false
  @already_subscribed = @local_assigns[:already_subscribed] || false
  @base_path = @local_assigns[:base_path] || nil
  @button_location = button_location_is_valid? ? @local_assigns[:button_location] : nil
  @button_type = @local_assigns[:already_subscribed] ? "Unsubscribe" : "Subscribe"
  @classes = %w[gem-c-single-page-notification-button]
  @classes << "js-personalisation-enhancement" if js_enhancement
  @button_text_subscribe = custom_button_text_is_valid? ? custom_subscribe_text : default_subscribe_text
  @button_text_unsubscribe = custom_button_text_is_valid? ? custom_unsubscribe_text : default_unsubscribe_text
  @skip_account = @local_assigns[:skip_account] || nil
end

Instance Attribute Details

#already_subscribedObject (readonly)

Returns the value of attribute already_subscribed.



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

def already_subscribed
  @already_subscribed
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#button_locationObject (readonly)

Returns the value of attribute button_location.



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

def button_location
  @button_location
end

#button_typeObject (readonly)

Returns the value of attribute button_type.



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

def button_type
  @button_type
end

#classesObject (readonly)

Returns the value of attribute classes.



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

def classes
  @classes
end

#data_attributesObject (readonly)

Returns the value of attribute data_attributes.



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

def data_attributes
  @data_attributes
end

#js_enhancementObject (readonly)

Returns the value of attribute js_enhancement.



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

def js_enhancement
  @js_enhancement
end

#skip_accountObject (readonly)

Returns the value of attribute skip_account.



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

def 
  @skip_account
end

Instance Method Details

#button_location_is_valid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 35

def button_location_is_valid?
  %w[bottom top].include? @local_assigns[:button_location]
end

#button_textObject



39
40
41
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 39

def button_text
  @already_subscribed ? @button_text_unsubscribe : @button_text_subscribe
end

#custom_button_text_is_valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 43

def custom_button_text_is_valid?
  custom_subscribe_text.present? && custom_unsubscribe_text.present?
end

#custom_subscribe_textObject



47
48
49
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 47

def custom_subscribe_text
  @local_assigns.dig(:button_text, :subscribe)
end

#custom_unsubscribe_textObject



51
52
53
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 51

def custom_unsubscribe_text
  @local_assigns.dig(:button_text, :unsubscribe)
end

#dataObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 21

def data
  @data_attributes[:track_label] = base_path
  # data-action for tracking should have the format of e.g. "Unsubscribe-button-top", or "Subscribe-button-bottom"
  # when button_location is not present data-action will fall back to "Unsubscribe-button"/"Subscribe-button"
  @data_attributes[:track_action] = [button_type, "button", button_location].compact.join("-")
  @data_attributes[:module] = "single-page-notification-button" if js_enhancement
  @data_attributes[:track_category] = "Single-page-notification-button"
  # This attribute is passed through to the personalisation API to ensure when a new button is returned from the API, it has the same button_location
  @data_attributes[:button_location] = button_location
  @data_attributes[:button_text_subscribe] = @button_text_subscribe
  @data_attributes[:button_text_unsubscribe] = @button_text_unsubscribe
  @data_attributes
end

#default_subscribe_textObject



55
56
57
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 55

def default_subscribe_text
  I18n.t("components.single_page_notification_button.subscribe_text")
end

#default_unsubscribe_textObject



59
60
61
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 59

def default_unsubscribe_text
  I18n.t("components.single_page_notification_button.unsubscribe_text")
end

#email_alert_frontend_endpoint_enforce_accountObject



71
72
73
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 71

def 
  "/email/subscriptions/single-page/new"
end

#email_alert_frontend_endpoint_no_accountObject



75
76
77
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 75

def 
  "/email-signup"
end

#form_actionObject



63
64
65
66
67
68
69
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 63

def form_action
  if skip_the_gov_uk_account?
    
  else
    
  end
end

#skip_account_paramObject



79
80
81
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 79

def 
  "single_page_subscription"
end

#skip_the_gov_uk_account?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb', line 83

def skip_the_gov_uk_account?
  @skip_account == "true"
end