Class: Proscenium::UI::Form::Fields::Checkbox

Inherits:
Base
  • Object
show all
Defined in:
lib/proscenium/ui/form/fields/checkbox.rb

Overview

Renders a checkbox field similar to how Rails handles it.

A predicate attribute name can be given:

checkbox_field :active?

Instance Attribute Summary

Attributes inherited from Base

#attribute, #attributes, #form, #model

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Proscenium::UI::Form::Fields::Base

Instance Method Details

#view_templateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/proscenium/ui/form/fields/checkbox.rb', line 13

def view_template
  checked = ActiveModel::Type::Boolean.new.cast(value.nil? ? false : value)

  checked_value = attributes.delete(:checked_value) || '1'
  unchecked_value = attributes.delete(:unchecked_value) || '0'

  # TODO: use component
  # render Proscenium::UI::Fields::Checkbox::Component.new field_name, checked:

  field :pui_checkbox do
    label do |content|
      input(name: field_name, type: :hidden, value: unchecked_value, **attributes)
      input(name: field_name, type: :checkbox, value: checked_value, checked:, **attributes)
      yield_content_with_no_args { content }
    end
    hint
  end
end