Skip to content

Commit

Permalink
Make date segments overridable
Browse files Browse the repository at this point in the history
Following on from #493 this allows the date segments to be overridden on
a field-by-field basis instead of just at the app level.
  • Loading branch information
peteryates committed Apr 23, 2024
1 parent f3ac940 commit 00863f7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,8 @@ def govuk_submit(text = config.default_submit_button_text, warning: false, secon
# @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
# @param omit_day [Boolean] do not render a day input, only capture month and year
# @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 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 @@ -944,8 +946,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, form_group: {}, **kwargs, &block)
Elements::Date.new(self, object_name, attribute_name, hint:, legend:, caption:, date_of_birth:, omit_day:, maxlength_enabled:, 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: {}, **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
end

# Generates a summary of errors in the form, each linking to the corresponding
Expand Down
11 changes: 4 additions & 7 deletions lib/govuk_design_system_formbuilder/elements/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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:, form_group:, date_of_birth: false, **kwargs, &block)
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)

@legend = legend
Expand All @@ -19,6 +19,7 @@ def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, o
@date_of_birth = date_of_birth
@omit_day = omit_day
@maxlength_enabled = maxlength_enabled
@segments = segments
@form_group = form_group
@html_attributes = kwargs
end
Expand All @@ -33,10 +34,6 @@ def html

private

def segments
config.default_date_segments
end

def fieldset_options
{ legend: @legend, caption: @caption, described_by: [error_id, hint_id, supplemental_id] }
end
Expand Down Expand Up @@ -139,7 +136,7 @@ def id(segment, link_errors)
if has_errors? && link_errors
field_id(link_errors:)
else
[@object_name, @attribute_name, segments.fetch(segment)].join("_")
[@object_name, @attribute_name, @segments.fetch(segment)].join("_")
end
end

Expand All @@ -148,7 +145,7 @@ def name(segment)
"%<object_name>s[%<input_name>s(%<segment>s)]",
object_name: @object_name,
input_name: @attribute_name,
segment: segments.fetch(segment)
segment: @segments.fetch(segment)
)
end

Expand Down
16 changes: 16 additions & 0 deletions spec/govuk_design_system_formbuilder/builder/date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@
end
end

context 'when segments are overridden' do
subject { builder.send(*args, segments: { day: "day", month: "month", year: "year" }) }

specify "sets the day field correctly" do
expect(subject).to have_tag('input', with: { id: "person_born_on_day", name: "person[born_on(day)]" })
end

specify "sets the month field correctly" do
expect(subject).to have_tag('input', with: { id: "person_born_on_month", name: "person[born_on(month)]" })
end

specify "sets the year field correctly" do
expect(subject).to have_tag('input', with: { id: "person_born_on_year", name: "person[born_on(year)]" })
end
end

context 'showing only month and year inputs' do
subject { builder.send(*args, omit_day: true) }

Expand Down

0 comments on commit 00863f7

Please sign in to comment.