Class: Phlexi::Form::Components::CollectionRadioButtons

Inherits:
Base
  • Object
show all
Includes:
Phlexi::Form::Components::Concerns::AcceptsChoices, Phlexi::Form::Components::Concerns::HandlesInput
Defined in:
lib/phlexi/form/components/collection_radio_buttons.rb

Instance Attribute Summary

Attributes inherited from Base

#attributes, #field

Instance Method Summary collapse

Methods included from Phlexi::Form::Components::Concerns::ExtractsInput

#extract_input

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Phlexi::Form::Components::Base

Instance Method Details

#view_templateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/phlexi/form/components/collection_radio_buttons.rb', line 10

def view_template
  div(**attributes.slice(:id, :class)) do
    field.repeated(choices.values) do |builder|
      render builder.hidden_field_tag if builder.index == 0

      field = builder.field(
        label: choices[builder.key],
        # We set the attributes here so they are applied to all input components even if the user decides to use a block
        input_attributes: {
          checked_value: builder.key,
          checked: selected?(builder.key)
        }
      )
      if block_given?
        yield field
      else
        render field.radio_button_tag
        render field.label_tag
      end
    end
  end
end