Skip to content

Commit

Permalink
@shivam091 #50 Fixed bug causing button helpers not to work with bloc…
Browse files Browse the repository at this point in the history
…k and options
  • Loading branch information
shivam091 committed Jul 1, 2023
1 parent ed0d530 commit ffa8611
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/rails_bootstrap_form/bootstrap_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
include RailsBootstrapForm::InputGroupBuilder
include RailsBootstrapForm::Inputs

delegate :capture, :concat, :tag, to: :@template
delegate :capture, :concat, :tag, :button_tag, to: :@template

attr_accessor :bootstrap_form_options

Expand Down
30 changes: 21 additions & 9 deletions lib/rails_bootstrap_form/helpers/buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ module Buttons
extend ActiveSupport::Concern

def self.included(base_class)
def render_button(value = nil, options = {}, &block)
value, options = nil, value if value.is_a?(Hash)
def button(value, options, &block)
bootstrap = bootstrap_form_options.scoped(options.delete(:bootstrap))

value, options = nil, value.merge(options) if value.is_a?(Hash)

add_css_class!(options, "btn")
add_css_class!(options, button_variant_class(options))

button_html = if (bootstrap.render_as_button? || block)
button(value, options, &block)
button_tag(value, options, &block)
else
submit(value, options)
end
Expand All @@ -26,18 +30,26 @@ def render_button(value = nil, options = {}, &block)
end

def secondary(value = nil, options = {}, &block)
add_css_class!(options, "btn btn-secondary")
render_button(value, options, &block)
button(value, options.merge!(variant: "secondary"), &block)
end

def primary(value = nil, options = {}, &block)
add_css_class!(options, "btn btn-primary")
render_button(value, options, &block)
button(value, options.merge!(variant: "primary"), &block)
end

def danger(value = nil, options = {}, &block)
add_css_class!(options, "btn btn-danger")
render_button(value, options, &block)
button(value, options.merge!(variant: "danger"), &block)
end
end

private

def button_variant_class(options)
case options.delete(:variant)
when "secondary" then "btn-secondary"
when "primary" then "btn-primary"
when "danger" then "btn-danger"
else ""
end
end
end
Expand Down
63 changes: 63 additions & 0 deletions spec/rails_bootstrap_form/helpers/buttons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
expect(actual).to match_html(expected)
end

it "renders as button helper when block is passed along with aditional options" do
expected = <<~HTML
<button name="button" type="submit" data-controller="test" class="btn btn-danger">Delete</button>
HTML

actual = form_builder.danger data: {controller: "test"} do
"Delete"
end

expect(actual).to match_html(expected)
end

it "adds additional class" do
expected = <<~HTML
<input type="submit" name="commit" value="Delete" class="custom-button btn btn-danger" data-disable-with="Delete">
Expand All @@ -74,5 +86,56 @@

expect(actual).to match_html(expected)
end

it "renders secondary button" do
expected = <<~HTML
<input type="submit" name="commit" value="Search" class="btn btn-secondary" data-disable-with="Search">
HTML

actual = form_builder.secondary "Search"

expect(actual).to match_html(expected)
end

it "renders primary button" do
expected = <<~HTML
<input type="submit" name="commit" value="Register" class="btn btn-primary" data-disable-with="Register">
HTML

actual = form_builder.primary "Register"

expect(actual).to match_html(expected)
end

it "renders danger button" do
expected = <<~HTML
<input type="submit" name="commit" value="Delete" class="btn btn-danger" data-disable-with="Delete">
HTML

actual = form_builder.danger "Delete"

expect(actual).to match_html(expected)
end
end

describe "#button_variant_class" do
let(:dummy_class) { Class.new { include RailsBootstrapForm::Helpers::Buttons } }
subject { dummy_class.new }

it "does add secondary variant class" do
expect(subject.send(:button_variant_class, {variant: "secondary"})).to eq("btn-secondary")
end

it "does add primary variant class" do
expect(subject.send(:button_variant_class, {variant: "primary"})).to eq("btn-primary")
end

it "does add danger variant class" do
expect(subject.send(:button_variant_class, {variant: "danger"})).to eq("btn-danger")
end

it "does not add any variant class" do
expect(subject.send(:button_variant_class, {variant: "invalid"})).to eq("")
end
end
end

0 comments on commit ffa8611

Please sign in to comment.