Skip to content

Commit

Permalink
Merge pull request #14 from shivam091/0.5.1
Browse files Browse the repository at this point in the history
0.5.1
  • Loading branch information
shivam091 committed May 20, 2023
2 parents 48fd435 + 4d7ca90 commit e3d90ce
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 27 deletions.
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.5.0)
rails_bootstrap_form (0.5.1)

GEM
remote: http://rubygems.org/
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 @@ -18,7 +18,7 @@
<%= form.fields_for :address, include_id: false do |address_form| %>
<%= address_form.text_area :street %>
<%= address_form.text_field :state %>
<%= address_form.grouped_collection_select :city, ::Country.all, :cities, :name, :id, :name %>
<%= address_form.grouped_collection_select :city, ::Country.all, :cities, :name, :id, :name, {include_blank: "Select city"} %>
<%= address_form.text_field :postal_code %>
<%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id),
{include_blank: "Select Country", bootstrap_form: {}} %>
Expand Down
4 changes: 3 additions & 1 deletion demo/config/locales/en.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
help_texts: {
user: {
email: "Please use official email address",
terms: "You must first accept terms and conditions in order to continue"
terms: "You must first accept terms and conditions in order to continue",
skill_ids: "Select your strong skills",
fruit_id: "Select your favorite fruit"
}
},
}
Expand Down
6 changes: 1 addition & 5 deletions lib/rails_bootstrap_form/bootstrap_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def apply_default_form_options(options)
options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
end

def control_specific_class(field_tag_name)
"rails-bootstrap-forms-#{field_tag_name.to_s.tr("_", "-")}"
end

private :apply_default_form_options, :control_specific_class
private :apply_default_form_options
end
end
4 changes: 2 additions & 2 deletions lib/rails_bootstrap_form/components/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ def check_box_classes(attribute, options)

def check_box_label_class(attribute, bootstrap_options, options)
classes = Array("form-check-label") << bootstrap_options.additional_label_class
classes << "required" if is_field_required?(attribute, options)
classes << "required" if is_field_required?(attribute, options) && !bootstrap_options.inline?
classes << "is-invalid" if is_invalid?(attribute)
classes << bootstrap_options.hide_class if bootstrap_options.hide_label
classes.flatten.compact
end

def check_box_wrapper_class(bootstrap_options)
classes = ["mb-3", "form-check"]
classes = Array("form-check")
classes << "form-switch" if bootstrap_options.switch
classes << "form-check-inline" if bootstrap_options.inline?
classes.flatten.compact
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_bootstrap_form/components/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ def radio_button_classes(attribute, options)

def radio_button_label_class(attribute, bootstrap_options, options)
classes = Array("form-check-label") << bootstrap_options.additional_label_class
classes << "required" if is_field_required?(attribute, options)
classes << "required" if is_field_required?(attribute, options) && !bootstrap_options.inline?
classes << "is-invalid" if is_invalid?(attribute)
classes << bootstrap_options.hide_class if bootstrap_options.hide_label
classes.flatten.compact
end

def radio_button_wrapper_class(bootstrap_options)
classes = ["mb-3", "form-check"]
classes = Array("form-check")
classes << "form-check-inline" if bootstrap_options.inline?
classes.flatten.compact
end
Expand Down
37 changes: 24 additions & 13 deletions lib/rails_bootstrap_form/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
module RailsBootstrapForm
module Inputs

extend ActiveSupport::Autoload

autoload :Base

include Base

FIELD_HELPERS = %i[
text_field
url_field
Expand Down Expand Up @@ -134,8 +140,8 @@ def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0
check_box_html = tag.div(class: check_box_wrapper_class(bootstrap_options)) do
concat(check_box_field)
concat(check_box_label)
concat(check_box_help_text)
concat(generate_error(attribute)) if is_invalid?(attribute)
concat(check_box_help_text) unless bootstrap_options.inline?
concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline?
end

check_box_html
Expand All @@ -154,8 +160,8 @@ def radio_button(attribute, value, options = {})
radio_button_html = tag.div(class: radio_button_wrapper_class(bootstrap_options)) do
concat(radio_button_field)
concat(radio_button_label)
concat(radio_button_help_text)
concat(generate_error(attribute)) if is_invalid?(attribute)
concat(radio_button_help_text) unless bootstrap_options.inline?
concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline?
end

radio_button_html
Expand All @@ -177,7 +183,15 @@ def collection_check_boxes(attribute, collection, value_method, text_method, opt
inputs << check_box(attribute, input_options, input_value, nil)
end

inputs
if options.delete(:include_hidden) { true }
inputs.prepend hidden_field(attribute, value: "", multiple: true)
end

field_wrapper_builder(attribute, options, html_options) do
concat(tag.div(class: control_specific_class(:collection_check_boxes)) do
concat(inputs)
end)
end
end

def collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
Expand All @@ -198,14 +212,11 @@ def collection_radio_buttons(attribute, collection, value_method, text_method, o
inputs << radio_button(attribute, input_value, input_options)
end

inputs
end

def collection_input_checked?(checked, obj, input_value)
checked == input_value || Array(checked).try(:include?, input_value) ||
checked == obj || Array(checked).try(:include?, obj)
field_wrapper_builder(attribute, options, html_options) do
concat(tag.div(class: control_specific_class(:collection_radio_buttons)) do
concat(inputs)
end)
end
end

private :collection_input_checked?
end
end
24 changes: 24 additions & 0 deletions lib/rails_bootstrap_form/inputs/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module RailsBootstrapForm
module Inputs
module Base
extend ActiveSupport::Concern

def self.included(base_class)
def collection_input_checked?(checked, obj, input_value)
checked == input_value || Array(checked).try(:include?, input_value) ||
checked == obj || Array(checked).try(:include?, obj)
end

def control_specific_class(field_tag_name)
"rails-bootstrap-forms-#{field_tag_name.to_s.tr("_", "-")}"
end

private :collection_input_checked?, :control_specific_class
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.5.0".freeze
VERSION = "0.5.1".freeze
REQUIRED_RAILS_VERSION = "~> 7.0".freeze
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.5.0")
expect(RailsBootstrapForm::VERSION).to eq("0.5.1")
end

it "has a valid rails version number" do
Expand Down

0 comments on commit e3d90ce

Please sign in to comment.