Class: ActiveRecord::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboss_on_ruby/active_foo.rb

Overview

Add more extensive reporting on errors including field name along with a message when errors are serialized to XML

Instance Method Summary collapse

Instance Method Details

#to_fxml(options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruboss_on_ruby/active_foo.rb', line 106

def to_fxml(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|
    # The @errors instance variable is a Hash inside the Errors class
    @errors.each_key do |attr|
      @errors[attr].each do |msg|
        next if msg.nil?
        if attr == "base"
          options[:builder].error("message" => msg)
        else
          fullmsg = @base.class.human_attribute_name(attr) + ' ' + msg
          options[:builder].error("field" => attr.camelcase(:lower), "message" => fullmsg)
        end
      end
    end
  end
end