Class: Effective::FormInputs::Checks

Inherits:
CollectionInput show all
Defined in:
app/models/effective/form_inputs/checks.rb

Constant Summary

Constants inherited from Effective::FormInput

Effective::FormInput::BLANK, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES

Instance Attribute Summary

Attributes inherited from Effective::FormInput

#name, #options

Instance Method Summary collapse

Methods inherited from CollectionInput

#assign_options_collection!, #assign_options_collection_methods!, #collection_options, #custom?, #group_label_method, #group_method, #grouped?, #html_options, #initialize, #inline?, #label_method, #option_key_method, #option_value_method, #options_collection, #polymorphic?, #polymorphic_id_method, #polymorphic_id_value, #polymorphic_type_method, #polymorphic_type_value, #polymorphic_value, #value_method

Methods inherited from Effective::FormInput

#hint_options, #initialize, #input_group_options, #input_js_options, #label_options, #to_html

Constructor Details

This class inherits a constructor from Effective::FormInputs::CollectionInput

Instance Method Details

#build_input(&block) ⇒ Object



5
6
7
# File 'app/models/effective/form_inputs/checks.rb', line 5

def build_input(&block)
  @builder.collection_check_boxes(name, options_collection, value_method, label_method, collection_options, item_input_options) { |builder| build_item(builder) }
end

#build_item(builder) ⇒ Object



47
48
49
50
# File 'app/models/effective/form_inputs/checks.rb', line 47

def build_item(builder)
  item_id = unique_id(builder.object)
  build_item_wrap { builder.check_box(id: item_id) + builder.label(item_label_options.merge(for: item_id)) }
end

#build_item_wrap(&block) ⇒ Object



52
53
54
55
56
57
58
# File 'app/models/effective/form_inputs/checks.rb', line 52

def build_item_wrap(&block)
  if custom?
    (:div, yield, class: 'custom-control custom-checkbox ' + (inline? ? 'custom-control-inline' : 'form-group'))
  else
    (:div, yield, class: 'form-check' + (inline? ? ' form-check-inline' : ''))
  end
end

#build_labelObject



38
39
40
41
42
43
44
45
# File 'app/models/effective/form_inputs/checks.rb', line 38

def build_label
  return BLANK if options[:label] == false
  return BLANK if name.kind_of?(NilClass)

  text = (options[:label].delete(:text) || (object.class.human_attribute_name(name) if object) || BLANK).html_safe

  ((inline? ? :label : :legend), text, options[:label])
end

#build_wrapper(&block) ⇒ Object



9
10
11
12
13
14
15
# File 'app/models/effective/form_inputs/checks.rb', line 9

def build_wrapper(&block)
  if layout == :horizontal
    (:fieldset, (:div, yield, class: 'row'), options[:wrapper])
  else
    (:fieldset, yield, options[:wrapper])
  end
end

#feedback_optionsObject



21
22
23
24
25
26
27
28
# File 'app/models/effective/form_inputs/checks.rb', line 21

def feedback_options
  return false if layout == :inline

  {
    valid: { class: 'valid-feedback', style: ('display: block;' if has_error? && !has_error?(name)) }.compact,
    invalid: { class: 'invalid-feedback', style: ('display: block;' if has_error?(name)) }.compact
  }
end

#input_html_optionsObject



30
31
32
33
34
35
36
# File 'app/models/effective/form_inputs/checks.rb', line 30

def input_html_options
  if custom?
    { class: 'custom-control-input' }
  else
    { class: 'form-check-input' }
  end
end

#item_input_optionsObject



60
61
62
# File 'app/models/effective/form_inputs/checks.rb', line 60

def item_input_options
  options[:input].except(:inline, :custom)
end

#item_label_optionsObject



64
65
66
67
68
69
70
# File 'app/models/effective/form_inputs/checks.rb', line 64

def item_label_options
  if custom?
    { class: 'custom-control-label' }
  else
    { class: 'form-check-label' }
  end
end

#wrapper_optionsObject



17
18
19
# File 'app/models/effective/form_inputs/checks.rb', line 17

def wrapper_options
  { class: "form-group #{tag_id}" }
end