Module: BootstrapForm::Helpers::Bootstrap

Included in:
FormBuilder
Defined in:
lib/bootstrap_form/helpers/bootstrap.rb

Instance Method Summary collapse

Instance Method Details

#alert_message(title, options = {}) ⇒ Object


25
26
27
28
29
30
31
32
33
34
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 25

def alert_message(title, options = {})
  css = options[:class] || 'alert alert-danger'

  if object.respond_to?(:errors) && object.errors.full_messages.any?
     :div, class: css do
      concat  :p, title
      concat error_summary unless options[:error_summary] == false
    end
  end
end

#button(value = nil, options = {}, &block) ⇒ Object


4
5
6
7
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 4

def button(value = nil, options = {}, &block)
  options.reverse_merge! class: 'btn btn-secondary'
  super
end

#custom_control(*args, &block) ⇒ Object


72
73
74
75
76
77
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 72

def custom_control(*args, &block)
  options = args.extract_options!
  name = args.first

  form_group_builder(name, options, &block)
end

#error_summaryObject


36
37
38
39
40
41
42
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 36

def error_summary
   :ul, class: 'rails-bootstrap-forms-error-summary' do
    object.errors.full_messages.each do |error|
      concat (:li, error)
    end
  end
end

#errors_on(name, options = {}) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 44

def errors_on(name, options = {})
  if has_error?(name)
    hide_attribute_name = options[:hide_attribute_name] || false

     :div, class: "alert alert-danger" do
      if hide_attribute_name
        object.errors[name].join(", ")
      else
        object.errors.full_messages_for(name).join(", ")
      end
    end
  end
end

#input_group_content(content) ⇒ Object


97
98
99
100
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 97

def input_group_content(content)
  return content if content.match(/btn/)
  (:span, content, class: 'input-group-text')
end

#input_with_error(name, &block) ⇒ Object


92
93
94
95
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 92

def input_with_error(name, &block)
  input = capture(&block)
  input << generate_error(name)
end

#prepend_and_append_input(name, options, &block) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 79

def prepend_and_append_input(name, options, &block)
  options = options.extract!(:prepend, :append, :input_group_class)
  input_group_class = ["input-group", options[:input_group_class]].compact.join(' ')

  input = capture(&block) || "".html_safe

  input = (:div, input_group_content(options[:prepend]), class: 'input-group-prepend') + input if options[:prepend]
  input << (:div, input_group_content(options[:append]), class: 'input-group-append') if options[:append]
  input << generate_error(name)
  input = (:div, input, class: input_group_class) unless options.empty?
  input
end

#primary(name = nil, options = {}, &block) ⇒ Object


14
15
16
17
18
19
20
21
22
23
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 14

def primary(name = nil, options = {}, &block)
  options.reverse_merge! class: 'btn btn-primary'

  if options[:render_as_button] || block_given?
    options.except! :render_as_button
    button(name, options, &block)
  else
    submit(name, options)
  end
end

#static_classObject


102
103
104
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 102

def static_class
  "form-control-plaintext"
end

#static_control(*args) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 58

def static_control(*args)
  options = args.extract_options!
  name = args.first

  static_options = options.merge({
    readonly: true,
    control_class: [options[:control_class], static_class].compact.join(" ")
  })

  static_options[:value] = object.send(name) unless static_options.has_key?(:value)

  text_field_with_bootstrap(name, static_options)
end

#submit(name = nil, options = {}) ⇒ Object


9
10
11
12
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 9

def submit(name = nil, options = {})
  options.reverse_merge! class: 'btn btn-secondary'
  super
end