@@ -31,44 +31,66 @@ def nested_attr_name_separator
3131 @nested_attr_name_separator = '.'
3232 end
3333
34- sig { returns ( T :: Hash [ Symbol , Shale ::Attribute ] ) }
34+ #: -> Hash[Symbol, Shale::Attribute]
3535 def validatable_attributes
3636 @validatable_attributes ||= attributes . select do |_ , val |
3737 val . validatable?
3838 end
3939 end
40+
41+ #: -> Array[Symbol]
42+ def validatable_attribute_names
43+ validatable_attribute_names . keys
44+ end
4045 end
4146 mixes_in_class_methods ClassMethods
4247
43- sig { returns ( T ::Boolean ) }
48+ #: -> Array[Symbol]
49+ def validatable_attribute_names
50+ self . class . validatable_attribute_names
51+ end
52+
53+ #: -> String
54+ def nested_attr_name_separator
55+ self . class . nested_attr_name_separator
56+ end
57+
58+ #: -> bool
4459 def valid?
4560 result = super
46- errlist = errors
47- klass = self . class #: as untyped
48- separator = klass . nested_attr_name_separator
4961
50- attrs = T . unsafe ( self ) . class . validatable_attributes
51- attrs . each_key do |name |
62+ validatable_attribute_names . each_key do |name |
63+ next unless name
64+
5265 val = public_send ( name )
5366 next unless val
5467 next if val . valid?
5568
5669 result = false
57- val . errors . each do |err |
58- errlist . import ( err , attribute : "#{ name } #{ separator } #{ err . attribute } " )
59- end
70+ import_errors ( val )
6071 end
6172
6273 result
6374 end
6475
76+ #: (ActiveModel::Validations?) -> void
77+ def import_errors ( obj )
78+ return unless obj
79+
80+ errlist = errors
81+ separator = nested_attr_name_separator
82+ obj . errors . each do |err |
83+ errlist . import ( err , attribute : "#{ name } #{ separator } #{ err . attribute } " )
84+ end
85+ end
86+
6587 end
6688 end
6789end
6890
6991module Shale
7092 class Attribute # rubocop:disable Style/Documentation
71- sig { returns ( T :: Boolean ) }
93+ #: -> bool
7294 def validatable?
7395 Boolean ( type . is_a? ( Class ) && type < ::ActiveModel ::Validations )
7496 end
0 commit comments