Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the back link component to take blocks #207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/components/govuk_component/back_link_component.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
class GovukComponent::BackLinkComponent < GovukComponent::Base
attr_reader :text, :href

def initialize(text:, href:, classes: nil, html_attributes: {})
def initialize(href:, text: nil, classes: nil, html_attributes: {})
super(classes: classes, html_attributes: html_attributes)

@text = text
@href = href
end

def call
link_to(text, href, class: classes, **html_attributes)
link_to(link_content, href, class: classes, **html_attributes)
end

private

def link_content
text || content || fail(ArgumentError, "no text or content")
end

def default_classes
%w(govuk-back-link)
end
Expand Down
25 changes: 25 additions & 0 deletions spec/components/govuk_component/back_link_component_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

RSpec.describe(GovukComponent::BackLinkComponent, type: :component) do
include_context 'helpers'
include_context 'setup'

let(:text) { 'Department for Education' }
Expand All @@ -14,6 +15,30 @@
expect(rendered_component).to have_tag('a', text: text, with: { href: href, class: component_css_class })
end

context 'when link text is provided via a block' do
let(:custom_text) { "Some text" }
let(:custom_tag) { :code }
subject! do
render_inline(GovukComponent::BackLinkComponent.new(href: href)) do
helper.content_tag(custom_tag, custom_text)
end
end

specify 'renders the component with custom tag and text' do
expect(rendered_component).to have_tag('a', with: { href: href, class: component_css_class }) do
with_tag(custom_tag, text: custom_text)
end
end
end

context "when neither text or a block is supplied" do
specify "raises an appropriate error" do
expect {
render_inline(GovukComponent::BackLinkComponent.new(href: href))
}.to raise_error(ArgumentError, "no text or content")
end
end

it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'
end