-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
bootstrap_fields_for
form helper.
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="✓"/>' 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="✓"/>' 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 |