Skip to content

Commit

Permalink
Change image card to use shared helper for heading
Browse files Browse the repository at this point in the history
- update shared helper to return a span if heading level 0 is passed, instead of a heading level
- retain the default of h2 if nothing or an invalid value is passed
  • Loading branch information
andysellick committed Feb 22, 2019
1 parent fc1d9b3 commit 0866905
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
brand ||= false
brand_helper = GovukPublishingComponents::AppHelpers::BrandHelper.new(brand)
card_helper = GovukPublishingComponents::Presenters::ImageCardHelper.new(local_assigns)
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
%>
<% if card_helper.href || card_helper.extra_links.any? %>
<div class="gem-c-image-card <%= "gem-c-image-card--large" if card_helper.large %> <%= brand_helper.brand_class %>"
Expand All @@ -13,7 +14,7 @@
<%= card_helper.context %>

<% if card_helper.heading_text %>
<%= content_tag(card_helper.heading_tag,
<%= content_tag(shared_helper.get_heading_level,
class: "gem-c-image-card__title") do %>
<% if card_helper.href %>
<%= link_to card_helper.heading_text, card_helper.href,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def initialize(local_assigns)
@description = local_assigns[:description]
@large = local_assigns[:large]
@heading_text = local_assigns[:heading_text]
@heading_level = local_assigns[:heading_level] || 2
@extra_links_no_indent = local_assigns[:extra_links_no_indent]
@metadata = local_assigns[:metadata]
end
Expand Down Expand Up @@ -59,11 +58,6 @@ def context
end
end

def heading_tag
return "h#{@heading_level}" if [1, 2, 3, 4, 5, 6].include? @heading_level
return "span" if @heading_level.zero?
end

def description
content_tag(:div, @description, class: "gem-c-image-card__description") if @description
end
Expand Down
4 changes: 3 additions & 1 deletion lib/govuk_publishing_components/presenters/shared_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def get_margin_bottom
end

def get_heading_level
[*1..6].include?(@heading_level) ? "h#{@heading_level}" : "h2"
return [*1..6].include?(@heading_level) ? "h#{@heading_level}" : "h2" unless @heading_level.zero?

"span"
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/components/shared_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
heading = shared_helper.get_heading_level
expect(heading).to eql('h2')
end

it "returns a span instead of a heading if heading level is 0" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(heading_level: 0)
result = shared_helper.get_heading_level
expect(result).to eql('span')
end
end
end

0 comments on commit 0866905

Please sign in to comment.