Module: NitroKit::CheckboxHelper

Defined in:
app/helpers/nitro_kit/checkbox_helper.rb

Instance Method Summary collapse

Instance Method Details

#nk_checkbox(compat_object_name = nil, compat_method = nil, compat_options = {}, compat_checked_value = "1", compat_unchecked_value = "0", label: nil, **attrs) ⇒ Object

Make API compatible with Rails’ checkbox but allow empty arguments



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/nitro_kit/checkbox_helper.rb', line 6

def nk_checkbox(
  compat_object_name = nil,
  compat_method = nil,
  compat_options = {},
  compat_checked_value = "1",
  compat_unchecked_value = "0",
  label: nil,
  **attrs
)
  name = field_name(compat_object_name, compat_method)
  checked = compat_options["checked"] || attrs[:checked]

  # TODO: multiple, unchecked hidden field

  render(Checkbox.from_template(name:, label:, value: compat_checked_value, checked:, **attrs))
end

#nk_checkbox_group(**attrs, &block) ⇒ Object



46
47
48
# File 'app/helpers/nitro_kit/checkbox_helper.rb', line 46

def nk_checkbox_group(**attrs, &block)
  render(CheckboxGroup.from_template(**attrs), &block)
end

#nk_checkbox_tag(name, *args) ⇒ Object Also known as: nk_check_box_tag



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/nitro_kit/checkbox_helper.rb', line 23

def nk_checkbox_tag(name, *args)
  if args.length >= 4
    raise ArgumentError, "wrong number of arguments (given #{args.length + 1}, expected 1..4)"
  end

  options = args.extract_options!
  value, checked = args.empty? ? [ "1", false ] : [ *args, false ]
  attrs = {
    type: "checkbox",
    name: name,
    id: sanitize_to_id(name),
    value: value
  }.update(
    options.symbolize_keys
  )

  attrs[:checked] = "checked" if checked

  render(Checkbox.from_template(name:, **attrs))
end