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::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS

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)


186
187
188
189
# File 'app/models/effective/form_inputs/radios.rb', line 186

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



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

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



28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/effective/form_inputs/radios.rb', line 28

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/effective/form_inputs/radios.rb', line 108

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' if active_item?(builder)), ('first-card' if first_item?) ].compact.join(' ')

    builder.label(opts) { builder.radio_button(id: item_id) + build_item_wrap { builder.text } }
  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



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/effective/form_inputs/radios.rb', line 126

def build_item_wrap(&block)
  if cards? && options_collection_includes_cards?
    yield # Not required.
  elsif cards?
    (:div, class: 'card') do
      (:div, yield, class: ('card-body' if card_body?))
    end
  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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/effective/form_inputs/radios.rb', line 90

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



18
19
20
21
22
23
24
25
26
# File 'app/models/effective/form_inputs/radios.rb', line 18

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



58
59
60
61
62
63
64
65
66
67
# File 'app/models/effective/form_inputs/radios.rb', line 58

def button_group_class
  [
    'effective-radios',
    ('btn-group btn-group-toggle' if buttons?),
    ('cards 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



177
178
179
# File 'app/models/effective/form_inputs/radios.rb', line 177

def button_group_id
  "#{tag_id}_btn_group"
end

#buttons?Boolean

default false

Returns:

  • (Boolean)


161
162
163
164
# File 'app/models/effective/form_inputs/radios.rb', line 161

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

#card_body?Boolean

default true

Returns:

  • (Boolean)


171
172
173
174
175
# File 'app/models/effective/form_inputs/radios.rb', line 171

def card_body? # default true
  return @card_body unless @card_body.nil?
  value = options.delete(:card_body)
  @card_body = (value == nil ? true : value)
end

#cards?Boolean

default false

Returns:

  • (Boolean)


166
167
168
169
# File 'app/models/effective/form_inputs/radios.rb', line 166

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

#feedback_optionsObject



69
70
71
72
73
74
75
76
# File 'app/models/effective/form_inputs/radios.rb', line 69

def feedback_options
  return false if layout == :inline

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

#first_item?Boolean

Returns:

  • (Boolean)


181
182
183
184
# File 'app/models/effective/form_inputs/radios.rb', line 181

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

#input_html_optionsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/effective/form_inputs/radios.rb', line 78

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

#item_input_optionsObject



140
141
142
# File 'app/models/effective/form_inputs/radios.rb', line 140

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

#item_label_optionsObject



149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/effective/form_inputs/radios.rb', line 149

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

#options_collection_includes_cards?Boolean

Returns:

  • (Boolean)


144
145
146
147
# File 'app/models/effective/form_inputs/radios.rb', line 144

def options_collection_includes_cards?
  return @options_collection_includes_cards unless @options_collection_includes_cards.nil?
  @options_collection_includes_cards = options_collection.first(3).any? { |text, _| text.to_s.include?("div class=\"card") }
end

#wrapper_optionsObject



50
51
52
53
54
55
56
# File 'app/models/effective/form_inputs/radios.rb', line 50

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