Skip to content

Commit

Permalink
Move code from component into a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed May 10, 2018
1 parent a2a0684 commit 2d2e237
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,57 +1,40 @@
<%
email_signup_link ||= false
email_signup_link_text ||= t("govuk_component.subscription_links.email_signup_link_text", default: "Get email alerts")
email_signup_link_data_attributes ||= false

feed_link ||= false
feed_link_text ||= t("govuk_component.subscription_links.feed_link_text", default: "Subscribe to feed")
feed_link_box_value ||= false
feed_link_data_attributes ||= {}

tracking_is_present = true if email_signup_link_data_attributes || feed_link_data_attributes.any?

brand ||= false
brand_helper = GovukPublishingComponents::Presenters::BrandHelper.new(brand)

if feed_link_box_value
feed_link = "#"
feed_box_id = SecureRandom.hex(2)
feed_link_data_attributes[:controls] = "feed-reader-#{feed_box_id}"
feed_link_data_attributes[:expanded] = "false"
end
sl_helper = GovukPublishingComponents::Presenters::SubscriptionLinksHelper.new(local_assigns)
%>
<% if email_signup_link || feed_link || feed_link_box_value %>
<% if sl_helper.component_data_is_valid? %>
<section
class="gem-c-subscription-links <%= brand_helper.get_brand %>"
<%= "data-module=gem-toggle" if feed_link_box_value %>
<%= "data-module=gem-toggle" if sl_helper.feed_link_box_value %>
>
<h2 class="visuallyhidden"><%= t("govuk_component.subscription_links.subscriptions", default: "Subscriptions") %></h2>
<ul
class="gem-c-subscription-links__list"
<%= "data-module=track-click" if tracking_is_present %>
<%= "data-module=track-click" if sl_helper.tracking_is_present? %>
>
<% if email_signup_link.present? %>
<% if sl_helper.email_signup_link.present? %>
<li class="gem-c-subscription-links__list-item">
<%= link_to email_signup_link_text, email_signup_link,
<%= link_to sl_helper.email_signup_link_text, sl_helper.email_signup_link,
class: "gem-c-subscription-links__link gem-c-subscription-links__link--email-alerts #{brand_helper.get_brand_element("color")}",
data: (email_signup_link_data_attributes if email_signup_link_data_attributes)
data: sl_helper.email_signup_link_data_attributes
%>
</li>
<% end %>

<% if feed_link_box_value || feed_link.present? %>
<% if sl_helper.feed_link_box_value || sl_helper.feed_link %>
<li class="gem-c-subscription-links__list-item">
<%= link_to feed_link_text, feed_link,
<%= link_to sl_helper.feed_link_text, sl_helper.feed_link,
class: "gem-c-subscription-links__link gem-c-subscription-links__link--feed #{brand_helper.get_brand_element("color")}",
data: (feed_link_data_attributes if feed_link_data_attributes)
data: sl_helper.feed_link_data_attributes
%>
</li>
<% end %>
</ul>

<% if feed_link_box_value %>
<div class="gem-c-subscription-links__feed-box js-hidden" id="feed-reader-<%= feed_box_id %>">
<p class="gem-c-subscription-links__feed-description js-hidden"><%= feed_link_text %></p>
<% if sl_helper.feed_link_box_value %>
<div class="gem-c-subscription-links__feed-box js-hidden" id="<%= sl_helper.feed_box_id %>">
<p class="gem-c-subscription-links__feed-description js-hidden"><%= sl_helper.feed_link_text %></p>
<%= render "govuk_publishing_components/components/input", {
label: {
text: "Copy and paste this URL into your feed reader"
Expand Down
1 change: 1 addition & 0 deletions lib/govuk_publishing_components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "govuk_publishing_components/presenters/rummager_taxonomy_sidebar_links"
require "govuk_publishing_components/presenters/curated_taxonomy_sidebar_links"
require "govuk_publishing_components/presenters/content_item"
require "govuk_publishing_components/presenters/subscription_links_helper"

require "govuk_publishing_components/app_helpers/taxon_breadcrumbs"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module GovukPublishingComponents
module Presenters
class SubscriptionLinksHelper
def initialize(local_assigns)
@local_assigns = local_assigns
@feed_box_id = "feed-reader-#{SecureRandom.hex(2)}"
end

def email_signup_link
@local_assigns[:email_signup_link]
end

def email_signup_link_data_attributes
@local_assigns[:email_signup_link_data_attributes]
end

def feed_link_box_value
@local_assigns[:feed_link_box_value]
end

def feed_box_id
@feed_box_id
end

def email_signup_link_text
return @local_assigns[:email_signup_link_text] if @local_assigns[:email_signup_link_text]
I18n.t("govuk_component.subscription_links.email_signup_link_text", default: "Get email alerts")
end

def feed_link_text
return @local_assigns[:feed_link_text] if @local_assigns[:feed_link_text]
I18n.t("govuk_component.subscription_links.feed_link_text", default: "Subscribe to feed")
end

def component_data_is_valid?
email_signup_link.present? || feed_link.present? || feed_link_box_value.present?
end

def tracking_is_present?
@local_assigns[:email_signup_link_data_attributes].present? || @local_assigns[:feed_link_data_attributes].present?
end

def feed_link
return "#" if feed_link_box_value
@local_assigns[:feed_link]
end

def feed_link_data_attributes
data = @local_assigns[:feed_link_data_attributes] || {}
data[:controls] = feed_box_id
data[:expanded] = "false"
data
end

end
end
end

0 comments on commit 2d2e237

Please sign in to comment.