Class: Bootstrap4FormBuilder::FormBuilder::BootstrapBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/bootstrap4_form_builder/form_builder.rb

Constant Summary collapse

FIELD_HELPERS =
%w{color_field date_field datetime_field datetime_local_field
email_field month_field number_field password_field phone_field
range_field search_field telephone_field text_area text_field time_field
url_field week_field}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ BootstrapBuilder

Returns a new instance of BootstrapBuilder.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 14

def initialize(object_name, object, template, options)
  @layout = options[:layout]
  @label_col = options[:label_col]
  @control_col = options[:control_col]
  @inline_errors = options[:inline_errors].nil? ? true : options[:inline_errors]
  @inline_error_class = options[:inline_error_class]
  #puts "Inline Errors: #{@inline_errors}"
  #Could be done in helper.
  if inline_layout?
    options.reverse_merge! html: {}
    options[:html][:class] = [options[:html][:class], inline_class].compact.join(" ")
  end
    
  super
end

Instance Attribute Details

#control_colObject (readonly)

Returns the value of attribute control_col.



7
8
9
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 7

def control_col
  @control_col
end

#label_colObject (readonly)

Returns the value of attribute label_col.



7
8
9
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 7

def label_col
  @label_col
end

#layoutObject (readonly)

Returns the value of attribute layout.



7
8
9
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 7

def layout
  @layout
end

Instance Method Details

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



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 134

def alert_message(title, options = {})
  alert_class = options[:class] || 'alert alert-danger'
  display_errors = options.delete(:error_summary)
    
  if object.respond_to?(:errors) && object.errors.full_messages.any?
     :div, class: alert_class do
      content = [(:p, title)]
      content << error_summary if display_errors
      content.join.html_safe
    end
  end
end

#check_box(name, options = {}, checked_value = "1", unchecked_value = "0", &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 58

def check_box(name, options = {}, checked_value = "1", unchecked_value = "0", &block)
  options = options.symbolize_keys!
  check_box_options = options.except(:label, :label_class, :inline)
  label_class = [options[:label_class]]
  
  temporarily_disable_field_error_proc do
    html = super(name, check_box_options, checked_value, unchecked_value)
    label_content = block_given? ? capture(&block) : options[:label]
    html.concat(" ").concat(label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize)

    label_name = name
  
    label_class = options[:label_class] 
  
    if options[:inline]
      label_class = ["checkbox-inline", label_class].compact.join(' ')
      label(label_name, html, class: label_class)
    else
      (:div, class: "checkbox") do
        label(label_name, html, class: label_class)
      end
    end
  end
end

#error_summaryObject



147
148
149
150
151
152
153
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 147

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

#form_group(*args, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 102

def form_group(*args, &block)
  options = args.extract_options!
  name = args.first
    
  options[:class] = ["form-group", options[:class]]
  options[:class] << label_error_class if has_errors?(name)
  options[:class] = options[:class].compact.join(" ")
    
  (:div, options.except(:label, :label_class)) do
    label_class = [@label_col, options[:label_class]].compact.join(' ')
    
    label = @template.label_tag(nil, options[:label], class: label_class) if options[:label]
    controls = capture(&block).to_s

    error_help = generate_error_help(name)
    controls = (:div, class: @control_col) { concat(controls) } if gridded_form?


    concat(label).concat(controls).concat(error_help)
  end
end

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



129
130
131
132
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 129

def primary(name = nil, options = {})
  options[:class] = ["btn btn-primary", options[:class]].compact.join(" ")
  method(:submit).super_method.call(name, options)
end

#radio_button(name, value, *args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 83

def radio_button(name, value, *args)
  options = args.extract_options!.symbolize_keys!
  args << options.except(:label, :label_class, :inline)
  label_class = [options[:label_class]]
  
  temporarily_disable_field_error_proc do
    html = super(name, value, *args) + " " + value.humanize

    if options[:inline]
      label_class = ["radio-inline", label_class].compact.join(" ")
      label(value, html, value: value, class: label_class)
    else
      (:div, class: "radio") do
        label(value, html, value: value, class: label_class)
      end
    end
  end
end

#standard_html(type, method, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 37

def standard_html(type, method, options = {})
  form_group_builder(method, options) do
    content = []
    options[:class] = [control_class, options[:class]].compact.join(" ")
    
    
    temporarily_disable_field_error_proc do
      content << generate_label(method, options)
      if gridded_form?
        content << (:div, class: @control_col) do
          @template.send(type, object_name, method, options)
        end
      else
        content << @template.send(type, object_name, method, options)
      end
    end
    content << generate_error_help(method, options)
    content.compact.join.html_safe
  end
end

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



124
125
126
127
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 124

def submit(name = nil, options = {})
  options[:class] = ["btn btn-default", options[:class]].compact.join(" ")
  super(name, options)
end