diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-02-20 01:25:28 -0800 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-02-20 01:25:28 -0800 |
commit | e1c381603d277bc4d6cb430d53a5f1d1aca377e7 (patch) | |
tree | 11b93900af2583008180799f50e141703af83e78 /activemodel | |
parent | 1ef12fd1727909f7a33fd7f2d750d48d7f9b47e9 (diff) | |
parent | 534dc4ca1a730bff3fc9fe116bec25172c7c8afc (diff) | |
download | rails-e1c381603d277bc4d6cb430d53a5f1d1aca377e7.tar.gz rails-e1c381603d277bc4d6cb430d53a5f1d1aca377e7.tar.bz2 rails-e1c381603d277bc4d6cb430d53a5f1d1aca377e7.zip |
Merge pull request #5075 from bogdan/json_full_messages
AM::Errors: allow :full_messages parameter for #to_json
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 17 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 10 |
3 files changed, 26 insertions, 3 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 258c3681f6..e4779c90d8 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,5 @@ +* `AM::Errors#to_json`: support `:full_messages` parameter *Bogdan Gusiev* + * Trim down Active Model API by removing `valid?` and `errors.full_messages` *José Valim* ## Rails 3.2.0 (January 20, 2012) ## 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 diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 7a610e0c2c..3bc0d58351 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -184,6 +184,16 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["is invalid"], hash[:email] end + test 'should return a JSON hash representation of the errors with full messages' do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:name, "can not be nil") + person.errors.add(:email, "is invalid") + hash = person.errors.as_json(:full_messages => true) + assert_equal ["name can not be blank", "name can not be nil"], hash[:name] + assert_equal ["email is invalid"], hash[:email] + end + test "generate_message should work without i18n_scope" do person = Person.new assert !Person.respond_to?(:i18n_scope) |