Class: Validatable::Errors
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/validatable/errors.rb
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
Rails 3 API for errors, always return array.
-
#add(attribute, message) ⇒ Object
:nodoc:.
-
#count ⇒ Object
:nodoc:.
-
#errors ⇒ Object
:nodoc:.
-
#full_messages ⇒ Object
call-seq: full_messages -> an_array_of_messages.
-
#humanize(lower_case_and_underscored_word) ⇒ Object
:nodoc:.
-
#invalid?(attribute) ⇒ Boolean
Returns true if the specified
attribute
has errors associated with it. -
#merge!(errors) ⇒ Object
:nodoc:.
-
#on(attribute) ⇒ Object
call-seq: on(attribute).
-
#raw(attribute) ⇒ Object
call-seq: raw(attribute).
-
#replace(attribute, value) ⇒ Object
call-seq: replace(attribute).
Instance Method Details
#[](attribute) ⇒ Object
Rails 3 API for errors, always return array.
33 34 35 |
# File 'lib/validatable/errors.rb', line 33 def [](attribute) errors[attribute.to_sym] || [] end |
#add(attribute, message) ⇒ Object
:nodoc:
37 38 39 40 |
# File 'lib/validatable/errors.rb', line 37 def add(attribute, ) #:nodoc: errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil? errors[attribute.to_sym] << end |
#count ⇒ Object
:nodoc:
65 66 67 |
# File 'lib/validatable/errors.rb', line 65 def count #:nodoc: errors.values.flatten.size end |
#errors ⇒ Object
:nodoc:
61 62 63 |
# File 'lib/validatable/errors.rb', line 61 def errors #:nodoc: @errors ||= {} end |
#full_messages ⇒ Object
call-seq: full_messages -> an_array_of_messages
Returns an array containing the full list of error messages.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/validatable/errors.rb', line 72 def = [] errors.each_key do |attribute| errors[attribute].each do |msg| next if msg.nil? if attribute.to_s == "base" << msg else << humanize(attribute.to_s) + " " + msg end end end end |
#humanize(lower_case_and_underscored_word) ⇒ Object
:nodoc:
89 90 91 |
# File 'lib/validatable/errors.rb', line 89 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
18 19 20 |
# File 'lib/validatable/errors.rb', line 18 def invalid?(attribute) !@errors[attribute.to_sym].nil? end |
#merge!(errors) ⇒ Object
:nodoc:
42 43 44 45 |
# File 'lib/validatable/errors.rb', line 42 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
.
27 28 29 30 |
# File 'lib/validatable/errors.rb', line 27 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 |
#raw(attribute) ⇒ Object
call-seq: raw(attribute)
-
Returns an array of error messages associated with the specified
attribute
.
57 58 59 |
# File 'lib/validatable/errors.rb', line 57 def raw(attribute) errors[attribute.to_sym] end |
#replace(attribute, value) ⇒ Object
call-seq: replace(attribute)
-
Replaces the errors value for the given
attribute
50 51 52 |
# File 'lib/validatable/errors.rb', line 50 def replace(attribute, value) errors[attribute.to_sym] = value end |