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



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 4

def alert_message(title, options={})
  css = options[:class] || "alert alert-danger"
  return unless object.respond_to?(:errors) && object.errors.full_messages.any?

  tag.div class: css do
    if options[:error_summary] == false
      title
    else
      tag.p(title) + error_summary
    end
  end
end

#custom_control(*args, &block) ⇒ Object



56
57
58
59
60
61
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 56

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

  form_group_builder(name, options, &block)
end

#error_summaryObject



17
18
19
20
21
22
23
24
25
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 17

def error_summary
  return unless object.errors.any?

  tag.ul(class: "rails-bootstrap-forms-error-summary") do
    object.errors.full_messages.reduce(ActiveSupport::SafeBuffer.new) do |acc, error|
      acc << tag.li(error)
    end
  end
end

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 27

def errors_on(name, options={})
  return unless error?(name)

  hide_attribute_name = options[:hide_attribute_name] || false
  custom_class = options[:custom_class] || false

  tag.div class: custom_class || "invalid-feedback" do
    if hide_attribute_name
      object.errors[name].join(", ")
    else
      object.errors.full_messages_for(name).join(", ")
    end
  end
end

#input_group_content(content) ⇒ Object



80
81
82
83
84
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 80

def input_group_content(content)
  return content if content.include?("btn")

  tag.span(content, class: "input-group-text")
end

#input_with_error(name, &block) ⇒ Object



75
76
77
78
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 75

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

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



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 63

def prepend_and_append_input(name, options, &block)
  options = options.extract!(:prepend, :append, :input_group_class).compact

  input = capture(&block) || ActiveSupport::SafeBuffer.new

  input = attach_input(options, :prepend) + input + attach_input(options, :append)
  input << generate_error(name)
  options.present? &&
    input = tag.div(input, class: ["input-group", options[:input_group_class]].compact)
  input
end

#static_classObject



86
87
88
# File 'lib/bootstrap_form/helpers/bootstrap.rb', line 86

def static_class
  "form-control-plaintext"
end

#static_control(*args) ⇒ Object



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

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
  )

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

  text_field_with_bootstrap(name, static_options)
end