Class: Effective::FormInputs::Radios

Inherits:
CollectionInput show all
Defined in:
app/models/effective/form_inputs/radios.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, #translate, #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

#active_item?(builder) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
# File 'app/models/effective/form_inputs/radios.rb', line 168

def active_item?(builder)
  value = self.value || collection_options[:checked]
  value == builder.value || Array(value).map(&:to_s) == Array(builder.value).map(&:to_s)
end

#build_button_group(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/models/effective/form_inputs/radios.rb', line 33

def build_button_group(&block)
  if buttons?
    (:div, yield, id: button_group_id, class: button_group_class, 'data-toggle': 'buttons')
  elsif cards?
    (:div, yield, id: button_group_id, class: button_group_class, 'data-toggle': 'cards')
  else
    yield
  end
end

#build_input(&block) ⇒ Object



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

def build_input(&block)
  build_button_group do
    html = @builder.collection_radio_buttons(name, options_collection, value_method, label_method, collection_options, item_input_options) { |builder| build_item(builder) }

    if disabled? # collection_check_boxes doesn't correctly disable the input type hidden, but does on the build_items
      html = html.sub('<input type="hidden"', '<input type="hidden" disabled="disabled"').html_safe
    end

    html
  end
end

#build_item(builder) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/effective/form_inputs/radios.rb', line 101

def build_item(builder)
  item_id = unique_id(builder.object)

  if buttons?
    opts = item_label_options.merge(for: item_id)
    opts[:class] = [opts[:class], ('active' if active_item?(builder)), ('first-button' if first_item?) ].compact.join(' ')

    builder.label(opts) { builder.radio_button(id: item_id) + builder.text }
  elsif cards?
    opts = item_label_options.merge(for: item_id)
    opts[:class] = [opts[:class], ('active border-secondary' if active_item?(builder)), ('first-card' if first_item?) ].compact.join(' ')

    if active_item?(builder)
      builder.label(opts) { builder.radio_button(id: item_id) + builder.text.sub('card-header', 'card-header bg-secondary text-white').html_safe }
    else
      builder.label(opts) { builder.radio_button(id: item_id) + builder.text }
    end
  else
    build_item_wrap { builder.radio_button(id: item_id) + builder.label(item_label_options.merge(for: item_id)) }
  end
end

#build_item_wrap(&block) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'app/models/effective/form_inputs/radios.rb', line 123

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

#build_labelObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/effective/form_inputs/radios.rb', line 83

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

  tag = (buttons? || inline?) ? :label : :legend
  text = (options[:label].delete(:text) || (object.class.human_attribute_name(name) if object) || BLANK).html_safe

  if buttons?
    (:label, text, options[:label].merge(for: button_group_id))
  elsif cards?
    (:label, text, options[:label].merge(for: button_group_id))
  elsif inline?
    (:label, text, options[:label])
  else
    (:legend, text, options[:label])
  end
end

#build_wrapper(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'app/models/effective/form_inputs/radios.rb', line 11

def build_wrapper(&block)
  tag = (buttons? || cards?) ? :div : :fieldset

  if layout == :horizontal
    (tag, (:div, yield, class: 'row'), options[:wrapper])
  else
    (tag, yield, options[:wrapper])
  end
end

#button_group_classObject



51
52
53
54
55
56
57
58
59
60
# File 'app/models/effective/form_inputs/radios.rb', line 51

def button_group_class
  [
    'effective-radios',
    ('btn-group btn-group-toggle' if buttons?),
    ('card-deck' if cards?),
    ('no-feedback' unless feedback_options),
    ('is-invalid' if feedback_options && has_error?(name)),
    ('is-valid' if feedback_options && has_error? && !has_error?(name))
  ].compact.join(' ')
end

#button_group_idObject



159
160
161
# File 'app/models/effective/form_inputs/radios.rb', line 159

def button_group_id
  "#{tag_id}_btn_group"
end

#buttons?Boolean

default false

Returns:

  • (Boolean)


149
150
151
152
# File 'app/models/effective/form_inputs/radios.rb', line 149

def buttons? # default false
  return @buttons unless @buttons.nil?
  @buttons = (options.delete(:buttons) || false)
end

#cards?Boolean

default false

Returns:

  • (Boolean)


154
155
156
157
# File 'app/models/effective/form_inputs/radios.rb', line 154

def cards? # default false
  return @cards unless @cards.nil?
  @cards = (options.delete(:cards) || false)
end

#feedback_optionsObject



62
63
64
65
66
67
68
69
# File 'app/models/effective/form_inputs/radios.rb', line 62

def feedback_options
  return false if layout == :inline

  {
    valid: { class: 'valid-feedback' }.compact,
    invalid: { class: 'invalid-feedback' }.compact
  }
end

#first_item?Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'app/models/effective/form_inputs/radios.rb', line 163

def first_item?
  return false unless @first_item.nil?
  @first_item = true
end

#input_html_optionsObject



71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/effective/form_inputs/radios.rb', line 71

def input_html_options
  if buttons?
    { autocomplete: 'off' }
  elsif cards?
    { autocomplete: 'off' }
  elsif custom?
    { class: 'custom-control-input' }
  else
    { class: 'form-check-input' }
  end
end

#item_input_optionsObject



133
134
135
# File 'app/models/effective/form_inputs/radios.rb', line 133

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

#item_label_optionsObject



137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/effective/form_inputs/radios.rb', line 137

def item_label_options
  if buttons?
    { class: 'btn btn-outline-secondary' }
  elsif cards?
    { class: 'card' }
  elsif custom?
    { class: 'custom-control-label' }
  else
    { class: 'form-check-label' }
  end
end

#wrapper_optionsObject



43
44
45
46
47
48
49
# File 'app/models/effective/form_inputs/radios.rb', line 43

def wrapper_options
  if buttons? || cards?
    { class: "form-group #{tag_id}" }
  else
    { class: "form-group #{tag_id} #{button_group_class}" }
  end
end