From 6eba354ae140a82698c1f77cb821dc6cb6e87703 Mon Sep 17 00:00:00 2001 From: Daniel Fone Date: Mon, 4 Jul 2016 11:19:26 +1200 Subject: [PATCH] Prevent mutating (possibly) frozen strings Currently the `#form_group` method attempts to mutate the `:control_col` option if no label is supplied. Since this option is passed by the user, it may be a frozen string literal. Instead, we simply avoid mutating the supplied string. As we approach Ruby 3 and frozen string literals become more common, this is more likely to fail. --- CHANGELOG.md | 1 + lib/bootstrap_form/form_builder.rb | 4 ++-- test/bootstrap_form_group_test.rb | 10 +++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9d1d416e..cadb456a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Bugfixes: - Minor README corrections (#184, @msmithstubbs) - Fix `alias_method_chain` deprecation warnings when using Rails 5 + - Allow `form_group` to work with frozen string options Features: diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index bd58dbf02..775a360fd 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -198,10 +198,10 @@ def form_group(*args, &block) control.concat(generate_icon(options[:icon])) if options[:icon] if get_group_layout(options[:layout]) == :horizontal - control_class = (options[:control_col] || control_col.clone) + control_class = options[:control_col] || control_col unless options[:label] control_offset = offset_col(/([0-9]+)$/.match(options[:label_col] || @label_col)) - control_class.concat(" #{control_offset}") + control_class = "#{control_class} #{control_offset}" end control = content_tag(:div, control, class: control_class) end diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index c6620e57b..b4dd9c80b 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -267,5 +267,13 @@ def setup expected = %{

Bar

} assert_equal expected, output - end + end + + test "non-default column span on form isn't mutated" do + frozen_horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, { layout: :horizontal, label_col: "col-sm-3".freeze, control_col: "col-sm-9".freeze }) + output = frozen_horizontal_builder.form_group { 'test' } + + expected = %{
test
} + assert_equal expected, output + end end