Class: Validatable::Errors

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/validatable/errors.rb

Instance Method Summary collapse

Instance Method Details

#[](attribute) ⇒ Object

Rails 3 API for errors, always return array.



46
47
48
# File 'lib/validatable/errors.rb', line 46

def [](attribute)
  errors[attribute.to_sym] || []
end

#add(attribute, message) ⇒ Object

:nodoc:



50
51
52
53
# File 'lib/validatable/errors.rb', line 50

def add(attribute, message) #:nodoc:
  errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
  errors[attribute.to_sym] << message
end

#add_to_base(msg) ⇒ Object

Adds an error to the base object instead of any particular attribute. This is used to report errors that don’t tie to any specific attribute, but rather to the object as a whole. These error messages don’t get prepended with any field name when iterating with each_full, so they should be complete sentences.



26
27
28
# File 'lib/validatable/errors.rb', line 26

def add_to_base(msg)
  add(:base, msg)
end

#countObject

:nodoc:



78
79
80
# File 'lib/validatable/errors.rb', line 78

def count #:nodoc:
  errors.values.flatten.size
end

#errorsObject

:nodoc:



74
75
76
# File 'lib/validatable/errors.rb', line 74

def errors #:nodoc:
  @errors ||= {}
end

#full_messagesObject

call-seq: full_messages -> an_array_of_messages

Returns an array containing the full list of error messages.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/validatable/errors.rb', line 85

def full_messages
  full_messages = []

  errors.each_key do |attribute|
    errors[attribute].each do |msg|
      next if msg.nil?

      if attribute.to_s == "base"
        full_messages << msg
      else
        full_messages << humanize(attribute.to_s) + " " + msg
      end
    end
  end
  full_messages
end

#humanize(lower_case_and_underscored_word) ⇒ Object

:nodoc:



102
103
104
# File 'lib/validatable/errors.rb', line 102

def humanize(lower_case_and_underscored_word) #:nodoc:
  lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
end

#invalid?(attribute) ⇒ Boolean

Returns true if the specified attribute has errors associated with it.

class Company < ActiveRecord::Base
  validates_presence_of :name, :address, :email
  validates_length_of :name, :in => 5..30
end

company = Company.create(:address => '123 First St.')
company.errors.invalid?(:name)      # => true
company.errors.invalid?(:address)   # => false

Returns:

  • (Boolean)


18
19
20
# File 'lib/validatable/errors.rb', line 18

def invalid?(attribute)
  !@errors[attribute.to_sym].nil?
end

#merge!(errors) ⇒ Object

:nodoc:



55
56
57
58
# File 'lib/validatable/errors.rb', line 55

def merge!(errors) #:nodoc:
  errors.each_pair{|k, v| add(k,v)}
  self
end

#on(attribute) ⇒ Object

call-seq: on(attribute)

  • Returns nil, if no errors are associated with the specified attribute.

  • Returns the error message, if one error is associated with the specified attribute.

  • Returns an array of error messages, if more than one error is associated with the specified attribute.



40
41
42
43
# File 'lib/validatable/errors.rb', line 40

def on(attribute)
  return nil if errors[attribute.to_sym].nil?
  errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
end

#on_baseObject

Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).



31
32
33
# File 'lib/validatable/errors.rb', line 31

def on_base
  on(:base)
end

#raw(attribute) ⇒ Object

call-seq: raw(attribute)

  • Returns an array of error messages associated with the specified attribute.



70
71
72
# File 'lib/validatable/errors.rb', line 70

def raw(attribute)
  errors[attribute.to_sym]
end

#replace(attribute, value) ⇒ Object

call-seq: replace(attribute)

  • Replaces the errors value for the given attribute



63
64
65
# File 'lib/validatable/errors.rb', line 63

def replace(attribute, value)
  errors[attribute.to_sym] = value
end