Skip to content

Commit 09d09d6

Browse files
authored
Merge pull request #759 from lcreid/744-checkbox-textarea
Helpers Without Underscore in Rails 8, e.g. checkbox textarea
2 parents f947b14 + 531c369 commit 09d09d6

File tree

8 files changed

+82
-32
lines changed

8 files changed

+82
-32
lines changed

demo/app/views/bootstrap/form.html.erb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
66
<%= form.password_field :password, placeholder: "Password" %>
77
<%= form.select :status, [['activated', 1], ['blocked', 2]], prompt: "Please Select" %>
8-
<%= form.text_area :comments %>
9-
<%= form.check_box :terms, label: "Agree to Terms" %>
10-
<%= form.collection_check_boxes :checkboxes, @collection, :id, :street %>
8+
<%= form.textarea :comments %>
9+
<%= form.checkbox :terms, label: "Agree to Terms" %>
10+
<%= form.collection_checkboxes :checkboxes, @collection, :id, :street %>
1111
<%= form.collection_radio_buttons :radio_buttons, @collection, :id, :street %>
1212
<%= form.file_field :file %>
1313
<%= form.datetime_select :created_at, include_blank: true %>
@@ -23,7 +23,7 @@
2323
<%= form.alert_message "This is an alert" %>
2424
<%= form.error_summary %>
2525
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
26-
<%= form.collection_check_boxes :misc, @collection, :id, :street %>
26+
<%= form.collection_checkboxes :misc, @collection, :id, :street %>
2727
<%= form.submit %>
2828
<% end %>
2929
<% end %>
@@ -34,8 +34,8 @@
3434
<%= bootstrap_form_for @user, layout: :inline do |form| %>
3535
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
3636
<%= form.password_field :password, placeholder: "Password" %>
37-
<%= form.check_box :terms, label: "Agree to Terms" %>
38-
<%= form.collection_check_boxes :misc, @collection, :id, :street %>
37+
<%= form.checkbox :terms, label: "Agree to Terms" %>
38+
<%= form.collection_checkboxes :misc, @collection, :id, :street %>
3939
<%= form.submit %>
4040
<% end %>
4141
<% end %>
@@ -46,9 +46,9 @@
4646
<%= bootstrap_form_for @user, url: "/" do |form| %>
4747
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
4848
<%= form.password_field :password, placeholder: "Password" %>
49-
<%= form.check_box :terms, label: "Agree to Terms" %>
50-
<%= form.collection_check_boxes :misc, @collection, :id, :street %>
51-
<%= form.rich_text_area(:life_story) %>
49+
<%= form.checkbox :terms, label: "Agree to Terms" %>
50+
<%= form.collection_checkboxes :misc, @collection, :id, :street %>
51+
<%= form.rich_textarea(:life_story) %>
5252
<%= form.submit %>
5353
<% end %>
5454
<% end %>
@@ -60,7 +60,7 @@
6060
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else", floating: true %>
6161
<%= form.password_field :password, placeholder: "Password", floating: true %>
6262
<%= form.text_field :misc, floating: true %>
63-
<%= form.text_area :comments, floating: true %>
63+
<%= form.textarea :comments, floating: true %>
6464
<%= form.select :status, [["Active", 1], ["Inactive", 2]], include_blank: "Select a value", floating: true %>
6565
<%= form.submit %>
6666
<% end %>

lib/bootstrap_form/inputs/check_box.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def check_box_with_bootstrap(name, options={}, checked_value="1", unchecked_valu
2020
end
2121

2222
bootstrap_alias :check_box
23+
alias_method :checkbox_with_bootstrap, :check_box_with_bootstrap if Rails::VERSION::MAJOR >= 8
24+
bootstrap_alias :checkbox if Rails::VERSION::MAJOR >= 8
2325
end
2426

2527
private

lib/bootstrap_form/inputs/collection_check_boxes.rb

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module CollectionCheckBoxes
77
include Base
88
include InputsCollection
99

10-
included do # rubocop:disable Metrics/BlockLength
10+
included do
1111
def collection_check_boxes_with_bootstrap(*args)
1212
html = inputs_collection(*args) do |name, value, options|
1313
options[:multiple] = true
@@ -21,27 +21,8 @@ def collection_check_boxes_with_bootstrap(*args)
2121
end
2222

2323
bootstrap_alias :collection_check_boxes
24-
25-
if Rails::VERSION::MAJOR < 7
26-
def field_name(method, *methods, multiple: false, index: @options[:index])
27-
object_name = @options.fetch(:as) { @object_name }
28-
29-
field_name_shim(object_name, method, *methods, index: index, multiple: multiple)
30-
end
31-
32-
private
33-
34-
def field_name_shim(object_name, method_name, *method_names, multiple: false, index: nil)
35-
names = method_names.map! { |name| "[#{name}]" }.join
36-
if object_name.blank?
37-
"#{method_name}#{names}#{'[]' if multiple}"
38-
elsif index
39-
"#{object_name}[#{index}][#{method_name}]#{names}#{'[]' if multiple}"
40-
else
41-
"#{object_name}[#{method_name}]#{names}#{'[]' if multiple}"
42-
end
43-
end
44-
end
24+
alias_method :collection_checkboxes_with_bootstrap, :collection_check_boxes_with_bootstrap if Rails::VERSION::MAJOR >= 8
25+
bootstrap_alias :collection_checkboxes if Rails::VERSION::MAJOR >= 8
4526
end
4627
end
4728
end

lib/bootstrap_form/inputs/rich_text_area.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def rich_text_area_with_bootstrap(name, options={})
1717
end
1818

1919
bootstrap_alias :rich_text_area
20+
alias_method :rich_textarea, :rich_text_area if Rails::VERSION::MAJOR >= 8
21+
bootstrap_alias :rich_textarea if Rails::VERSION::MAJOR >= 8
2022
end
2123
end
2224
end

lib/bootstrap_form/inputs/text_area.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module TextArea
88

99
included do
1010
bootstrap_field :text_area
11+
alias_method :textarea_with_bootstrap, :text_area_with_bootstrap if Rails::VERSION::MAJOR >= 8
12+
bootstrap_field :textarea if Rails::VERSION::MAJOR >= 8
1113
end
1214
end
1315
end

test/bootstrap_checkbox_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ class BootstrapCheckboxTest < ActionView::TestCase
184184
label: "This is a checkbox collection", help: "With a help!")
185185
end
186186

187+
if Rails::VERSION::MAJOR >= 8
188+
test "collection_checkboxes renders the form_group correctly" do
189+
collection = [Address.new(id: 1, street: "Foobar")]
190+
expected = <<~HTML
191+
<input #{autocomplete_attr} id="user_misc" name="user[misc][]" type="hidden" value="" />
192+
<div class="mb-3">
193+
<label class="form-label" for="user_misc">This is a checkbox collection</label>
194+
<div class="form-check">
195+
<input class="form-check-input" id="user_misc_1" name="user[misc][]" type="checkbox" value="1" />
196+
<label class="form-check-label" for="user_misc_1">Foobar</label>
197+
</div>
198+
<small class="form-text text-muted">With a help!</small>
199+
</div>
200+
HTML
201+
202+
assert_equivalent_html expected, @builder.collection_checkboxes(:misc, collection, :id, :street,
203+
label: "This is a checkbox collection", help: "With a help!")
204+
end
205+
end
206+
187207
test "collection_check_boxes renders multiple checkboxes correctly" do
188208
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
189209
expected = <<~HTML
@@ -680,4 +700,19 @@ class BootstrapCheckboxTest < ActionView::TestCase
680700
HTML
681701
assert_equivalent_html expected, @builder.check_box(:misc)
682702
end
703+
704+
if Rails::VERSION::MAJOR >= 8
705+
test "checkbox alias works" do
706+
expected = <<~HTML
707+
<div class="form-check mb-3">
708+
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
709+
<input class="form-check-input" extra="extra arg" id="user_terms" name="user[terms]" type="checkbox" value="1" />
710+
<label class="form-check-label" for="user_terms">
711+
I agree to the terms
712+
</label>
713+
</div>
714+
HTML
715+
assert_equivalent_html expected, @builder.checkbox(:terms, label: "I agree to the terms", extra: "extra arg")
716+
end
717+
end
683718
end

test/bootstrap_fields_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ class BootstrapFieldsTest < ActionView::TestCase
213213
assert_equivalent_html expected, @builder.text_area(:comments)
214214
end
215215

216+
if Rails::VERSION::MAJOR >= 8
217+
test "text areas are aliased" do
218+
expected = <<~HTML
219+
<div class="mb-3">
220+
<label class="form-label" for="user_comments">Comments</label>
221+
<textarea class="form-control" id="user_comments" name="user[comments]">\nmy comment</textarea>
222+
</div>
223+
HTML
224+
assert_equivalent_html expected, @builder.textarea(:comments)
225+
end
226+
end
227+
216228
test "text areas are wrapped correctly using form_with" do
217229
expected = <<~HTML
218230
<div class="mb-3">

test/bootstrap_rich_text_area_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ class BootstrapRichTextAreaTest < ActionView::TestCase
2121
assert_equivalent_html expected, form_with_builder.rich_text_area(:life_story, extra: "extra arg")
2222
end
2323

24+
if Rails::VERSION::MAJOR >= 8
25+
test "rich text areas are aliased" do
26+
expected = nil
27+
with_stub_token do
28+
expected = <<~HTML
29+
<div class="mb-3">
30+
<label class="form-label" for="user_life_story">Life story</label>
31+
<input autocomplete="off" type="hidden" name="user[life_story]" id="user_life_story_trix_input_user"/>
32+
<trix-editor class="trix-content form-control" extra="extra arg" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" id="user_life_story" input="user_life_story_trix_input_user"/>
33+
</div>
34+
HTML
35+
end
36+
assert_equivalent_html expected, form_with_builder.rich_textarea(:life_story, extra: "extra arg")
37+
end
38+
end
39+
2440
def data_blob_url_template
2541
"http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename"
2642
end

0 commit comments

Comments
 (0)