Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move errors inside input-group and form-group. #434

Merged
merged 24 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
42d2c5a
Test case for #417.
lcreid Feb 3, 2018
d7ef709
Undoing mess created on wrong branch.
lcreid Feb 3, 2018
187aa69
Remove test that accidentally leaked in from another branch.
lcreid Feb 4, 2018
b25ab77
Working but needs cleanup #417.
lcreid Feb 4, 2018
463486e
Partially cleaned up.
lcreid Feb 4, 2018
9e66706
Remove unneeded method and parameters.
lcreid Feb 4, 2018
e83bf64
Test selects error.
lcreid Feb 5, 2018
a2b4586
Test selects error.
lcreid Feb 5, 2018
e48451d
time_zone_select works for new error placement.
lcreid Feb 5, 2018
b2d832a
Add tests for date, time, datetime selects.
lcreid Feb 5, 2018
073a1e3
Add tests for date, time, datetime selects.
lcreid Feb 5, 2018
844e133
date, time, datetime tests pass with old implementation.
lcreid Feb 5, 2018
ebab68a
Merge branch 'new-builder' into 417-input-group
lcreid Feb 5, 2018
0a73344
Date, time, and datetime for new implementation.
lcreid Feb 5, 2018
365dbf2
Tests for file and collection selects.
lcreid Feb 5, 2018
5cc7869
Merge branch 'error-tests' into 417-input-group
lcreid Feb 5, 2018
a15d06d
File and collection selects with new implementation.
lcreid Feb 5, 2018
d12e146
Merge branch 'master' into 417-input-group
lcreid Feb 15, 2018
3983dbb
Merge branch 'master' into 417-input-group
lcreid Feb 18, 2018
4d7248c
Keep tests but revert lib to try another approach.
lcreid Feb 18, 2018
7c53f24
form_group tests passing.
lcreid Feb 18, 2018
24def25
Clean up and add back commented test.
lcreid Feb 18, 2018
f60ed6c
checkbox and radio button tests still have old behaviour.
lcreid Feb 18, 2018
c0aaf19
Clean up and block append and prepend.
lcreid Feb 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def initialize(object_name, object, template, options)

define_method(with_method_name) do |name, options = {}|
form_group_builder(name, options) do
prepend_and_append_input(options) do
send(without_method_name, name, options)
end
send(without_method_name, name, options)
end
end

Expand All @@ -52,6 +50,7 @@ def initialize(object_name, object, template, options)
without_method_name = "#{method_name}_without_bootstrap"

define_method(with_method_name) do |name, options = {}, html_options = {}|
prevent_prepend_and_append!(options)
form_group_builder(name, options, html_options) do
content_tag(:div, send(without_method_name, name, options, html_options), class: control_specific_class(method_name))
end
Expand All @@ -61,6 +60,7 @@ def initialize(object_name, object, template, options)
end

def file_field_with_bootstrap(name, options = {})
prevent_prepend_and_append!(options)
options = options.reverse_merge(control_class: 'form-control-file')
form_group_builder(name, options) do
file_field_without_bootstrap(name, options)
Expand All @@ -71,15 +71,14 @@ def file_field_with_bootstrap(name, options = {})

def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block)
form_group_builder(method, options, html_options) do
prepend_and_append_input(options) do
select_without_bootstrap(method, choices, options, html_options, &block)
end
select_without_bootstrap(method, choices, options, html_options, &block)
end
end

bootstrap_method_alias :select

def collection_select_with_bootstrap(method, collection, value_method, text_method, options = {}, html_options = {})
prevent_prepend_and_append!(options)
form_group_builder(method, options, html_options) do
collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options)
end
Expand All @@ -88,6 +87,7 @@ def collection_select_with_bootstrap(method, collection, value_method, text_meth
bootstrap_method_alias :collection_select

def grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
prevent_prepend_and_append!(options)
form_group_builder(method, options, html_options) do
grouped_collection_select_without_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
end
Expand All @@ -96,6 +96,7 @@ def grouped_collection_select_with_bootstrap(method, collection, group_method, g
bootstrap_method_alias :grouped_collection_select

def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {})
prevent_prepend_and_append!(options)
form_group_builder(method, options, html_options) do
time_zone_select_without_bootstrap(method, priority_zones, options, html_options)
end
Expand All @@ -104,6 +105,7 @@ def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {},
bootstrap_method_alias :time_zone_select

def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block)
prevent_prepend_and_append!(options)
options = options.symbolize_keys!
check_box_options = options.except(:label, :label_class, :help, :inline, :custom, :hide_label, :skip_label)
check_box_classes = [check_box_options[:class]]
Expand Down Expand Up @@ -164,6 +166,7 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_
bootstrap_method_alias :check_box

def radio_button_with_bootstrap(name, value, *args)
prevent_prepend_and_append!(options)
options = args.extract_options!.symbolize_keys!
radio_options = options.except(:label, :label_class, :help, :inline, :custom, :hide_label, :skip_label)
radio_classes = [options[:class]]
Expand Down Expand Up @@ -210,6 +213,7 @@ def radio_button_with_bootstrap(name, value, *args)
bootstrap_method_alias :radio_button

def collection_check_boxes_with_bootstrap(*args)
prevent_prepend_and_append!(options)
html = inputs_collection(*args) do |name, value, options|
options[:multiple] = true
check_box(name, options, value, nil)
Expand All @@ -220,6 +224,7 @@ def collection_check_boxes_with_bootstrap(*args)
bootstrap_method_alias :collection_check_boxes

def collection_radio_buttons_with_bootstrap(*args)
prevent_prepend_and_append!(options)
inputs_collection(*args) do |name, value, options|
radio_button(name, value, options)
end
Expand All @@ -235,10 +240,9 @@ def form_group(*args, &block)
options[:class] << " row" if get_group_layout(options[:layout]) == :horizontal
options[:class] << " #{feedback_class}" if options[:icon]

content_tag(:div, options.except(:id, :label, :help, :icon, :label_col, :control_col, :layout)) do
content_tag(:div, options.except(:append, :id, :label, :help, :icon, :input_group_class, :label_col, :control_col, :layout, :prepend)) do
label = generate_label(options[:id], name, options[:label], options[:label_col], options[:layout]) if options[:label]
control = capture(&block).to_s
control.concat(generate_help(name, options[:help]).to_s)
control = prepend_and_append_input(name, options, &block).to_s

if get_group_layout(options[:layout]) == :horizontal
control_class = options[:control_col] || control_col
Expand Down Expand Up @@ -366,6 +370,10 @@ def form_group_builder(method, options, html_options = nil)
class: wrapper_class
}

form_group_options[:append] = options.delete(:append) if options[:append]
form_group_options[:prepend] = options.delete(:prepend) if options[:prepend]
form_group_options[:input_group_class] = options.delete(:input_group_class) if options[:input_group_class]

if wrapper_options.is_a?(Hash)
form_group_options.merge!(wrapper_options)
end
Expand Down
14 changes: 12 additions & 2 deletions lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,28 @@ def custom_control(*args, &block)
form_group_builder(name, options, &block)
end

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

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

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

# Some helpers don't currently accept prepend and append. However, it's not
# clear if that's corrent. In the meantime, strip to options before calling
# methods that don't accept prepend and append.
def prevent_prepend_and_append!(options)
options.delete(:append)
options.delete(:prepend)
end

def input_group_content(content)
return content if content.match(/btn/)
content_tag(:span, content, class: 'input-group-text')
Expand Down
25 changes: 25 additions & 0 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ class BootstrapFormGroupTest < ActionView::TestCase
assert_equivalent_xml expected, @builder.text_field(:email, prepend: '$', append: '.00')
end

test "adding both prepend and append text with validation error" do
@user.email = nil
assert @user.invalid?

expected = <<-HTML.strip_heredoc
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form">
<input name="utf8" type="hidden" value="&#x2713;"/>
<div class="form-group">
<label class="required" for="user_email">Email</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">$</div>
</div>
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
<div class="input-group-append">
<span class="input-group-text">.00</span>
</div>
<div class="invalid-feedback">can't be blank, is too short (minimum is 5 characters)</span>
</div>
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.text_field :email, prepend: '$', append: '.00' }
end

test "help messages for default forms" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
Expand Down