diff options
author | José Valim <jose.valim@gmail.com> | 2011-11-25 09:58:18 +0000 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-11-25 09:59:35 +0000 |
commit | fcacc6986ab60f1fb2e423a73bf47c7abd7b191d (patch) | |
tree | b4176f645915ac80d2ba43b54aa836d3bc4f7d21 /actionpack/test | |
parent | 4565c871a63dddcb9543eb5be315f152ac551c17 (diff) | |
parent | 696d01f7f4a8ed787924a41cce6df836cd73c46f (diff) | |
download | rails-fcacc6986ab60f1fb2e423a73bf47c7abd7b191d.tar.gz rails-fcacc6986ab60f1fb2e423a73bf47c7abd7b191d.tar.bz2 rails-fcacc6986ab60f1fb2e423a73bf47c7abd7b191d.zip |
Merge branch 'serializers'
This implements the ActiveModel::Serializer object. Includes code, tests, generators and guides.
From José and Yehuda with love.
Conflicts:
railties/CHANGELOG.md
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/render_json_test.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/actionpack/test/controller/render_json_test.rb b/actionpack/test/controller/render_json_test.rb index fc604a2db3..dc09812ba3 100644 --- a/actionpack/test/controller/render_json_test.rb +++ b/actionpack/test/controller/render_json_test.rb @@ -15,9 +15,36 @@ class RenderJsonTest < ActionController::TestCase end end + class JsonSerializer + def initialize(object, scope) + @object, @scope = object, scope + end + + def as_json(*) + { :object => @object.as_json, :scope => @scope.as_json } + end + end + + class JsonSerializable + def initialize(skip=false) + @skip = skip + end + + def active_model_serializer + JsonSerializer unless @skip + end + + def as_json(*) + { :serializable_object => true } + end + end + class TestController < ActionController::Base protect_from_forgery + serialization_scope :current_user + attr_reader :current_user + def self.controller_path 'test' end @@ -61,6 +88,16 @@ class RenderJsonTest < ActionController::TestCase def render_json_without_options render :json => JsonRenderable.new end + + def render_json_with_serializer + @current_user = Struct.new(:as_json).new(:current_user => true) + render :json => JsonSerializable.new + end + + def render_json_with_serializer_api_but_without_serializer + @current_user = Struct.new(:as_json).new(:current_user => true) + render :json => JsonSerializable.new(true) + end end tests TestController @@ -132,4 +169,15 @@ class RenderJsonTest < ActionController::TestCase get :render_json_without_options assert_equal '{"a":"b"}', @response.body end + + def test_render_json_with_serializer + get :render_json_with_serializer + assert_match '"scope":{"current_user":true}', @response.body + assert_match '"object":{"serializable_object":true}', @response.body + end + + def test_render_json_with_serializer_api_but_without_serializer + get :render_json_with_serializer_api_but_without_serializer + assert_match '{"serializable_object":true}', @response.body + end end |