-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from shivam091/0.2.1
0.2.1
- Loading branch information
Showing
12 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module Components | ||
extend ActiveSupport::Autoload | ||
|
||
autoload :HelpText | ||
|
||
include HelpText | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module Components | ||
module HelpText | ||
extend ActiveSupport::Concern | ||
|
||
def self.included(base_class) | ||
def help_text(attribute, bootstrap_options) | ||
return if bootstrap_options.help_text == false | ||
|
||
help_text = (bootstrap_options.help_text || scoped_help_text(attribute)) | ||
|
||
tag.span(help_text, class: "form-text text-muted") if help_text.present? | ||
end | ||
|
||
def object_class | ||
if !object.class.is_a?(ActiveModel::Naming) && | ||
object.respond_to?(:klass) && object.klass.is_a?(ActiveModel::Naming) | ||
object.klass | ||
else | ||
object.class | ||
end | ||
end | ||
|
||
def partial_scope | ||
if object_class.respond_to?(:model_name) | ||
object_class.model_name.name | ||
else | ||
object_class.name | ||
end | ||
end | ||
|
||
def scoped_help_text(attribute) | ||
translation_scope = "activerecord.help_texts.#{partial_scope.underscore}" | ||
|
||
help_text = translated_help_text(attribute, translation_scope).presence | ||
|
||
help_text | ||
end | ||
|
||
def translated_help_text(attribute, scope) | ||
ActiveSupport::SafeBuffer.new(I18n.t(attribute, scope: scope, default: "")) | ||
end | ||
|
||
private :help_text, :partial_scope, :object_class, :scoped_help_text | ||
:translated_help_text | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module FieldWrapperBuilder | ||
def field_wrapper_builder(attribute, options, html_options = nil, &block) | ||
bootstrap_options = bootstrap_form_options.scoped(options.delete(:bootstrap_form)) | ||
|
||
field_options = field_css_options(attribute, bootstrap_options, options, html_options.try(:symbolize_keys!)) | ||
|
||
field_wrapper(attribute, bootstrap_options, field_options, &block) | ||
end | ||
|
||
def field_wrapper(attribute, bootstrap_options, options, &block) | ||
help_text = help_text(attribute, bootstrap_options) | ||
|
||
tag.div(class: field_wrapper_classes) do | ||
concat(capture(&block)) | ||
concat(help_text) | ||
end | ||
end | ||
|
||
def field_wrapper_classes | ||
classes = [form_wrapper_default_class] | ||
classes.flatten.compact | ||
end | ||
|
||
def field_wrapper_options | ||
end | ||
|
||
def form_wrapper_default_class | ||
"mb-3" | ||
end | ||
|
||
def field_css_options(attribute, bootstrap_options, options, html_options) | ||
css_options = (html_options || options) | ||
|
||
field_classes = [ | ||
bootstrap_options.field_class, | ||
bootstrap_options.additional_field_class | ||
] | ||
css_options[:class] = field_classes.flatten.compact | ||
|
||
css_options | ||
end | ||
|
||
private :field_wrapper, :field_wrapper_classes, :form_wrapper_default_class, | ||
:field_css_options | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module Inputs | ||
|
||
FIELD_HELPERS = %i[ | ||
text_field | ||
url_field | ||
search_field | ||
telephone_field | ||
number_field | ||
email_field | ||
file_field | ||
phone_field | ||
password_field | ||
text_area | ||
date_field | ||
time_field | ||
datetime_field | ||
datetime_local_field | ||
month_field | ||
week_field | ||
].freeze | ||
|
||
FIELD_HELPERS.each do |field_tag_name| | ||
define_method(field_tag_name) do |attribute, options = {}| | ||
field_wrapper_builder(attribute, options) do | ||
super(attribute, options) | ||
end | ||
end | ||
end | ||
|
||
def select(attribute, choices = nil, options = {}, html_options = {}, &block) | ||
options = options.reverse_merge(bootstrap_form: {field_class: "form-select"}) | ||
|
||
field_wrapper_builder(attribute, options, html_options) do | ||
super(attribute, choices, options, html_options, &block) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters