Skip to content
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

0.2.2 #4

Merged
merged 2 commits into from
May 15, 2023
Merged
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
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.2.1)
rails_bootstrap_form (0.2.2)

GEM
remote: http://rubygems.org/
Expand Down
28 changes: 28 additions & 0 deletions lib/rails_bootstrap_form/bootstrap_form_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ class BootstrapFormOptions
# Default is nil.
attr_accessor :help_text

# An option to override automatically generated label text.
attr_accessor :label_text

# An option to custmize whether the label is to be displayed or not.
attr_accessor :skip_label

# An option to customize whether the label is only visible to screen readers.
attr_accessor :hide_label

# The CSS class that will be used when the label is only accessible by screen
# readers. Default is `visually-hidden`
attr_accessor :hide_class

# Default CSS class that will be applied to all label tags.
# Default is `form-label`.
attr_accessor :label_class

# An additional CSS class that will be added along with the existing
# `label_class` of the label. Default is nil.
attr_accessor :additional_label_class

def initialize(options = {})
set_defaults
set_bootstrap_form_options(options)
Expand Down Expand Up @@ -83,6 +104,13 @@ def set_defaults
@additional_field_class = nil

@help_text = nil

@label_text = ""
@skip_label = false
@hide_label = false
@hide_class = "visually-hidden"
@label_class = "form-label"
@additional_label_class = nil
end

private :set_defaults
Expand Down
2 changes: 2 additions & 0 deletions lib/rails_bootstrap_form/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Components
extend ActiveSupport::Autoload

autoload :HelpText
autoload :Labels

include HelpText
include Labels
end
end
34 changes: 34 additions & 0 deletions lib/rails_bootstrap_form/components/labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module RailsBootstrapForm
module Components
module Labels
extend ActiveSupport::Concern

def self.included(base_class)
def label(attribute, bootstrap_options)
unless bootstrap_options.skip_label
label_class = label_classes(attribute, bootstrap_options)
label_text = label_text(attribute, bootstrap_options)

super(attribute, label_text, class: label_class)
end
end

def label_classes(attribute, bootstrap_options)
classes = [bootstrap_options.label_class, bootstrap_options.additional_label_class]
classes << bootstrap_options.hide_class if bootstrap_options.hide_label
classes.flatten.compact
end

def label_text(attribute, bootstrap_options)
bootstrap_options.label_text || object&.class.try(:human_attribute_name, attribute)
end

private :label, :label_classes, :label_text
end
end
end
end
2 changes: 2 additions & 0 deletions lib/rails_bootstrap_form/field_wrapper_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def field_wrapper_builder(attribute, options, html_options = nil, &block)
end

def field_wrapper(attribute, bootstrap_options, options, &block)
label = label(attribute, bootstrap_options)
help_text = help_text(attribute, bootstrap_options)

tag.div(class: field_wrapper_classes) do
concat(label)
concat(capture(&block))
concat(help_text)
end
Expand Down
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.2.1".freeze
VERSION = "0.2.2".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.2.1")
expect(RailsBootstrapForm::VERSION).to eq("0.2.2")
end

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