Module: Gourami::Extensions::Resources
- Defined in:
- lib/gourami/extensions/resources.rb
Instance Method Summary collapse
-
#any_errors? ⇒ Boolean
Determine if current form instance is valid by running the validations specified on #validate.
-
#any_resource_errors? ⇒ Boolean
Return true if any resources by any uids have any errors.
-
#append_resource_error(resource_namespace, resource_uid, attribute_name, error) ⇒ Object
Append an error to the given attribute for a resource.
-
#clear_and_set_resource_errors(new_resource_errors) ⇒ Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>
Replace the existing resource errors with the provided errors Hash.
- #handle_validation_error(error) ⇒ Object
- #raise_validate_errors ⇒ Object
-
#resource_attribute_has_errors?(resource_namespace, resource_uid, attribute_name) ⇒ Boolean
TODO: YARD.
-
#resource_errors ⇒ Hash<Symbol>
Return a deeply nested Hash which allows you to identify errors by resource.
-
#resource_has_errors?(resource_namespace, resource_uid) ⇒ Boolean
TODO: YARD.
Instance Method Details
#any_errors? ⇒ Boolean
Determine if current form instance is valid by running the validations
specified on #validate.
83 84 85 |
# File 'lib/gourami/extensions/resources.rb', line 83 def any_errors? super || any_resource_errors? end |
#any_resource_errors? ⇒ Boolean
Return true if any resources by any uids have any errors.
90 91 92 |
# File 'lib/gourami/extensions/resources.rb', line 90 def any_resource_errors? resource_errors.values.flat_map(&:values).map(&:values).flatten.any? end |
#append_resource_error(resource_namespace, resource_uid, attribute_name, error) ⇒ Object
Append an error to the given attribute for a resource. TODO: consider coercing attribute_name ‘.to_s` too.
75 76 77 |
# File 'lib/gourami/extensions/resources.rb', line 75 def append_resource_error(resource_namespace, resource_uid, attribute_name, error) resource_errors[resource_namespace][resource_uid.to_s][attribute_name] << error end |
#clear_and_set_resource_errors(new_resource_errors) ⇒ Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>
Replace the existing resource errors with the provided errors Hash.
99 100 101 102 103 104 105 |
# File 'lib/gourami/extensions/resources.rb', line 99 def clear_and_set_resource_errors(new_resource_errors) new_resource_errors = new_resource_errors.dup resource_errors.clear resource_errors.merge!(new_resource_errors) resource_errors end |
#handle_validation_error(error) ⇒ Object
107 108 109 110 |
# File 'lib/gourami/extensions/resources.rb', line 107 def handle_validation_error(error) super(error) clear_and_set_resource_errors(error.resource_errors) unless error.resource_errors.nil? end |
#raise_validate_errors ⇒ Object
112 113 114 |
# File 'lib/gourami/extensions/resources.rb', line 112 def raise_validate_errors raise ValidationError.new(errors, resource_errors) end |
#resource_attribute_has_errors?(resource_namespace, resource_uid, attribute_name) ⇒ Boolean
TODO: YARD
63 64 65 |
# File 'lib/gourami/extensions/resources.rb', line 63 def resource_attribute_has_errors?(resource_namespace, resource_uid, attribute_name) resource_errors[resource_namespace][resource_uid.to_s][attribute_name].any? end |
#resource_errors ⇒ Hash<Symbol>
Return a deeply nested Hash which allows you to identify errors by resource.
47 48 49 50 51 52 53 54 55 |
# File 'lib/gourami/extensions/resources.rb', line 47 def resource_errors @resource_errors ||= Hash.new do |resource_errors, resource_name| resource_errors[resource_name] = Hash.new do |resource_uids, resource_uid| resource_uids[resource_uid] = Hash.new do |attributes, attribute_name| attributes[attribute_name] = [] end end end end |
#resource_has_errors?(resource_namespace, resource_uid) ⇒ Boolean
TODO: YARD
58 59 60 |
# File 'lib/gourami/extensions/resources.rb', line 58 def resource_has_errors?(resource_namespace, resource_uid) resource_errors[resource_namespace, resource_uid.to_s].values.map(&:flatten).any? end |