Module: Gourami::Extensions::Resources

Defined in:
lib/gourami/extensions/resources.rb

Instance Method Summary collapse

Instance Method Details

#any_errors?Boolean

Determine if current form instance is valid by running the validations

specified on #validate.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • resource_namespace (Symbol)
  • resource_uid (String)
  • attribute_name (Symbol)
  • error (Symbol, String)

    The error identifier.



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.

Parameters:

  • new_resource_errors (Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>)

Returns:

  • (Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>)


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_errorsObject

Raises:



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

Returns:

  • (Boolean)


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_errorsHash<Symbol>

Return a deeply nested Hash which allows you to identify errors by resource.

Examples:

resource_errors
# => {
#   :social_broadcasts => {
#     :"facebook_page-41" => {
#       :trim_start_time => [:is_invalid, :is_too_short],
#       :trim_end_time => [:is_invalid]
#     },
#     :"youtube_account-42" => {
#       :title => [:is_too_short]
#     }
#   },
#   :another_resource => {
#     :"other_resource_id-12" => {
#       :something => [:is_too_long]
#     }
#   }
# }

resource_errors[:social_broadcasts]
# => {
#   :"facebook_page-41" => {
#     :trim_start_time => [:is_invalid, :is_too_short],
#     :trim_end_time => [:is_invalid]
#   },
#   :"youtube_account-42" => {
#     :title => [:is_too_short]
#   }
# }

resource_errors[:social_broadcasts][:"facebook_page-41"]
# => {
#   :trim_start_time => [:is_invalid, :is_too_short],
#   :trim_end_time => [:is_invalid]
# }

resource_errors[:social_broadcasts][:"facebook_page-41"][:trim_start_time]
# => [:is_invalid, :is_too_short]

Returns:

  • (Hash<Symbol>)


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

Returns:

  • (Boolean)


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