aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/errors.rb
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2012-02-17 12:12:10 +0200
committerBogdan Gusiev <agresso@gmail.com>2012-02-20 11:17:11 +0200
commit534dc4ca1a730bff3fc9fe116bec25172c7c8afc (patch)
tree905a0e87a2adea890348e29e37e3906dc61ef018 /activemodel/lib/active_model/errors.rb
parentdecafdd57a2c992a15df894cc43cd0a7ff6ea985 (diff)
downloadrails-534dc4ca1a730bff3fc9fe116bec25172c7c8afc.tar.gz
rails-534dc4ca1a730bff3fc9fe116bec25172c7c8afc.tar.bz2
rails-534dc4ca1a730bff3fc9fe116bec25172c7c8afc.zip
AM::Errors: allow :full_messages parameter for #as_json
Diffstat (limited to 'activemodel/lib/active_model/errors.rb')
-rw-r--r--activemodel/lib/active_model/errors.rb17
1 files changed, 14 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index e548aa975d..042f9cd7e2 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -206,12 +206,23 @@ module ActiveModel
end
# Returns an Hash that can be used as the JSON representation for this object.
+ # Options:
+ # * <tt>:full_messages</tt> - determines if json object should contain
+ # full messages or not. Default: <tt>false</tt>.
def as_json(options=nil)
- to_hash
+ to_hash(options && options[:full_messages])
end
- def to_hash
- messages.dup
+ def to_hash(full_messages = false)
+ if full_messages
+ messages = {}
+ self.messages.each do |attribute, array|
+ messages[attribute] = array.map{|message| full_message(attribute, message) }
+ end
+ messages
+ else
+ self.messages.dup
+ end
end
# Adds +message+ to the error messages on +attribute+. More than one error can be added to the same