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 brand overrides on a per-helper basis #515

Closed
wants to merge 4 commits into from
Closed
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
11 changes: 9 additions & 2 deletions lib/govuk_design_system_formbuilder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ class Base
delegate :content_tag, :safe_join, :tag, :link_to, :capture, to: :@builder
delegate :config, to: GOVUKDesignSystemFormBuilder

def initialize(builder, object_name, attribute_name, &block)
# FIXME remove the `= nil` from override_brand once the rest of the super calls
# have been updated
def initialize(builder, object_name, attribute_name, override_brand = nil, &block)
@builder = builder
@object_name = object_name
@attribute_name = attribute_name
@override_brand = override_brand
@block_content = capture { block.call } if block_given?
end

Expand Down Expand Up @@ -47,10 +50,14 @@ def bound
[@builder, @object_name, @attribute_name]
end

def brand(override = nil)
def brand(override = @override_brand)
override || config.brand
end

def brand_options
{ brand: @override_brand }
end

def has_errors?
@builder.object.respond_to?(:errors) &&
@builder.object.errors.any? &&
Expand Down
10 changes: 6 additions & 4 deletions lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ def govuk_check_box_divider(text = config.default_check_box_divider_text)
# @param block [Block] When content is passed in via a block the submit element and the block content will
# be wrapped in a +<div class="govuk-button-group">+ which will space the buttons and links within
# evenly.
# @param brand [String] override the default brand prefix for this input
# @raise [ArgumentError] raised if both +warning+ and +secondary+ are true
# @return [ActiveSupport::SafeBuffer] HTML output
# @note Only the first additional button or link (passed in via a block) will be given the
Expand All @@ -895,8 +896,8 @@ def govuk_check_box_divider(text = config.default_check_box_divider_text)
# = f.govuk_submit "Proceed", prevent_double_click: true do
# = link_to 'Cancel', some_other_path, class: 'govuk-button__secondary'
#
def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, inverse: false, prevent_double_click: true, validate: config.default_submit_validate, disabled: false, **kwargs, &block)
Elements::Submit.new(self, text, warning:, secondary:, inverse:, prevent_double_click:, validate:, disabled:, **kwargs, &block).html
def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, inverse: false, prevent_double_click: true, validate: config.default_submit_validate, disabled: false, brand: nil, **kwargs, &block)
Elements::Submit.new(self, text, warning:, secondary:, inverse:, prevent_double_click:, validate:, disabled:, brand:, **kwargs, &block).html
end

# Generates three inputs for the +day+, +month+ and +year+ components of a date
Expand Down Expand Up @@ -924,6 +925,7 @@ def govuk_submit(text = config.default_submit_button_text, warning: false, secon
# @param maxlength_enabled [Boolean] adds maxlength attribute to day, month and year inputs (2, 2, and 4, respectively)
# @param segments [Hash] allows Rails' multiparameter attributes to be overridden on a field-by-field basis. Hash must
# contain +day:+, +month:+ and +year:+ keys. Defaults to the default value set in the +default_date_segments+ setting in {GOVUKDesignSystemFormBuilder.DEFAULTS}
# @param brand [String] override the default brand prefix for this input
# @param form_group [Hash] configures the form group
# @option form_group kwargs [Hash] additional attributes added to the form group
# @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element
Expand All @@ -946,8 +948,8 @@ def govuk_submit(text = config.default_submit_button_text, warning: false, secon
# @example A date input with legend supplied as a proc
# = f.govuk_date_field :finishes_on,
# legend: -> { tag.h3('Which category do you belong to?') }
def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_birth: false, omit_day: false, maxlength_enabled: false, segments: config.default_date_segments, form_group: {}, **kwargs, &block)
Elements::Date.new(self, object_name, attribute_name, hint:, legend:, caption:, date_of_birth:, omit_day:, maxlength_enabled:, segments:, form_group:, **kwargs, &block).html
def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_birth: false, omit_day: false, maxlength_enabled: false, segments: config.default_date_segments, form_group: {}, brand: nil, **kwargs, &block)
Elements::Date.new(self, object_name, attribute_name, hint:, legend:, caption:, date_of_birth:, omit_day:, maxlength_enabled:, segments:, form_group:, brand:, **kwargs, &block).html
end

# Generates a summary of errors in the form, each linking to the corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module GOVUKDesignSystemFormBuilder
module Containers
class ButtonGroup < Base
def initialize(builder, buttons)
super(builder, nil, nil)
def initialize(builder, buttons, brand:)
super(builder, nil, nil, brand)

@buttons = buttons
end
Expand Down
6 changes: 3 additions & 3 deletions lib/govuk_design_system_formbuilder/containers/fieldset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Containers
class Fieldset < Base
include Traits::HTMLAttributes

def initialize(builder, object_name = nil, attribute_name = nil, legend: {}, caption: {}, described_by: nil, **kwargs, &block)
super(builder, object_name, attribute_name, &block)
def initialize(builder, object_name = nil, attribute_name = nil, brand:, legend: {}, caption: {}, described_by: nil, **kwargs, &block)
super(builder, object_name, attribute_name, brand, &block)

@legend = legend
@caption = caption
Expand Down Expand Up @@ -36,7 +36,7 @@ def legend_element
@legend_element ||= if @legend.nil?
Elements::Null.new
else
Elements::Legend.new(*bound, **legend_options)
Elements::Legend.new(*bound, **brand_options, **legend_options)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/govuk_design_system_formbuilder/containers/form_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class FormGroup < Base
include Traits::HTMLAttributes
include Traits::HTMLClasses

def initialize(builder, object_name, attribute_name, **kwargs)
super(builder, object_name, attribute_name)
def initialize(builder, object_name, attribute_name, brand:, **kwargs)
super(builder, object_name, attribute_name, brand)

@html_attributes = kwargs
end
Expand Down
8 changes: 4 additions & 4 deletions lib/govuk_design_system_formbuilder/elements/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Date < Base

MULTIPARAMETER_KEY = { day: 3, month: 2, year: 1 }.freeze

def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, maxlength_enabled:, segments:, form_group:, date_of_birth: false, **kwargs, &block)
super(builder, object_name, attribute_name, &block)
def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, maxlength_enabled:, segments:, form_group:, brand:, date_of_birth: false, **kwargs, &block)
super(builder, object_name, attribute_name, brand, &block)

@legend = legend
@caption = caption
Expand All @@ -25,8 +25,8 @@ def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, o
end

def html
Containers::FormGroup.new(*bound, **@form_group, **@html_attributes).html do
Containers::Fieldset.new(*bound, **fieldset_options).html do
Containers::FormGroup.new(*bound, **brand_options, **@form_group, **@html_attributes).html do
Containers::Fieldset.new(*bound, **brand_options, **fieldset_options).html do
safe_join([supplemental_content, hint_element, error_element, date])
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/govuk_design_system_formbuilder/elements/legend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Legend < Base
include Traits::HTMLAttributes
include Traits::HTMLClasses

def initialize(builder, object_name, attribute_name, text: nil, size: config.default_legend_size, hidden: false, tag: config.default_legend_tag, caption: nil, content: nil, **kwargs)
super(builder, object_name, attribute_name)
def initialize(builder, object_name, attribute_name, brand:, text: nil, size: config.default_legend_size, hidden: false, tag: config.default_legend_tag, caption: nil, content: nil, **kwargs)
super(builder, object_name, attribute_name, brand)

if content
@content = capture { content.call }
Expand Down
6 changes: 3 additions & 3 deletions lib/govuk_design_system_formbuilder/elements/submit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Submit < Base
include Traits::HTMLClasses
include Traits::HTMLAttributes

def initialize(builder, text, warning:, secondary:, inverse:, prevent_double_click:, validate:, disabled:, **kwargs, &block)
super(builder, nil, nil)
def initialize(builder, text, warning:, secondary:, inverse:, prevent_double_click:, validate:, disabled:, brand:, **kwargs, &block)
super(builder, nil, nil, brand)

fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary

Expand Down Expand Up @@ -39,7 +39,7 @@ def build_text(text)
end

def button_group
Containers::ButtonGroup.new(@builder, buttons).html
Containers::ButtonGroup.new(@builder, buttons, brand: @brand).html
end

def buttons
Expand Down
1 change: 1 addition & 0 deletions spec/govuk_design_system_formbuilder/builder/date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
it_behaves_like 'a field that supports setting the hint via localisation'

it_behaves_like 'a field that supports custom branding'
it_behaves_like 'a field that allows branding to be explicitly overriden'
it_behaves_like 'a field that contains a customisable form group'

it_behaves_like 'a field that supports a fieldset with legend'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
include_examples 'HTML formatting checks'

it_behaves_like 'a field that supports custom branding'
it_behaves_like 'a field that allows branding to be explicitly overriden'

it_behaves_like 'a field that supports custom classes' do
let(:element) { 'button' }
Expand Down
15 changes: 15 additions & 0 deletions spec/support/shared/shared_supports_branding_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@
expect(subject).not_to match(Regexp.new(default_brand))
end
end

shared_examples 'a field that allows branding to be explicitly overriden' do
let(:custom_brand) { 'globex-corp' }
let(:default_brand) { 'govuk' }

subject { builder.send(*args, brand: custom_brand) }

specify 'should contain the custom branding' do
expect(subject).to match(Regexp.new(custom_brand))
end

specify 'should not contain any default branding' do
expect(subject).not_to match(Regexp.new(default_brand))
end
end
Loading