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

Don't use concat when not necessary #660

Merged
merged 2 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion lib/bootstrap_form/components/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def label_layout_classes(custom_label_col, group_layout)

def label_text(name, options)
if label_errors && error?(name)
(options[:text] || object.class.human_attribute_name(name)).to_s.concat(" #{get_error_messages(name)}")
(options[:text] || object.class.human_attribute_name(name)).to_s + " #{get_error_messages(name)}"
else
options[:text] || object&.class.try(:human_attribute_name, name)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap_form/components/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_error_messages(name)
next unless a.is_a?(ActiveRecord::Reflection::BelongsToReflection)
next unless a.foreign_key == name.to_s

messages.concat object.errors[association_name]
messages << object.errors[association_name]
end
messages.join(", ")
end
Expand Down
15 changes: 9 additions & 6 deletions lib/bootstrap_form/form_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ def form_group_content_tag(name, field_name, without_field_name, options, html_o
end
end

def form_group_content(label, help_text, options, &block)
def form_group_content(label, help_text, options, &block) # rubocop:disable Metrics/AbcSize
label ||= ActiveSupport::SafeBuffer.new
if group_layout_horizontal?(options[:layout])
concat(label) << tag.div(capture(&block) + help_text, class: form_group_control_class(options))
label + tag.div(capture(&block) + help_text, class: form_group_control_class(options))
else
content = ActiveSupport::SafeBuffer.new
# Floating labels need to be rendered after the field
concat(label) unless options[:floating]
concat(capture(&block))
concat(label) if options[:floating]
concat(help_text) if help_text
content << label unless options[:floating]
content << capture(&block)
content << label if options[:floating]
content << help_text if help_text
content
end
end

Expand Down
11 changes: 5 additions & 6 deletions lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ def alert_message(title, options={})
if options[:error_summary] == false
title
else
concat tag.p title
concat error_summary
tag.p(title) + error_summary
end
end
end

def error_summary
return unless object.errors.any?

tag.ul class: "rails-bootstrap-forms-error-summary" do
object.errors.full_messages.each do |error|
concat tag.li(error)
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
Expand Down Expand Up @@ -88,7 +87,7 @@ def prepend_and_append_input(name, options, &block)
input = capture(&block) || ActiveSupport::SafeBuffer.new

input = attach_input(options, :prepend) + input + attach_input(options, :append)
input += generate_error(name)
input << generate_error(name)
options.present? &&
input = tag.div(input, class: ["input-group", options[:input_group_class]].compact)
input
Expand Down
4 changes: 2 additions & 2 deletions lib/bootstrap_form/inputs/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def check_box_with_bootstrap(name, options={}, checked_value="1", unchecked_valu

tag.div(class: check_box_wrapper_class(options), **options[:wrapper].to_h.except(:class)) do
html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value)
html.concat(check_box_label(name, options, checked_value, &block)) unless options[:skip_label]
html.concat(generate_error(name)) if options[:error_message]
html << check_box_label(name, options, checked_value, &block) unless options[:skip_label]
html << generate_error(name) if options[:error_message]
html
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bootstrap_form/inputs/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def radio_button_with_bootstrap(name, value, *args)
wrapper_attributes[:class] = radio_button_wrapper_class(options)
tag.div(**wrapper_attributes) do
html = radio_button_without_bootstrap(name, value, radio_button_options(name, options))
html.concat(radio_button_label(name, value, options)) unless options[:skip_label]
html.concat(generate_error(name)) if options[:error_message]
html << radio_button_label(name, value, options) unless options[:skip_label]
html << generate_error(name) if options[:error_message]
html
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ class BootstrapFormGroupTest < ActionView::TestCase
output = @builder.form_group :email do
html = '<p class="form-control-plaintext">Bar</p>'.html_safe
unless @user.errors[:email].empty?
html.concat(tag.div(@user.errors[:email].join(", "), class: "invalid-feedback",
style: "display: block;"))
html << tag.div(@user.errors[:email].join(", "), class: "invalid-feedback",
style: "display: block;")
end
html
end
Expand All @@ -413,9 +413,9 @@ class BootstrapFormGroupTest < ActionView::TestCase

output = bootstrap_form_for(@user) do |f|
f.form_group :email do
f.radio_button(:misc, "primary school")
.concat(f.radio_button(:misc, "high school"))
.concat(f.radio_button(:misc, "university", error_message: true))
concat(f.radio_button(:misc, "primary school"))
concat(f.radio_button(:misc, "high school"))
concat(f.radio_button(:misc, "university", error_message: true))
end
end

Expand Down Expand Up @@ -528,7 +528,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
"Hallo"
end

output += @horizontal_builder.text_field(:email)
output << @horizontal_builder.text_field(:email)

expected = <<~HTML
<div class="mb-3 row">
Expand Down
48 changes: 24 additions & 24 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class BootstrapFormTest < ActionView::TestCase

collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user) do |f|
f.email_field(:email, layout: :horizontal)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :horizontal))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :horizontal))
concat(f.email_field(:email, layout: :horizontal))
concat(f.check_box(:terms, label: "I agree to the terms"))
concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :horizontal))
concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :horizontal))
end

assert_equivalent_xml expected, actual
Expand Down Expand Up @@ -102,10 +102,10 @@ class BootstrapFormTest < ActionView::TestCase

collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user) do |f|
f.email_field(:email, layout: :inline)
.concat(f.check_box(:terms, label: "I agree to the terms", inline: true))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :inline))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :inline))
concat(f.email_field(:email, layout: :inline))
concat(f.check_box(:terms, label: "I agree to the terms", inline: true))
concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :inline))
concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :inline))
end

assert_equivalent_xml expected, actual
Expand Down Expand Up @@ -164,10 +164,10 @@ class BootstrapFormTest < ActionView::TestCase

collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :inline) do |f|
f.email_field(:email)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]]))
concat(f.email_field(:email))
concat(f.check_box(:terms, label: "I agree to the terms"))
concat(f.collection_radio_buttons(:misc, collection, :id, :street))
concat(f.select(:status, [["activated", 1], ["blocked", 2]]))
end

assert_equivalent_xml expected, actual
Expand Down Expand Up @@ -215,10 +215,10 @@ class BootstrapFormTest < ActionView::TestCase

collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :horizontal) do |f|
f.email_field(:email)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]]))
concat(f.email_field(:email))
concat(f.check_box(:terms, label: "I agree to the terms"))
concat(f.collection_radio_buttons(:misc, collection, :id, :street))
concat(f.select(:status, [["activated", 1], ["blocked", 2]]))
end

assert_equivalent_xml expected, actual
Expand Down Expand Up @@ -260,10 +260,10 @@ class BootstrapFormTest < ActionView::TestCase

collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :horizontal) do |f|
f.email_field(:email, layout: :default)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :default))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :default))
concat(f.email_field(:email, layout: :default))
concat(f.check_box(:terms, label: "I agree to the terms"))
concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :default))
concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :default))
end

assert_equivalent_xml expected, actual
Expand Down Expand Up @@ -306,10 +306,10 @@ class BootstrapFormTest < ActionView::TestCase

collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :horizontal) do |f|
f.email_field(:email, layout: :inline)
.concat(f.check_box(:terms, label: "I agree to the terms", inline: true))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :inline))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :inline))
concat(f.email_field(:email, layout: :inline))
concat(f.check_box(:terms, label: "I agree to the terms", inline: true))
concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :inline))
concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :inline))
end

assert_equivalent_xml expected, actual
Expand Down