Class: ActiveRecord::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulx/rx_active_record.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#to_amf(options = {}) ⇒ Object

serialize errors to AMF



354
355
356
357
358
# File 'lib/restfulx/rx_active_record.rb', line 354

def to_amf(options = {})
  options[:amf_version] = 3
  options[:serializer] ||= RestfulX::AMF::RxAMFSerializer.new
  options[:serializer].serialize_errors(Hash[*@errors.to_a.flatten]).to_s
end

#to_fxml(options = {}) ⇒ Object

serializer errors to fXML



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/restfulx/rx_active_record.rb', line 330

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 do |attr, msg|
      next if msg.nil?
      if attr == "base"
        options[:builder].error("message", msg.to_s)
      else
        options[:builder].error("field" => attr.camelcase(:lower), "message" => msg.to_s)
      end
    end
  end
end

#to_json(options = {}) ⇒ Object

serialize errors to JSON



349
350
351
# File 'lib/restfulx/rx_active_record.rb', line 349

def to_json(options = {})
  "{#{'errors'.inspect}:#{to_json_original(options)}}"
end

#to_json_originalObject



327
# File 'lib/restfulx/rx_active_record.rb', line 327

alias_method :to_json_original, :to_json