aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorBrian Lopez <seniorlopez@gmail.com>2010-08-02 13:30:08 -0700
committerBrian Lopez <seniorlopez@gmail.com>2010-08-02 13:30:08 -0700
commit954de9940b8f212cafdb9f5e97abc83d27b3e4ff (patch)
tree2cc9e60c1967cc217d55dcb187c4bcfc24c44cd5 /activemodel
parent2353e826b0446917eb655b60c47533f5a9c2a07f (diff)
parent88b5f938cf7d3eb26ad204451a4dbb9c2cf4f571 (diff)
downloadrails-954de9940b8f212cafdb9f5e97abc83d27b3e4ff.tar.gz
rails-954de9940b8f212cafdb9f5e97abc83d27b3e4ff.tar.bz2
rails-954de9940b8f212cafdb9f5e97abc83d27b3e4ff.zip
Merge branch 'master' into mysql2
* master: Bring returning back to ease migration. Remove duplicated logic. Eager loading an association should not change the count of children fix loading of different elements in array then int and string [#5036 state:resolved] Tidy up previous commit. test and fix collection_singular_ids= with string primary keys [#5125 state:resolved] Handle edge cases in the previous patch. Improved how AppGenerator generates the application name. It now detects the current app name whenever possible. This means that renaming the residing directory will not effect the app name generated by AppGenerator. ActiveModel::Errors json serialization to work as Rails 3b4 [#5254 state:resolved] Add missing require in ActiveSupport::HashWithIndifferentAccess [#5189 state:resolved] Add an internal (private API) after_touch callback. [#5271 state:resolved] added failing touch propagation test Makes rails destroy scaffold don't duplicate routes.draw do |map| |map| when using the deprecated syntax Failing test to check for route file corruption if legacy map parameter is used. [#5263 state:open] Corrected the rake test:units and test:functionals description [#5251 state:committed] Use AS::OrderedHash when trusting in the order of the hash Fix label form helper to use I18n and html options, without the need of 'nil' text param:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/errors.rb4
-rw-r--r--activemodel/test/cases/serializeration/json_serialization_test.rb16
-rw-r--r--activemodel/test/cases/validations_test.rb8
3 files changed, 22 insertions, 6 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index bf93126d27..f39678db83 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -169,9 +169,9 @@ module ActiveModel
to_a.to_xml options.reverse_merge(:root => "errors", :skip_types => true)
end
- # Returns an array as JSON representation for this object.
+ # Returns an ActiveSupport::OrderedHash that can be used as the JSON representation for this object.
def as_json(options=nil)
- to_a
+ self
end
# Adds +message+ to the error messages on +attribute+, which will be returned on a call to
diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb
index 04b50e5bb8..1ac991a8f1 100644
--- a/activemodel/test/cases/serializeration/json_serialization_test.rb
+++ b/activemodel/test/cases/serializeration/json_serialization_test.rb
@@ -89,7 +89,7 @@ class JsonSerializationTest < ActiveModel::TestCase
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
- test "methds are called on object" do
+ test "methods are called on object" do
# Define methods on fixture.
def @contact.label; "Has cheezburger"; end
def @contact.favorite_quote; "Constraints are liberating"; end
@@ -102,4 +102,18 @@ class JsonSerializationTest < ActiveModel::TestCase
assert_match %r{"label":"Has cheezburger"}, methods_json
assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
end
+
+ test "should return OrderedHash for errors" do
+ car = Automobile.new
+
+ # run the validation
+ car.valid?
+
+ hash = ActiveSupport::OrderedHash.new
+ hash[:make] = "can't be blank"
+ hash[:model] = "is too short (minimum is 2 characters)"
+ assert_equal hash.to_json, car.errors.to_json
+ end
+
+
end
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index e94d8ce88c..8d6bdeb6a5 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -170,9 +170,11 @@ class ValidationsTest < ActiveModel::TestCase
assert_match %r{<errors>}, xml
assert_match %r{<error>Title can't be blank</error>}, xml
assert_match %r{<error>Content can't be blank</error>}, xml
-
- json = t.errors.to_json
- assert_equal t.errors.to_a.to_json, json
+
+ hash = ActiveSupport::OrderedHash.new
+ hash[:title] = "can't be blank"
+ hash[:content] = "can't be blank"
+ assert_equal t.errors.to_json, hash.to_json
end
def test_validation_order