aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-06-26 11:57:43 +0200
committerJosé Valim <jose.valim@gmail.com>2010-06-26 12:01:13 +0200
commit7bd85a8fc2d216a5e2b1d0380df572f782a54d1c (patch)
tree0e9a1b63353b01244bce81bb7262bfb87e92b997 /activemodel/lib
parentcfaaed3f40e820d2b4d60c2d8bc1f9a005cee086 (diff)
downloadrails-7bd85a8fc2d216a5e2b1d0380df572f782a54d1c.tar.gz
rails-7bd85a8fc2d216a5e2b1d0380df572f782a54d1c.tar.bz2
rails-7bd85a8fc2d216a5e2b1d0380df572f782a54d1c.zip
Work around the fact the JSON gem was overwriting to_json implementation for all Ruby core classes.
This is required because the JSON gem is incompatible with Rails behavior and was not allowing ActiveModel::Errors to be serialized. So we need to ensure Rails implementation is the one triggered. [#4890 state:resolved]
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/errors.rb17
1 files changed, 7 insertions, 10 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index ae7822d8d5..d42fc5291d 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -3,6 +3,7 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/ordered_hash'
module ActiveModel
@@ -164,15 +165,12 @@ module ActiveModel
# # <error>name must be specified</error>
# # </errors>
def to_xml(options={})
- require 'builder' unless defined? ::Builder
- 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|
- to_a.each { |error| e.error(error) }
- end
+ to_a.to_xml options.reverse_merge(:root => "errors", :skip_types => true)
+ end
+
+ # Returns an array as JSON representation for this object.
+ def as_json(options=nil)
+ to_a
end
# Adds +message+ to the error messages on +attribute+, which will be returned on a call to
@@ -283,7 +281,6 @@ module ActiveModel
# <li><tt>errors.attributes.title.blank</tt></li>
# <li><tt>errors.messages.blank</tt></li>
# </ol>
-
def generate_message(attribute, type = :invalid, options = {})
type = options.delete(:message) if options[:message].is_a?(Symbol)