Skip to content

Helpers Without Underscore in Rails 8, e.g. checkbox textarea #759

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions demo/app/views/bootstrap/form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
<%= form.password_field :password, placeholder: "Password" %>
<%= form.select :status, [['activated', 1], ['blocked', 2]], prompt: "Please Select" %>
<%= form.text_area :comments %>
<%= form.check_box :terms, label: "Agree to Terms" %>
<%= form.collection_check_boxes :checkboxes, @collection, :id, :street %>
<%= form.textarea :comments %>
<%= form.checkbox :terms, label: "Agree to Terms" %>
<%= form.collection_checkboxes :checkboxes, @collection, :id, :street %>
<%= form.collection_radio_buttons :radio_buttons, @collection, :id, :street %>
<%= form.file_field :file %>
<%= form.datetime_select :created_at, include_blank: true %>
Expand All @@ -23,7 +23,7 @@
<%= form.alert_message "This is an alert" %>
<%= form.error_summary %>
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
<%= form.collection_check_boxes :misc, @collection, :id, :street %>
<%= form.collection_checkboxes :misc, @collection, :id, :street %>
<%= form.submit %>
<% end %>
<% end %>
Expand All @@ -34,8 +34,8 @@
<%= bootstrap_form_for @user, layout: :inline do |form| %>
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
<%= form.password_field :password, placeholder: "Password" %>
<%= form.check_box :terms, label: "Agree to Terms" %>
<%= form.collection_check_boxes :misc, @collection, :id, :street %>
<%= form.checkbox :terms, label: "Agree to Terms" %>
<%= form.collection_checkboxes :misc, @collection, :id, :street %>
<%= form.submit %>
<% end %>
<% end %>
Expand All @@ -46,9 +46,9 @@
<%= bootstrap_form_for @user, url: "/" do |form| %>
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else" %>
<%= form.password_field :password, placeholder: "Password" %>
<%= form.check_box :terms, label: "Agree to Terms" %>
<%= form.collection_check_boxes :misc, @collection, :id, :street %>
<%= form.rich_text_area(:life_story) %>
<%= form.checkbox :terms, label: "Agree to Terms" %>
<%= form.collection_checkboxes :misc, @collection, :id, :street %>
<%= form.rich_textarea(:life_story) %>
<%= form.submit %>
<% end %>
<% end %>
Expand All @@ -60,7 +60,7 @@
<%= form.email_field :email, placeholder: "Enter Email", label: "Email address", help: "We'll never share your email with anyone else", floating: true %>
<%= form.password_field :password, placeholder: "Password", floating: true %>
<%= form.text_field :misc, floating: true %>
<%= form.text_area :comments, floating: true %>
<%= form.textarea :comments, floating: true %>
<%= form.select :status, [["Active", 1], ["Inactive", 2]], include_blank: "Select a value", floating: true %>
<%= form.submit %>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions lib/bootstrap_form/inputs/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def check_box_with_bootstrap(name, options={}, checked_value="1", unchecked_valu
end

bootstrap_alias :check_box
alias_method :checkbox_with_bootstrap, :check_box_with_bootstrap if Rails::VERSION::MAJOR >= 8
bootstrap_alias :checkbox if Rails::VERSION::MAJOR >= 8
end

private
Expand Down
25 changes: 3 additions & 22 deletions lib/bootstrap_form/inputs/collection_check_boxes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module CollectionCheckBoxes
include Base
include InputsCollection

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

bootstrap_alias :collection_check_boxes

if Rails::VERSION::MAJOR < 7
def field_name(method, *methods, multiple: false, index: @options[:index])
object_name = @options.fetch(:as) { @object_name }

field_name_shim(object_name, method, *methods, index: index, multiple: multiple)
end

private

def field_name_shim(object_name, method_name, *method_names, multiple: false, index: nil)
names = method_names.map! { |name| "[#{name}]" }.join
if object_name.blank?
"#{method_name}#{names}#{'[]' if multiple}"
elsif index
"#{object_name}[#{index}][#{method_name}]#{names}#{'[]' if multiple}"
else
"#{object_name}[#{method_name}]#{names}#{'[]' if multiple}"
end
end
end
alias_method :collection_checkboxes_with_bootstrap, :collection_check_boxes_with_bootstrap if Rails::VERSION::MAJOR >= 8
bootstrap_alias :collection_checkboxes if Rails::VERSION::MAJOR >= 8
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/bootstrap_form/inputs/rich_text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def rich_text_area_with_bootstrap(name, options={})
end

bootstrap_alias :rich_text_area
alias_method :rich_textarea, :rich_text_area if Rails::VERSION::MAJOR >= 8
bootstrap_alias :rich_textarea if Rails::VERSION::MAJOR >= 8
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/bootstrap_form/inputs/text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module TextArea

included do
bootstrap_field :text_area
alias_method :textarea_with_bootstrap, :text_area_with_bootstrap if Rails::VERSION::MAJOR >= 8
bootstrap_field :textarea if Rails::VERSION::MAJOR >= 8
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions test/bootstrap_checkbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ class BootstrapCheckboxTest < ActionView::TestCase
label: "This is a checkbox collection", help: "With a help!")
end

if Rails::VERSION::MAJOR >= 8
test "collection_checkboxes renders the form_group correctly" do
collection = [Address.new(id: 1, street: "Foobar")]
expected = <<~HTML
<input #{autocomplete_attr} id="user_misc" name="user[misc][]" type="hidden" value="" />
<div class="mb-3">
<label class="form-label" for="user_misc">This is a checkbox collection</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc][]" type="checkbox" value="1" />
<label class="form-check-label" for="user_misc_1">Foobar</label>
</div>
<small class="form-text text-muted">With a help!</small>
</div>
HTML

assert_equivalent_html expected, @builder.collection_checkboxes(:misc, collection, :id, :street,
label: "This is a checkbox collection", help: "With a help!")
end
end

test "collection_check_boxes renders multiple checkboxes correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
Expand Down Expand Up @@ -680,4 +700,19 @@ class BootstrapCheckboxTest < ActionView::TestCase
HTML
assert_equivalent_html expected, @builder.check_box(:misc)
end

if Rails::VERSION::MAJOR >= 8
test "checkbox alias works" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" extra="extra arg" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.checkbox(:terms, label: "I agree to the terms", extra: "extra arg")
end
end
end
12 changes: 12 additions & 0 deletions test/bootstrap_fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ class BootstrapFieldsTest < ActionView::TestCase
assert_equivalent_html expected, @builder.text_area(:comments)
end

if Rails::VERSION::MAJOR >= 8
test "text areas are aliased" do
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_comments">Comments</label>
<textarea class="form-control" id="user_comments" name="user[comments]">\nmy comment</textarea>
</div>
HTML
assert_equivalent_html expected, @builder.textarea(:comments)
end
end

test "text areas are wrapped correctly using form_with" do
expected = <<~HTML
<div class="mb-3">
Expand Down
16 changes: 16 additions & 0 deletions test/bootstrap_rich_text_area_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ class BootstrapRichTextAreaTest < ActionView::TestCase
assert_equivalent_html expected, form_with_builder.rich_text_area(:life_story, extra: "extra arg")
end

if Rails::VERSION::MAJOR >= 8
test "rich text areas are aliased" do
expected = nil
with_stub_token do
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_life_story">Life story</label>
<input autocomplete="off" type="hidden" name="user[life_story]" id="user_life_story_trix_input_user"/>
<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"/>
</div>
HTML
end
assert_equivalent_html expected, form_with_builder.rich_textarea(:life_story, extra: "extra arg")
end
end

def data_blob_url_template
"http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename"
end
Expand Down
Loading