Skip to content

Commit

Permalink
Add bootstrap_fields_for form helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
donv committed Sep 12, 2023
1 parent 3867a40 commit 28def01
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bootstrap_form/action_view_extensions/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def bootstrap_form_tag(options={}, &block)
bootstrap_form_for("", options, &block)
end

def bootstrap_fields_for(record_name, record_object = nil, options = {}, &block)
def bootstrap_fields_for(record_name, record_object=nil, options={}, &block)
options[:builder] = BootstrapForm::FormBuilder
fields_for(record_name, record_object, options, &block)
end
Expand Down
49 changes: 49 additions & 0 deletions test/bootstrap_fields_for_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative "test_helper"

class BootstrapFieldsForTest < ActionView::TestCase
include BootstrapForm::ActionViewExtensions::FormHelper

setup :setup_test_fixture

test "bootstrap_fields_for helper works for associations" do
@user.address = Address.new(street: "123 Main Street")

output = bootstrap_form_for(@user) do |_f|
bootstrap_fields_for @user.address do |af|
af.text_field(:street)
end
end

expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label" for="address_street">Street</label>
<input class="form-control" id="address_street" name="address[street]" type="text" value="123 Main Street" />
</div>
</form>
HTML
assert_equivalent_html expected, output
end

test "bootstrap_form_for helper works for serialized hash attributes" do
@user.preferences = { "favorite_color" => "cerulean" }

output = bootstrap_form_for(@user) do |_f|
bootstrap_fields_for :preferences do |builder|
builder.text_field :favorite_color, value: @user.preferences["favorite_color"]
end
end

expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label" for="preferences_favorite_color">Favorite color</label>
<input class="form-control" id="preferences_favorite_color" name="preferences[favorite_color]" type="text" value="cerulean" />
</div>
</form>
HTML
assert_equivalent_html expected, output
end
end

0 comments on commit 28def01

Please sign in to comment.