-
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 #6 from shivam091/0.3.0
0.3.0
- Loading branch information
Showing
14 changed files
with
203 additions
and
12 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,67 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module Components | ||
module Errors | ||
extend ActiveSupport::Concern | ||
|
||
def self.included(base_class) | ||
def is_invalid?(attribute) | ||
(attribute && object.respond_to?(:errors) && object.errors[attribute].any?) || | ||
has_association_error?(attribute) | ||
end | ||
|
||
def input_with_error(attribute, &block) | ||
input = capture(&block) | ||
input << generate_error(attribute) | ||
input | ||
end | ||
|
||
def generate_error(attribute) | ||
if is_invalid?(attribute) | ||
error_text = error_messages(attribute) | ||
error_klass = "invalid-feedback" | ||
|
||
tag.div(error_text, class: error_klass) | ||
end | ||
end | ||
|
||
def has_association_error?(attribute) | ||
object.class.reflections.any? do |association_name, association| | ||
next unless is_belongs_to_association?(association) | ||
next unless is_association_same?(attribute, association) | ||
|
||
object.errors[association_name].any? | ||
end | ||
end | ||
|
||
def error_messages(attribute) | ||
messages = object.errors[attribute] | ||
|
||
object.class.reflections.each do |association_name, association| | ||
next unless is_belongs_to_association?(association) | ||
next unless is_association_same?(attribute, association) | ||
|
||
messages << object.errors[association_name] | ||
end | ||
|
||
messages.flatten.to_sentence | ||
end | ||
|
||
def is_belongs_to_association?(association) | ||
association.is_a?(ActiveRecord::Reflection::BelongsToReflection) | ||
end | ||
|
||
def is_association_same?(attribute, association) | ||
(association.foreign_key == attribute.to_s) | ||
end | ||
|
||
private :is_invalid?, :input_with_error, :generate_error, | ||
:has_association_error?, :error_messages, | ||
:is_belongs_to_association?, :is_association_same? | ||
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
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,49 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module InputGroupBuilder | ||
extend ActiveSupport::Concern | ||
|
||
def self.included(base_class) | ||
def input_group_wrapper(attribute, bootstrap_options, &block) | ||
input = capture(&block) || ActiveSupport::SafeBuffer.new | ||
|
||
prepend = attach_input(bootstrap_options, :prepend) | ||
append = attach_input(bootstrap_options, :append) | ||
|
||
input = prepend + input + append | ||
input += generate_error(attribute) | ||
|
||
input = tag.div(input, class: input_group_classes(attribute, bootstrap_options)) | ||
|
||
input | ||
end | ||
|
||
def input_group_classes(attribute, bootstrap_options) | ||
classes = ["input-group", bootstrap_options.additional_input_group_class] | ||
# Require `has-validation` class if field has errors. | ||
classes << "has-validation" if is_invalid?(attribute) | ||
classes.flatten.compact | ||
end | ||
|
||
def attach_input(bootstrap_options, key) | ||
tags = [*bootstrap_options.send(key)].map do |item| | ||
input_group_content(item) | ||
end | ||
|
||
ActiveSupport::SafeBuffer.new(tags.join) | ||
end | ||
|
||
def input_group_content(content) | ||
return content if /button|submit/.match?(content) | ||
|
||
tag.span(content.html_safe, class: "input-group-text") | ||
end | ||
|
||
private :input_group_wrapper, :input_group_classes, :attach_input, | ||
:input_group_content | ||
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