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

add custom_control #289

Merged
merged 3 commits into from
Jan 14, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Bugfixes:

Features:
- Your contribution here!
- Add a FormBuilder#custom_control helper [#289](https://github.com/bootstrap-ruby/rails-bootstrap-forms/pull/289)

## [2.5.0][] (2016-08-12)

Expand Down
7 changes: 7 additions & 0 deletions lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ def static_control(*args, &block)
end
end

def custom_control(*args, &block)
options = args.extract_options!
name = args.first

form_group_builder(name, options, &block)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this accomplish the same thing? Do we need the extract_options! and capture?

def custom_control(name, options={}, &block)
   form_group_builder(name, options, &block)
end

Copy link
Contributor Author

@ThomasSevestre ThomasSevestre Dec 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract_options! is not needed. I've copied static_control to have the same ergonomy. It allows this kind of things:

= custom_control label: 'MyLabel' do
  ...

capture is not needed since it is already done in form_group

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see... So in order to support the example you described (custom_control label: 'MyLabel'), then we do need extract_options!. Right?

capture is not needed since it is already done in form_group

Agreed. Can you update the PR to remove it?


def prepend_and_append_input(options, &block)
options = options.extract!(:prepend, :append, :input_group_class)
input_group_class = ["input-group", options[:input_group_class]].compact.join(' ')
Expand Down
27 changes: 27 additions & 0 deletions test/bootstrap_other_components_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ def setup
assert_equal expected, output
end

test "custom control does't wrap given block in a p tag" do
output = @horizontal_builder.custom_control :email do
"this is a test"
end

expected = %{<div class="form-group"><label class="control-label col-sm-2 required" for="user_email">Email</label><div class="col-sm-10">this is a test</div></div>}
assert_equal expected, output
end

test "custom control doesn't require an actual attribute" do
output = @horizontal_builder.custom_control nil, label: "My Label" do
"this is a test"
end

expected = %{<div class="form-group"><label class="control-label col-sm-2" for="user_">My Label</label><div class="col-sm-10">this is a test</div></div>}
assert_equal expected, output
end

test "custom control doesn't require a name" do
output = @horizontal_builder.custom_control label: "Custom Label" do
"Custom Control"
end

expected = %{<div class="form-group"><label class="control-label col-sm-2" for="user_">Custom Label</label><div class="col-sm-10">Custom Control</div></div>}
assert_equal expected, output
end

test "submit button defaults to rails action name" do
expected = %{<input class="btn btn-default" name="commit" type="submit" value="Create User" />}
assert_equal expected, @builder.submit
Expand Down