Class: Gecko::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/gecko/helpers/validation_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set up the error object



38
39
40
41
# File 'lib/gecko/helpers/validation_helper.rb', line 38

def initialize(base)
  @base     = base
  @messages = {}
end

Instance Attribute Details

#messagesHash (readonly)

The hash of errors for this record

Returns:

  • (Hash)


33
34
35
# File 'lib/gecko/helpers/validation_helper.rb', line 33

def messages
  @messages
end

Instance Method Details

#[](attribute) ⇒ Array

Fetch the errors for a specific attribute

Examples:

product.errors[:name]
  #=> ["can't be blank"]

Returns:

  • (Array)


54
55
56
# File 'lib/gecko/helpers/validation_helper.rb', line 54

def [](attribute)
  messages[attribute.to_sym]
end

#clearundefined

Clear the validation errors

Examples:

product.errors.clear

Returns:

  • (undefined)


66
67
68
# File 'lib/gecko/helpers/validation_helper.rb', line 66

def clear
  @messages.clear
end

#empty?Boolean

Whether there are any errors

Returns:

  • (Boolean)


75
76
77
# File 'lib/gecko/helpers/validation_helper.rb', line 75

def empty?
  messages.all? { |_k, v| v&.empty? && !v.is_a?(String) }
end

#from_response(error_hash) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse JSON errors response into the error object

Returns:

  • (undefined)


87
88
89
90
91
# File 'lib/gecko/helpers/validation_helper.rb', line 87

def from_response(error_hash)
  error_hash.each do |attr, errors|
    @messages[attr.to_sym] = errors
  end
end