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_error(attribute_name, message) ⇒ Object
If a resource namespace is active (within with_resource block), append the error to the resource.
-
#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.
-
#current_resource ⇒ Object
If a resource namespace is active (within with_resource block), find the resource using the namespace and uid.
- #handle_validation_error(error) ⇒ Object
- #raise_validate_errors ⇒ Object
-
#resource_attribute_has_errors?(resource_namespace, resource_uid_or_attribute_name, attribute_name = nil) ⇒ 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 = nil) ⇒ Boolean
TODO: YARD.
-
#with_each_resource(resource_namespace, offset: nil) { ... } ⇒ Object
yield to the given block for each resource in the given namespace.
-
#with_resource(resource_namespace, resource_uid = nil, offset: nil) { ... } ⇒ Object
For the duration of the given block, all validations will be done on the given resource.
Instance Method Details
#any_errors? ⇒ Boolean
Determine if current form instance is valid by running the validations
specified on #validate.
194 195 196 |
# File 'lib/gourami/extensions/resources.rb', line 194 def any_errors? super || any_resource_errors? end |
#any_resource_errors? ⇒ Boolean
Return true if any resources by any uids have any errors.
201 202 203 |
# File 'lib/gourami/extensions/resources.rb', line 201 def any_resource_errors? resource_errors.values.flat_map(&:values).map(&:values).flatten.any? end |
#append_error(attribute_name, message) ⇒ Object
If a resource namespace is active (within with_resource block), append the error to the resource. Otherwise, append the error to the form object.
100 101 102 103 104 105 106 |
# File 'lib/gourami/extensions/resources.rb', line 100 def append_error(attribute_name, ) if @resource_namespace append_resource_error(@resource_namespace, @offset ? @resource_uid + @offset : @resource_uid, attribute_name, ) else super end 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.
186 187 188 |
# File 'lib/gourami/extensions/resources.rb', line 186 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.
210 211 212 213 214 215 216 |
# File 'lib/gourami/extensions/resources.rb', line 210 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 |
#current_resource ⇒ Object
If a resource namespace is active (within with_resource block), find the resource using the namespace and uid. Otherwise, return the form object.
88 89 90 91 92 93 94 95 96 |
# File 'lib/gourami/extensions/resources.rb', line 88 def current_resource if @resource_namespace && @resource_uid send(@resource_namespace)[@resource_uid] elsif @resource_namespace send(@resource_namespace) else super end end |
#handle_validation_error(error) ⇒ Object
218 219 220 221 |
# File 'lib/gourami/extensions/resources.rb', line 218 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
223 224 225 |
# File 'lib/gourami/extensions/resources.rb', line 223 def raise_validate_errors raise ValidationError.new(errors, resource_errors) end |
#resource_attribute_has_errors?(resource_namespace, resource_uid_or_attribute_name, attribute_name = nil) ⇒ Boolean
TODO: YARD
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gourami/extensions/resources.rb', line 166 def resource_attribute_has_errors?(resource_namespace, resource_uid_or_attribute_name, attribute_name = nil) if attribute_name.nil? # 2 arguments: resource_namespace, attribute_name attribute_name = resource_uid_or_attribute_name resource_uid = nil else # 3 arguments: resource_namespace, resource_uid, attribute_name resource_uid = resource_uid_or_attribute_name end 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.
150 151 152 153 154 155 156 157 158 |
# File 'lib/gourami/extensions/resources.rb', line 150 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 = nil) ⇒ Boolean
TODO: YARD
161 162 163 |
# File 'lib/gourami/extensions/resources.rb', line 161 def resource_has_errors?(resource_namespace, resource_uid = nil) resource_errors[resource_namespace][resource_uid&.to_s].values.map(&:flatten).any? end |
#with_each_resource(resource_namespace, offset: nil) { ... } ⇒ Object
yield to the given block for each resource in the given namespace.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gourami/extensions/resources.rb', line 38 def with_each_resource(resource_namespace, offset: nil, &_block) resources = send(resource_namespace) if resources.is_a?(Hash) return resources.each_with_index do |(key, resource), index| with_resource(resource_namespace, key, offset: offset) do yield(resource, key, index) end end end send(resource_namespace).each_with_index do |resource, index| with_resource(resource_namespace, index, offset: offset) do yield(resource, offset ? index + offset : index, index) end end end |
#with_resource(resource_namespace, resource_uid = nil, offset: nil) { ... } ⇒ Object
For the duration of the given block, all validations will be done on the given resource.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gourami/extensions/resources.rb', line 75 def with_resource(resource_namespace, resource_uid = nil, offset: nil, &_block) @resource_namespace = resource_namespace @resource_uid = resource_uid @offset = offset yield ensure @resource_namespace = nil @resource_uid = nil @offset = nil end |