Sha256: 3bc949fdffd63af5044be973cb336d87a8b874a9b10465375b3288006a774a37

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    module Radios
      class Collection < GOVUKDesignSystemFormBuilder::Base
        def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method:, hint_text:, legend:, inline:, small:, bold_labels:, &block)
          super(builder, object_name, attribute_name, &block)

          @collection    = collection
          @value_method  = value_method
          @text_method   = text_method
          @hint_method   = hint_method
          @inline        = inline
          @small         = small
          @legend        = legend
          @hint_text     = hint_text
          @bold_labels   = hint_method.present? || bold_labels
        end

        def html
          Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
            Containers::Fieldset.new(@builder, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
              safe_join(
                [
                  hint_element.html,
                  error_element.html,
                  supplemental_content.html,
                  Containers::Radios.new(@builder, inline: @inline, small: @small).html do
                    safe_join(build_collection)
                  end
                ]
              )
            end
          end
        end

      private

        def build_collection
          @collection.map.with_index do |item, i|
            Elements::Radios::CollectionRadioButton.new(
              @builder,
              @object_name,
              @attribute_name,
              item,
              value_method: @value_method,
              text_method: @text_method,
              hint_method: @hint_method,
              link_errors: has_errors? && i.zero?,
              bold_labels: @bold_labels
            ).html
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-1.0.1 lib/govuk_design_system_formbuilder/elements/radios/collection.rb
govuk_design_system_formbuilder-1.0.0 lib/govuk_design_system_formbuilder/elements/radios/collection.rb
govuk_design_system_formbuilder-0.9.8 lib/govuk_design_system_formbuilder/elements/radios/collection.rb