Skip to content

Commit

Permalink
fixed code repetition in simple_form bootstrap config template
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaipr committed Mar 29, 2016
1 parent 13d8bb6 commit 19a8ca6
Showing 1 changed file with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,45 @@ def html5_placeholder(wrapper)
wrapper.use :placeholder
end

def html5_readonly_label(input)
input.use :html5
input.optional :readonly
input.use :label, class: 'col-sm-5 control-label'
end

def input(input)
input.wrapper tag: 'div', class: 'input-group col-sm-12' do |append|
append.use :input, class: 'form-control'
end
input.use :error, wrap_with: { tag: 'span', class: 'help-block' }
input.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end

def form_optional(form)
form.optional :maxlength
form.optional :pattern
form.optional :min_max
form.optional :readonly
end

def form_input_error_hint(form)
form.use :input, class: 'form-control'
form.use :error, wrap_with: { tag: 'span', class: 'help-block' }
form.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end

SimpleForm.setup do |config|
config.error_notification_class = 'alert alert-danger'
config.button_class = 'btn btn-success'
config.boolean_label_class = nil

config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
html5_placeholder b
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
form_optional b

b.use :label, class: 'control-label'

b.use :input, class: 'form-control'
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
form_input_error_hint b
end

config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
Expand Down Expand Up @@ -84,10 +107,7 @@ def html5_placeholder(wrapper)
end

config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly

b.use :label, class: 'col-sm-5 control-label'
html5_readonly_label b

b.wrapper tag: 'div', class: 'col-sm-7' do |ba|
ba.use :input
Expand All @@ -97,10 +117,7 @@ def html5_placeholder(wrapper)
end

config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.optional :readonly

b.use :label, class: 'col-sm-5 control-label'
html5_readonly_label b

b.wrapper tag: 'div', class: 'col-sm-7' do |ba|
ba.use :input
Expand All @@ -111,27 +128,19 @@ def html5_placeholder(wrapper)

config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
html5_placeholder b
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
form_optional b

b.use :label, class: 'sr-only'

b.use :input, class: 'form-control'
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
form_input_error_hint b
end

config.wrappers :vertical_input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
html5_placeholder b
b.use :label, class: 'control-label'

b.wrapper tag: 'div' do |ba|
ba.wrapper tag: 'div', class: 'input-group col-sm-12' do |append|
append.use :input, class: 'form-control'
end
ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
input ba
end
end

Expand All @@ -140,11 +149,7 @@ def html5_placeholder(wrapper)
b.use :label, class: 'col-sm-3 control-label'

b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
ba.wrapper tag: 'div', class: 'input-group col-sm-12' do |append|
append.use :input, class: 'form-control'
end
ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
input ba
end
end

Expand All @@ -154,9 +159,9 @@ def html5_placeholder(wrapper)
# buttons and other elements.
config.default_wrapper = :vertical_form
config.wrapper_mappings = {
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean,
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean,
}
end

0 comments on commit 19a8ca6

Please sign in to comment.