Class: Formatic::Toggle

Inherits:
Base show all
Defined in:
app/components/formatic/toggle.rb

Overview

Stylish checkbox for a boolean attribute.

Instance Method Summary collapse

Methods inherited from Base

#input_name, #param_key, #value, #wrapper

Methods inherited from ApplicationComponent

#initialize

Constructor Details

This class inherits a constructor from Formatic::ApplicationComponent

Instance Method Details

#css_classesObject



35
36
37
# File 'app/components/formatic/toggle.rb', line 35

def css_classes
  ::Formatic::Css.call('c-formatic-toggle__input', manual_class)
end

#dom_idObject

There can be multiple checkboxes with the same attribute name on the page. To couple a <label> to its checkbox, make the checkbox ID unequivocal.



52
53
54
55
# File 'app/components/formatic/toggle.rb', line 52

def dom_id
  # For predictability in tests, maybe use something like `Time.now.nsec`?
  @dom_id ||= SecureRandom.hex(4)
end

#human_attribute_nameObject



44
45
46
47
48
# File 'app/components/formatic/toggle.rb', line 44

def human_attribute_name
  return unless f.object

  f.object.class.human_attribute_name(attribute_name)
end

#label_for_idObject

So that the wrapper <label> references our custom checkbox.



40
41
42
# File 'app/components/formatic/toggle.rb', line 40

def label_for_id
  dom_id
end