diff --git a/CHANGELOG.md b/CHANGELOG.md
index e2e31c7..3da6767 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,11 @@ You can find recent releases with docs in GitHub:
https://github.com/shivam091/rails_bootstrap_form/releases
+## [0.7.0](https://github.com/shivam091/rails_bootstrap_form/compare/v0.6.2...v0.7.0) - 2023-05-24
+
+### What's new
+- Added helper methods for rendering submit buttons on forms.
+
## [0.6.2](https://github.com/shivam091/rails_bootstrap_form/compare/v0.6.1...v0.6.2) - 2023-05-24
### What's fixed
diff --git a/Gemfile.lock b/Gemfile.lock
index 3908774..c513e17 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- rails_bootstrap_form (0.6.2)
+ rails_bootstrap_form (0.7.0)
actionpack (~> 7.0)
activemodel (~> 7.0)
diff --git a/demo/app/views/users/_horizontal_form.html.erb b/demo/app/views/users/_horizontal_form.html.erb
index eec3dc6..d5b7c36 100644
--- a/demo/app/views/users/_horizontal_form.html.erb
+++ b/demo/app/views/users/_horizontal_form.html.erb
@@ -24,7 +24,7 @@
<% end %>
<%= form.check_box :terms, required: true %>
- <%= form.submit "Register", class: "btn btn-primary" %>
+ <%= form.primary "Register" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
<% end %>
diff --git a/demo/app/views/users/_vertical_form.html.erb b/demo/app/views/users/_vertical_form.html.erb
index 0a736b9..9923a24 100644
--- a/demo/app/views/users/_vertical_form.html.erb
+++ b/demo/app/views/users/_vertical_form.html.erb
@@ -24,7 +24,7 @@
<% end %>
<%= form.check_box :terms, required: true %>
- <%= form.submit "Register", class: "btn btn-primary" %>
+ <%= form.primary "Register" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
<% end %>
diff --git a/lib/rails_bootstrap_form/bootstrap_form_options.rb b/lib/rails_bootstrap_form/bootstrap_form_options.rb
index e5b36f2..4e37a6c 100644
--- a/lib/rails_bootstrap_form/bootstrap_form_options.rb
+++ b/lib/rails_bootstrap_form/bootstrap_form_options.rb
@@ -119,6 +119,11 @@ class BootstrapFormOptions
# The default value is `col-sm-10`.
attr_accessor :field_col_wrapper_class
+ # Option to render submit button using `` instead of
+ # ` `.
+ # The default value is `false`.
+ attr_accessor :render_as_button
+
def initialize(options = {})
set_defaults
set_bootstrap_form_options(options)
@@ -157,7 +162,7 @@ def set_bootstrap_form_options(options)
end
end
- %i(inline floating switch skip_label hide_label).each do |method|
+ %i(inline floating switch skip_label hide_label render_as_button).each do |method|
define_method("#{method}?") { self.send(method) }
end
@@ -191,6 +196,8 @@ def set_defaults
@label_col_class = "col-form-label"
@label_col_wrapper_class = "col-sm-2"
@field_col_wrapper_class = "col-sm-10"
+
+ @render_as_button = false
end
private :set_defaults
diff --git a/lib/rails_bootstrap_form/helpers.rb b/lib/rails_bootstrap_form/helpers.rb
index 3bf1ee6..240488d 100644
--- a/lib/rails_bootstrap_form/helpers.rb
+++ b/lib/rails_bootstrap_form/helpers.rb
@@ -13,6 +13,7 @@ module Helpers
autoload :Errors
autoload :CheckBox
autoload :RadioButton
+ autoload :Buttons
include HelpText
include Labels
@@ -20,6 +21,7 @@ module Helpers
include Errors
include CheckBox
include RadioButton
+ include Buttons
def self.included(base_class)
def collection_input_checked?(checked, obj, input_value)
@@ -39,7 +41,19 @@ def field_offset_class(label_col_wrapper_class)
label_col_wrapper_class.gsub(/\bcol-(\w+)-(\d)\b/, 'offset-\1-\2')
end
- private :collection_input_checked?, :control_specific_class, :is_size_valid?
+ def add_css_class!(options, css_class)
+ the_class = [options[:class], css_class].compact
+ options[:class] = the_class if the_class.present?
+ end
+
+ def remove_css_class!(options, css_class)
+ the_class = options[:class].to_s.split(" ")
+ options[:class] = (the_class - [css_class]).compact.join(" ")
+ options.delete(:class) if options[:class].blank?
+ end
+
+ private :collection_input_checked?, :control_specific_class, :is_size_valid?,
+ :add_css_class!, :remove_css_class!
end
end
end
diff --git a/lib/rails_bootstrap_form/helpers/buttons.rb b/lib/rails_bootstrap_form/helpers/buttons.rb
new file mode 100644
index 0000000..268908b
--- /dev/null
+++ b/lib/rails_bootstrap_form/helpers/buttons.rb
@@ -0,0 +1,39 @@
+# -*- encoding: utf-8 -*-
+# -*- frozen_string_literal: true -*-
+# -*- warn_indent: true -*-
+
+module RailsBootstrapForm
+ module Helpers
+ 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)
+ bootstrap_options = bootstrap_form_options.scoped(options.delete(:bootstrap_form))
+
+ if (bootstrap_options.render_as_button? || block)
+ button(value, options, &block)
+ else
+ submit(value, options)
+ end
+ end
+
+ def secondary(value = nil, options = {}, &block)
+ add_css_class!(options, "btn btn-secondary")
+ render_button(value, options, &block)
+ end
+
+ def primary(value = nil, options = {}, &block)
+ add_css_class!(options, "btn btn-primary")
+ render_button(value, options, &block)
+ end
+
+ def danger(value = nil, options = {}, &block)
+ add_css_class!(options, "btn btn-danger")
+ render_button(value, options, &block)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/rails_bootstrap_form/version.rb b/lib/rails_bootstrap_form/version.rb
index 59e085c..b8a3aa4 100644
--- a/lib/rails_bootstrap_form/version.rb
+++ b/lib/rails_bootstrap_form/version.rb
@@ -3,6 +3,6 @@
# -*- warn_indent: true -*-
module RailsBootstrapForm
- VERSION = "0.6.2".freeze
+ VERSION = "0.7.0".freeze
REQUIRED_RAILS_VERSION = "~> 7.0".freeze
end
diff --git a/spec/rails_bootstrap_form/bootstrap_form_options_spec.rb b/spec/rails_bootstrap_form/bootstrap_form_options_spec.rb
index 278298e..8b816bc 100644
--- a/spec/rails_bootstrap_form/bootstrap_form_options_spec.rb
+++ b/spec/rails_bootstrap_form/bootstrap_form_options_spec.rb
@@ -31,6 +31,7 @@
expect(options.label_col_class).to eq("col-form-label")
expect(options.label_col_wrapper_class).to eq("col-sm-2")
expect(options.field_col_wrapper_class).to eq("col-sm-10")
+ expect(options.render_as_button).to be_falsy
end
end
@@ -56,6 +57,7 @@
label_col_class: "col-form-label",
label_col_wrapper_class: "col-sm-3",
field_col_wrapper_class: "col-sm-9",
+ render_as_button: true
)
end
@@ -79,6 +81,7 @@
expect(options.label_col_class).to eq("col-form-label")
expect(options.label_col_wrapper_class).to eq("col-sm-3")
expect(options.field_col_wrapper_class).to eq("col-sm-9")
+ expect(options.render_as_button).to be_truthy
end
end
@@ -120,37 +123,44 @@
end
describe "#floating?" do
- it "checks whether floating option is true" do
+ it "checks whether floating option is false" do
options = described_class.new
expect(options.floating?).to be_falsy
end
end
describe "#inline?" do
- it "checks whether inline option is true" do
+ it "checks whether inline option is false" do
options = described_class.new
expect(options.inline?).to be_falsy
end
end
describe "#switch?" do
- it "checks whether switch option is true" do
+ it "checks whether switch option is false" do
options = described_class.new
expect(options.switch?).to be_falsy
end
end
describe "#skip_label?" do
- it "checks whether skip_label option is true" do
+ it "checks whether skip_label option is false" do
options = described_class.new
expect(options.skip_label?).to be_falsy
end
end
describe "#hide_label?" do
- it "checks whether hide_label option is true" do
+ it "checks whether hide_label option is false" do
options = described_class.new
expect(options.hide_label?).to be_falsy
end
end
+
+ describe "#render_as_button?" do
+ it "checks whether render_as_button option is false" do
+ options = described_class.new
+ expect(options.render_as_button?).to be_falsy
+ end
+ end
end
diff --git a/spec/rails_bootstrap_form_spec.rb b/spec/rails_bootstrap_form_spec.rb
index 5883209..6538133 100644
--- a/spec/rails_bootstrap_form_spec.rb
+++ b/spec/rails_bootstrap_form_spec.rb
@@ -6,7 +6,7 @@
RSpec.describe RailsBootstrapForm do
it "has a valid version number" do
- expect(RailsBootstrapForm::VERSION).to eq("0.6.2")
+ expect(RailsBootstrapForm::VERSION).to eq("0.7.0")
end
it "has a valid rails version number" do