Class: BigRecord::Errors

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/big_record/validations.rb

Overview

Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object in a valid state to be saved. See usage example in Validations.

Constant Summary

@@default_error_messages =
{
  :inclusion => "is not included in the list",
  :exclusion => "is reserved",
  :invalid => "is invalid",
  :confirmation => "doesn't match confirmation",
  :accepted  => "must be accepted",
  :empty => "can't be empty",
  :blank => "can't be blank",
  :too_long => "is too long (maximum is %d characters)",
  :too_short => "is too short (minimum is %d characters)",
  :wrong_length => "is the wrong length (should be %d characters)",
  :taken => "has already been taken",
  :not_a_number => "is not a number"
}

Instance Method Summary

Constructor Details

- (Errors) initialize(base)

:nodoc:



22
23
24
# File 'lib/big_record/validations.rb', line 22

def initialize(base) # :nodoc:
  @base, @errors = base, {}
end

Instance Method Details

- (Object) add(attribute, msg = @@default_error_messages[:invalid])

Adds an error message (msg) to the attribute, which will be returned on a call to on(attribute) for the same attribute and ensure that this error object returns false when asked if empty?. More than one error can be added to the same attribute in which case an array will be returned on a call to on(attribute). If no msg is supplied, "invalid" is assumed.



57
58
59
60
# File 'lib/big_record/validations.rb', line 57

def add(attribute, msg = @@default_error_messages[:invalid])
  @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
  @errors[attribute.to_s] << msg
end

- (Object) add_on_blank(attributes, msg = @@default_error_messages[:blank])

Will add an error message to each of the attributes in attributes that is blank (using Object#blank?).



72
73
74
75
76
77
# File 'lib/big_record/validations.rb', line 72

def add_on_blank(attributes, msg = @@default_error_messages[:blank])
  for attr in [attributes].flatten
    value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
    add(attr, msg) if value.blank?
  end
end

- (Object) add_on_empty(attributes, msg = @@default_error_messages[:empty])

Will add an error message to each of the attributes in attributes that is empty.



63
64
65
66
67
68
69
# File 'lib/big_record/validations.rb', line 63

def add_on_empty(attributes, msg = @@default_error_messages[:empty])
  for attr in [attributes].flatten
    value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
    is_empty = value.respond_to?("empty?") ? value.empty? : false
    add(attr, msg) unless !value.nil? && !is_empty
  end
end

- (Object) add_to_base(msg)

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.



49
50
51
# File 'lib/big_record/validations.rb', line 49

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

- (Object) clear

Removes all the errors that have been added.



135
136
137
# File 'lib/big_record/validations.rb', line 135

def clear
  @errors = {}
end

- (Object) each

Yields each attribute and associated message per error added.



101
102
103
# File 'lib/big_record/validations.rb', line 101

def each
  @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
end

- (Object) each_full

Yields each full error message added. So Person.errors.add("first_name", "can’t be empty") will be returned through iteration as "First name can’t be empty".



107
108
109
# File 'lib/big_record/validations.rb', line 107

def each_full
  full_messages.each { |msg| yield msg }
end

- (Boolean) empty?

Returns true if no errors have been added.

Returns:

  • (Boolean)


130
131
132
# File 'lib/big_record/validations.rb', line 130

def empty?
  @errors.empty?
end

- (Object) full_messages

Returns all the full error messages in an array.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/big_record/validations.rb', line 112

def full_messages
  full_messages = []

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

      if attr == "base"
        full_messages << msg
      else
        full_messages << @base.human_attribute_name(attr) + " " + msg
      end
    end
  end
  full_messages
end

- (Boolean) invalid?(attribute)

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

Returns:

  • (Boolean)


80
81
82
# File 'lib/big_record/validations.rb', line 80

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

- (Object) on(attribute) Also known as: []

  • 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.


87
88
89
90
91
# File 'lib/big_record/validations.rb', line 87

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

- (Object) on_base

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



96
97
98
# File 'lib/big_record/validations.rb', line 96

def on_base
  on(:base)
end

- (Object) size Also known as: length, count

Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.



141
142
143
# File 'lib/big_record/validations.rb', line 141

def size
  @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
end

- (Object) to_xml(options = {})

Return an XML representation of this error object.



149
150
151
152
153
154
155
156
157
158
# File 'lib/big_record/validations.rb', line 149

def to_xml(options={})
  options[:root] ||= "errors"
  options[:indent] ||= 2
  options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])

  options[:builder].instruct! unless options.delete(:skip_instruct)
  options[:builder].errors do |e|
    full_messages.each { |msg| e.error(msg) }
  end
end