Skip to content

Commit

Permalink
Stop repeatedly listing all three bound objects
Browse files Browse the repository at this point in the history
Instead add a #bound method to GOVUKDesignSystemFormBuilder::Base that
returns them.
  • Loading branch information
peteryates committed Mar 14, 2021
1 parent 111de21 commit e5e38fd
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 27 deletions.
9 changes: 9 additions & 0 deletions lib/govuk_design_system_formbuilder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ def to_s

private

# returns the attributes bound to the object that are
# required to build all contained elements
#
# @return [GOVUKDesignSystemFormBuilder::FormBuilder, Symbol, Symbol] an array containing the
# builder, object name and attribute name
def bound
[@builder, @object_name, @attribute_name]
end

def brand(override = nil)
override || config.brand
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def initialize(builder, object_name, attribute_name, hint:, legend:, caption:, s
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
Containers::FormGroup.new(*bound, **@form_group).html do
Containers::Fieldset.new(*bound, **fieldset_options).html do
safe_join([hint_element, error_element, hidden_field, checkboxes])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/containers/fieldset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def legend_element
@legend_element ||= if @legend.nil?
Elements::Null.new
else
Elements::Legend.new(@builder, @object_name, @attribute_name, **legend_options)
Elements::Legend.new(*bound, **legend_options)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def initialize(builder, object_name, attribute_name, hint:, legend:, caption:, i
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
Containers::FormGroup.new(*bound, **@form_group).html do
Containers::Fieldset.new(*bound, **fieldset_options).html do
safe_join([hint_element, error_element, radios])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def initialize(builder, object_name, attribute_name, collection, value_method:,
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
Containers::FormGroup.new(*bound, **@form_group).html do
Containers::Fieldset.new(*bound, **fieldset_options).html do
safe_join([supplemental_content, hint_element, error_element, check_boxes])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def options
end

def label_element
@label_element ||= Label.new(@builder, @object_name, @attribute_name, @checkbox, value: @value, link_errors: @link_errors)
@label_element ||= Label.new(*bound, @checkbox, value: @value, link_errors: @link_errors)
end

def hint_element
@hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
@hint_element ||= Elements::Hint.new(*bound, **hint_options, **hint_content)
end

def hint_options
Expand Down
4 changes: 2 additions & 2 deletions lib/govuk_design_system_formbuilder/elements/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, o
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
Containers::FormGroup.new(*bound, **@form_group).html do
Containers::Fieldset.new(*bound, **fieldset_options).html do
safe_join([supplemental_content, hint_element, error_element, date])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/elements/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, fo
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::FormGroup.new(*bound, **@form_group).html do
safe_join([label_element, supplemental_content, hint_element, error_element, file])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def initialize(builder, object_name, attribute_name, collection, value_method:,
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::Fieldset.new(@builder, @object_name, @attribute_name, **fieldset_options).html do
Containers::FormGroup.new(*bound, **@form_group).html do
Containers::Fieldset.new(*bound, **fieldset_options).html do
safe_join([hidden_field, supplemental_content, hint_element, error_element, radios])
end
end
Expand Down Expand Up @@ -56,7 +56,7 @@ def hidden_field

def collection
@collection.map.with_index do |item, i|
Elements::Radios::CollectionRadioButton.new(@builder, @object_name, @attribute_name, item, **collection_options(i)).html
Elements::Radios::CollectionRadioButton.new(*bound, item, **collection_options(i)).html
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def options
end

def hint_element
@hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
@hint_element ||= Elements::Hint.new(*bound, **hint_options, **hint_content)
end

def hint_content
Expand All @@ -53,7 +53,7 @@ def hint_options
end

def label_element
@label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **label_options)
@label_element ||= Elements::Label.new(*bound, **label_options)
end

def label_options
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/elements/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(builder, object_name, attribute_name, collection, value_method:,
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::FormGroup.new(*bound, **@form_group).html do
safe_join([label_element, supplemental_content, hint_element, error_element, select])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/elements/text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, ro

def html
Containers::CharacterCount.new(@builder, **character_count_options).html do
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::FormGroup.new(*bound, **@form_group).html do
safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/traits/caption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def caption_element
@caption_element ||= if @caption.nil?
Elements::Null.new
else
Elements::Caption.new(@builder, @object_name, @attribute_name, **@caption)
Elements::Caption.new(*bound, **@caption)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/traits/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def error_id
private

def error_element
@error_element ||= Elements::ErrorMessage.new(@builder, @object_name, @attribute_name)
@error_element ||= Elements::ErrorMessage.new(*bound)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/govuk_design_system_formbuilder/traits/fieldset_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def label_element
@label_element ||= if @label.nil?
Elements::Null.new
else
Elements::Label.new(@builder, @object_name, @attribute_name, **label_content, **label_options)
Elements::Label.new(*bound, **label_content, **label_options)
end
end

Expand All @@ -19,7 +19,7 @@ def hint_element
@hint_element ||= if @hint.nil?
Elements::Null.new
else
Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_options, **hint_content)
Elements::Hint.new(*bound, **hint_options, **hint_content)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/traits/hint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def hint_element
@hint_element ||= if @hint.nil?
Elements::Null.new
else
Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_content)
Elements::Hint.new(*bound, **hint_content)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/traits/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, pr
end

def html
Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
Containers::FormGroup.new(*bound, **@form_group).html do
safe_join([label_element, supplemental_content, hint_element, error_element, content])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/traits/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def label_element
@label_element ||= if @label.nil?
Elements::Null.new
else
Elements::Label.new(@builder, @object_name, @attribute_name, caption: @caption, **label_content)
Elements::Label.new(*bound, caption: @caption, **label_content)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_design_system_formbuilder/traits/supplemental.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def supplemental_id
private

def supplemental_content
@supplemental_content ||= Containers::Supplemental.new(@builder, @object_name, @attribute_name, @block_content)
@supplemental_content ||= Containers::Supplemental.new(*bound, @block_content)
end
end
end
Expand Down

0 comments on commit e5e38fd

Please sign in to comment.