Module: FormtasticRebootstrap::Inputs::Base::Wrapping

Includes:
Formtastic::Inputs::Base::Wrapping
Included in:
FormtasticRebootstrap::Inputs::Base
Defined in:
lib/formtastic_rebootstrap/inputs/base/wrapping.rb

Instance Method Summary collapse

Instance Method Details

#add_on_content(content) ⇒ Object



55
56
57
58
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 55

def add_on_content(content)
  return nil unless content
  template.(:span, content, :class => 'input-group-addon')
end

#add_on_wrapper_classes(options) ⇒ Object



74
75
76
77
78
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 74

def add_on_wrapper_classes(options)
  [:prepend, :append, :prepend_content, :append_content].find do |key|
    options.has_key?(key)
  end ? ['input-group'] : []
end

#bootstrap_wrapping(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 8

def bootstrap_wrapping(&block)
  form_group_wrapping do
    label_html <<
    template.(:span, :class => wrapping_class_with_col&.uniq&.join(' ')) do
      input_content(&block) <<
      hint_html(:block) <<
      error_html(:block)
    end
  end
end

#form_group_wrapping(&block) ⇒ Object



60
61
62
63
64
65
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 60

def form_group_wrapping(&block)
  template.(:div,
    template.capture(&block).html_safe,
    wrapper_html_options
  )
end

#input_content(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 35

def input_content(&block)
  content = [
    add_on_content(options[:prepend]),
    options[:prepend_content],
    yield,
    add_on_content(options[:append]),
    options[:append_content]
  ].compact.join("\n").html_safe

  if prepended_or_appended?(options)
    template.(:div, content, :class => add_on_wrapper_classes(options).join(" "))
  else
    content
  end
end

#input_default_colObject



23
24
25
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 23

def input_default_col
  'col-md-9'
end

#input_options_classesObject



19
20
21
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 19

def input_options_classes
  options.dig(:input_html, :class)&.split(' ')&.flatten
end

#prepended_or_appended?(options) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 51

def prepended_or_appended?(options)
  options[:prepend] || options[:prepend_content] || options[:append] || options[:append_content]
end

#wrapper_html_optionsObject



67
68
69
70
71
72
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 67

def wrapper_html_options
  super.tap do |options|
    options[:class] << " form-group form-row"
    options[:class] << " has-error" if errors?
  end
end

#wrapping_class_with_colObject



27
28
29
30
31
32
33
# File 'lib/formtastic_rebootstrap/inputs/base/wrapping.rb', line 27

def wrapping_class_with_col
  return ['form-wrapper'] << input_default_col unless input_options_classes
  options[:input_html][:class] = options.dig(:input_html, :class)&.split(' ')
  ['form-wrapper'] <<  input_options_classes&.select do |c|
    c.match(/^col-(sm|md|lg|xl)?-?[0-9]{1,2}/) 
  end
end