Module: Shale::Builder::NestedValidations
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/shale/builder/nested_validations.rb
Overview
Include in a class that already includes ‘Shale::Builder` to add support for nested ActiveModel validations.
@requires_ancestor: Object
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#import_errors(obj, name: nil) ⇒ Object
: (ActiveModel::Validations?, ?name: String | Symbol?) -> void.
-
#nested_attr_name_separator ⇒ Object
: -> String.
-
#valid? ⇒ Boolean
: -> bool.
-
#validatable_attribute_names ⇒ Object
: -> Array.
Instance Method Details
#import_errors(obj, name: nil) ⇒ Object
: (ActiveModel::Validations?, ?name: String | Symbol?) -> void
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/shale/builder/nested_validations.rb', line 77 def import_errors(obj, name: nil) return unless obj errlist = errors if name separator = nested_attr_name_separator prefix = "#{name}#{separator}" end obj.errors.each do |err| errlist.import(err, attribute: "#{prefix}#{err.attribute}") end end |
#nested_attr_name_separator ⇒ Object
: -> String
54 55 56 |
# File 'lib/shale/builder/nested_validations.rb', line 54 def nested_attr_name_separator self.class.nested_attr_name_separator end |
#valid? ⇒ Boolean
: -> bool
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/shale/builder/nested_validations.rb', line 59 def valid? result = super validatable_attribute_names.each do |name| next unless name val = public_send(name) next unless val next if val.valid? result = false import_errors(val, name: name) end result end |
#validatable_attribute_names ⇒ Object
: -> Array
49 50 51 |
# File 'lib/shale/builder/nested_validations.rb', line 49 def validatable_attribute_names self.class.validatable_attribute_names end |