Skip to content

Commit

Permalink
Integrate Traits::CollectionItem with elements
Browse files Browse the repository at this point in the history
Instead of using `#send` inline to retrieve the value from `@item`, make
use of `#retrieve` which works with procs.

Refs #68
  • Loading branch information
peteryates committed Dec 7, 2019
1 parent b688946 commit de79ebe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module GOVUKDesignSystemFormBuilder
module Elements
module CheckBoxes
class CollectionCheckBox < GOVUKDesignSystemFormBuilder::Base
include Traits::CollectionItem

def initialize(builder, object_name, attribute_name, checkbox, hint_method = nil, link_errors: false)
super(builder, object_name, attribute_name)
@checkbox = checkbox
@item = checkbox.object
@value = checkbox.value
@hint_text = @item.send(hint_method) if hint_method.present?
@hint_text = retrieve(@item, hint_method)
@link_errors = link_errors
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ module GOVUKDesignSystemFormBuilder
module Elements
module Radios
class CollectionRadioButton < Base
include Traits::CollectionItem

# @param link_errors [Boolean] used to control the id generated for radio buttons. The
# error summary requires that the id of the first radio is linked-to from the corresponding
# error message. As when the summary is built what happens later in the form is unknown, we
# need to control this to ensure the link is generated correctly
def initialize(builder, object_name, attribute_name, item, value_method:, text_method:, hint_method:, link_errors: false, bold_labels:)
super(builder, object_name, attribute_name)
@item = item
@value = item.send(value_method)
@text = item.send(text_method)
@hint_text = item.send(hint_method) if hint_method.present?
@value = retrieve(item, value_method)
@label_text = retrieve(item, text_method)
@hint_text = retrieve(item, hint_method)
@link_errors = link_errors
@bold_labels = bold_labels
end
Expand Down Expand Up @@ -41,7 +43,7 @@ def hint_element
end

def label_element
@label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, text: @text, value: @value, radio: true, size: label_size, link_errors: @link_errors)
@label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, text: @label_text, value: @value, radio: true, size: label_size, link_errors: @link_errors)
end

def label_size
Expand Down

0 comments on commit de79ebe

Please sign in to comment.