Skip to content

Commit

Permalink
Merge pull request #22 from shivam091/0.7.0
Browse files Browse the repository at this point in the history
0.7.0
  • Loading branch information
shivam091 committed May 24, 2023
2 parents 0e3f54a + e69536d commit 4371ccc
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion demo/app/views/users/_horizontal_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<% end %>
<%= form.check_box :terms, required: true %>
<div class="mt-3">
<%= form.submit "Register", class: "btn btn-primary" %>
<%= form.primary "Register" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion demo/app/views/users/_vertical_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<% end %>
<%= form.check_box :terms, required: true %>
<div class="mt-3">
<%= form.submit "Register", class: "btn btn-primary" %>
<%= form.primary "Register" %>
<%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
</div>
<% end %>
Expand Down
9 changes: 8 additions & 1 deletion lib/rails_bootstrap_form/bootstrap_form_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<button type="submit">` instead of
# `<input type="submit">`.
# The default value is `false`.
attr_accessor :render_as_button

def initialize(options = {})
set_defaults
set_bootstrap_form_options(options)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion lib/rails_bootstrap_form/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ module Helpers
autoload :Errors
autoload :CheckBox
autoload :RadioButton
autoload :Buttons

include HelpText
include Labels
include RequiredField
include Errors
include CheckBox
include RadioButton
include Buttons

def self.included(base_class)
def collection_input_checked?(checked, obj, input_value)
Expand All @@ -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
39 changes: 39 additions & 0 deletions lib/rails_bootstrap_form/helpers/buttons.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/rails_bootstrap_form/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 15 additions & 5 deletions spec/rails_bootstrap_form/bootstrap_form_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/rails_bootstrap_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4371ccc

Please sign in to comment.