Class: BulmaPhlex::Rails::RadioButton

Inherits:
Base
  • Object
show all
Defined in:
lib/bulma_phlex/rails/components/radio_button.rb

Overview

# Radio Button Component

This component renders a Bulma-styled radio button within a form. It integrates with Rails’ form builder to ensure proper labeling and value handling.

The label is looked up using I18n based on the method and tag value. The key is the combination of the method name and the tag value, formatted as ‘method_tagvalue`, under the scope `helpers.label.object_name`. If no translation is found, it defaults to a humanized version of the tag value.

Example usage: “‘ruby form_with model: @project do |f|

f.radio_button :status, "active"
f.radio_button :status, "archived"

end

Instance Method Summary collapse

Constructor Details

#initialize(form_builder, method, tag_value, options, delivered) ⇒ RadioButton

Returns a new instance of RadioButton.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bulma_phlex/rails/components/radio_button.rb', line 21

def initialize(form_builder, method, tag_value, options, delivered)
  @form_builder = form_builder
  @method = method
  @tag_value = tag_value
  @options = options.dup
  @delivered = delivered

  @options[:class] = Array.wrap(@options[:class]) << "mr-2"
  @form_field_options = @options.extract!(:column, :grid)
                                .with_defaults(column: @form_builder.columns_flag,
                                               grid: @form_builder.grid_flag)
end

Instance Method Details

#view_templateObject



34
35
36
37
38
39
40
41
# File 'lib/bulma_phlex/rails/components/radio_button.rb', line 34

def view_template
  render FormField.new(**@form_field_options) do
    label(class: "radio") do
      raw @delivered.call(@options)
      plain label_from_tag_value
    end
  end
end