Class: FormProps::Inputs::CheckBox

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

Instance Method Summary collapse

Methods inherited from Base

#json

Constructor Details

#initialize(object_name, method_name, template_object, checked_value, unchecked_value, options) ⇒ CheckBox

Returns a new instance of CheckBox.



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

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

Instance Method Details

#input_checked?(options) ⇒ Boolean

Returns:

  • (Boolean)


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

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/form_props/inputs/check_box.rb', line 21

def render(flatten = false)
  options = @options.stringify_keys
  options[:type] = "checkbox"
  options[:value] = @checked_value
  options[:checked] = true if input_checked?(options)
  options[:unchecked_value] = @unchecked_value || ""
  options[:include_hidden] = options.fetch(:include_hidden) { true }

  body_block = -> {
    if options[:multiple]
      add_default_name_and_id_for_value(@checked_value, options)
      options.delete(:multiple)
    else
      add_default_name_and_id(options)
    end

    input_props(options)
  }

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