Class: Validatable::Errors

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

Instance Method Summary collapse

Constructor Details

#initialize(owner = nil) ⇒ Errors

Returns a new instance of Errors.



8
9
10
# File 'lib/validatable/errors.rb', line 8

def initialize(owner = nil)
  @owner = owner
end

Instance Method Details

#[](attribute) ⇒ Object

Rails 3 API for errors, always return array.



37
38
39
# File 'lib/validatable/errors.rb', line 37

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

#add(attribute, message) ⇒ Object

:nodoc:



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

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

#countObject

:nodoc:



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

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

#errorsObject

:nodoc:



65
66
67
# File 'lib/validatable/errors.rb', line 65

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/validatable/errors.rb', line 94

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 << (Validatable.use_i18n? ? msg : humanize(attribute.to_s) + " " + msg)
      end
    end
  end
  full_messages
end

#humanize(lower_case_and_underscored_word) ⇒ Object

:nodoc:



111
112
113
# File 'lib/validatable/errors.rb', line 111

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

#i18n_message(attribute, msg) ⇒ Object



87
88
89
# File 'lib/validatable/errors.rb', line 87

def i18n_message(attribute, msg)
  I18n.t(msg, :attribute => translate_attribute(attribute.to_s))
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)


22
23
24
# File 'lib/validatable/errors.rb', line 22

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

#klazz_nameObject



77
78
79
80
81
82
83
84
85
# File 'lib/validatable/errors.rb', line 77

def klazz_name
  @owner.class.name.empty? ?
    'generic' :
    @owner.class.name.to_s.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end

#merge!(errors) ⇒ Object

:nodoc:



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

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.



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

def on(attribute)
  return nil if errors[attribute.to_sym].nil?
  error = 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.



61
62
63
# File 'lib/validatable/errors.rb', line 61

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

#replace(attribute, value) ⇒ Object

call-seq: replace(attribute)

  • Replaces the errors value for the given attribute



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

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

#translate_attribute(attribute) ⇒ Object



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

def translate_attribute(attribute)
  I18n.t("validatable.attributes.#{klazz_name}.#{attribute.to_s}", :default => humanize(attribute.to_s))
end