Class: FormProps::Inputs::RadioButton

Inherits:
Base
  • Object
show all
Defined in:
lib/form_props/inputs/radio_button.rb

Instance Method Summary collapse

Methods inherited from Base

#json

Constructor Details

#initialize(object_name, method_name, template_object, tag_value, options) ⇒ RadioButton

Returns a new instance of RadioButton.



6
7
8
9
# File 'lib/form_props/inputs/radio_button.rb', line 6

def initialize(object_name, method_name, template_object, tag_value, options)
  @tag_value = tag_value
  super(object_name, method_name, template_object, options)
end

Instance Method Details

#input_checked?(options) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
# File 'lib/form_props/inputs/radio_button.rb', line 11

def input_checked?(options)
  if options.has_key?(:checked)
    checked = options.delete(:checked)
    checked == true || checked == "checked"
  else
    checked?(value)
  end
end

#render(flatten = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/form_props/inputs/radio_button.rb', line 20

def render(flatten = false)
  @options[:type] = "radio"
  @options[:value] = @tag_value
  @options[:checked] = true if input_checked?(@options)

  body_block = -> {
    add_default_name_and_id_for_value(@tag_value, @options)
    input_props(@options)
  }

  if flatten
    body_block.call
  else
    json.set!(sanitized_key) do
      body_block.call
    end
  end
end