gpi-active_model_serializers 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.travis.yml +18 -0
- data/CHANGELOG.md +113 -0
- data/CONTRIBUTING.md +20 -0
- data/DESIGN.textile +586 -0
- data/Gemfile +12 -0
- data/Gemfile.edge +9 -0
- data/MIT-LICENSE.txt +21 -0
- data/README.md +716 -0
- data/Rakefile +18 -0
- data/active_model_serializers.gemspec +28 -0
- data/bench/perf.rb +43 -0
- data/cruft.md +19 -0
- data/lib/action_controller/serialization.rb +58 -0
- data/lib/active_model/array_serializer.rb +65 -0
- data/lib/active_model/serializable.rb +49 -0
- data/lib/active_model/serializer.rb +475 -0
- data/lib/active_model/serializer/associations.rb +191 -0
- data/lib/active_model/serializer/caching.rb +37 -0
- data/lib/active_model/serializer/version.rb +5 -0
- data/lib/active_model_serializers.rb +95 -0
- data/lib/active_record/serializer_override.rb +16 -0
- data/lib/generators/resource_override.rb +13 -0
- data/lib/generators/serializer/USAGE +9 -0
- data/lib/generators/serializer/serializer_generator.rb +36 -0
- data/lib/generators/serializer/templates/serializer.rb +8 -0
- data/test/array_serializer_test.rb +85 -0
- data/test/association_test.rb +592 -0
- data/test/caching_test.rb +96 -0
- data/test/generators_test.rb +73 -0
- data/test/no_serialization_scope_test.rb +34 -0
- data/test/serialization_scope_name_test.rb +99 -0
- data/test/serialization_test.rb +394 -0
- data/test/serializer_support_test.rb +51 -0
- data/test/serializer_test.rb +1521 -0
- data/test/test_fakes.rb +202 -0
- data/test/test_helper.rb +41 -0
- metadata +176 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class CachingTest < ActiveModel::TestCase
|
4
|
+
class NullStore
|
5
|
+
def fetch(key)
|
6
|
+
return store[key] if store[key]
|
7
|
+
|
8
|
+
store[key] = yield
|
9
|
+
end
|
10
|
+
|
11
|
+
def clear
|
12
|
+
store.clear
|
13
|
+
end
|
14
|
+
|
15
|
+
def store
|
16
|
+
@store ||= {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def read(key)
|
20
|
+
store[key]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Programmer
|
25
|
+
def name
|
26
|
+
'Adam'
|
27
|
+
end
|
28
|
+
|
29
|
+
def skills
|
30
|
+
%w(ruby)
|
31
|
+
end
|
32
|
+
|
33
|
+
def read_attribute_for_serialization(name)
|
34
|
+
send name
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_serializers_have_a_cache_store
|
39
|
+
ActiveModel::Serializer.cache = NullStore.new
|
40
|
+
|
41
|
+
assert_kind_of NullStore, ActiveModel::Serializer.cache
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_serializers_can_enable_caching
|
45
|
+
serializer = Class.new(ActiveModel::Serializer) do
|
46
|
+
cached true
|
47
|
+
end
|
48
|
+
|
49
|
+
assert serializer.perform_caching
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_serializers_use_cache
|
53
|
+
serializer = Class.new(ActiveModel::Serializer) do
|
54
|
+
cached true
|
55
|
+
attributes :name, :skills
|
56
|
+
|
57
|
+
def self.to_s
|
58
|
+
'serializer'
|
59
|
+
end
|
60
|
+
|
61
|
+
def cache_key
|
62
|
+
object.name
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
serializer.cache = NullStore.new
|
67
|
+
instance = serializer.new Programmer.new
|
68
|
+
|
69
|
+
instance.to_json
|
70
|
+
|
71
|
+
assert_equal(instance.serializable_hash, serializer.cache.read('serializer/Adam/serialize'))
|
72
|
+
assert_equal(instance.to_json, serializer.cache.read('serializer/Adam/to-json'))
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_array_serializer_uses_cache
|
76
|
+
serializer = Class.new(ActiveModel::ArraySerializer) do
|
77
|
+
cached true
|
78
|
+
|
79
|
+
def self.to_s
|
80
|
+
'array_serializer'
|
81
|
+
end
|
82
|
+
|
83
|
+
def cache_key
|
84
|
+
'cache-key'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
serializer.cache = NullStore.new
|
89
|
+
instance = serializer.new [Programmer.new]
|
90
|
+
|
91
|
+
instance.to_json
|
92
|
+
|
93
|
+
assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serialize')
|
94
|
+
assert_equal instance.to_json, serializer.cache.read('array_serializer/cache-key/to-json')
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Foo < Rails::Application
|
4
|
+
if Rails.version.to_s.start_with? '4'
|
5
|
+
config.eager_load = false
|
6
|
+
config.secret_key_base = 'abc123'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Rails.application.load_generators
|
11
|
+
|
12
|
+
require 'generators/serializer/serializer_generator'
|
13
|
+
|
14
|
+
class SerializerGeneratorTest < Rails::Generators::TestCase
|
15
|
+
destination File.expand_path("../tmp", __FILE__)
|
16
|
+
setup :prepare_destination
|
17
|
+
|
18
|
+
tests Rails::Generators::SerializerGenerator
|
19
|
+
arguments %w(account name:string description:text business:references)
|
20
|
+
|
21
|
+
def test_generates_a_serializer
|
22
|
+
run_generator
|
23
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ActiveModel::Serializer/
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_generates_a_namespaced_serializer
|
27
|
+
run_generator ["admin/account"]
|
28
|
+
assert_file "app/serializers/admin/account_serializer.rb", /class Admin::AccountSerializer < ActiveModel::Serializer/
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_uses_application_serializer_if_one_exists
|
32
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
33
|
+
run_generator
|
34
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < ApplicationSerializer/
|
35
|
+
ensure
|
36
|
+
Object.send :remove_const, :ApplicationSerializer
|
37
|
+
end
|
38
|
+
|
39
|
+
# def test_uses_namespace_application_serializer_if_one_exists
|
40
|
+
# Object.const_set(:SerializerNamespace, Module.new)
|
41
|
+
# SerializerNamespace.const_set(:ApplicationSerializer, Class.new)
|
42
|
+
# Rails::Generators.namespace = SerializerNamespace
|
43
|
+
# run_generator
|
44
|
+
# assert_file "app/serializers/serializer_namespace/account_serializer.rb",
|
45
|
+
# /module SerializerNamespace\n class AccountSerializer < ApplicationSerializer/
|
46
|
+
# ensure
|
47
|
+
# Object.send :remove_const, :SerializerNamespace
|
48
|
+
# Rails::Generators.namespace = nil
|
49
|
+
# end
|
50
|
+
|
51
|
+
def test_uses_given_parent
|
52
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
53
|
+
run_generator ["Account", "--parent=MySerializer"]
|
54
|
+
assert_file "app/serializers/account_serializer.rb", /class AccountSerializer < MySerializer/
|
55
|
+
ensure
|
56
|
+
Object.send :remove_const, :ApplicationSerializer
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_generates_attributes_and_associations
|
60
|
+
run_generator
|
61
|
+
assert_file "app/serializers/account_serializer.rb" do |serializer|
|
62
|
+
assert_match(/^ attributes :id, :name, :description$/, serializer)
|
63
|
+
assert_match(/^ has_one :business$/, serializer)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_with_no_attributes_does_not_add_extra_space
|
68
|
+
run_generator ["account"]
|
69
|
+
assert_file "app/serializers/account_serializer.rb" do |content|
|
70
|
+
assert_no_match /\n\nend/, content
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class NoSerializationScopeTest < ActionController::TestCase
|
4
|
+
class ScopeSerializer
|
5
|
+
def initialize(object, options)
|
6
|
+
@object, @options = object, options
|
7
|
+
end
|
8
|
+
|
9
|
+
def as_json(*)
|
10
|
+
{ scope: @options[:scope].as_json }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ScopeSerializable
|
15
|
+
def active_model_serializer
|
16
|
+
ScopeSerializer
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class NoSerializationScopeController < ActionController::Base
|
21
|
+
serialization_scope nil
|
22
|
+
|
23
|
+
def index
|
24
|
+
render json: ScopeSerializable.new
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
tests NoSerializationScopeController
|
29
|
+
|
30
|
+
def test_disabled_serialization_scope
|
31
|
+
get :index, format: :json
|
32
|
+
assert_equal '{"scope":null}', @response.body
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
class DefaultScopeNameTest < ActionController::TestCase
|
5
|
+
TestUser = Struct.new(:name, :admin)
|
6
|
+
|
7
|
+
class UserSerializer < ActiveModel::Serializer
|
8
|
+
attributes :admin?
|
9
|
+
def admin?
|
10
|
+
current_user.admin
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class UserTestController < ActionController::Base
|
15
|
+
protect_from_forgery
|
16
|
+
|
17
|
+
before_filter { request.format = :json }
|
18
|
+
|
19
|
+
def current_user
|
20
|
+
TestUser.new('Pete', false)
|
21
|
+
end
|
22
|
+
|
23
|
+
def render_new_user
|
24
|
+
render json: TestUser.new('pete', false), serializer: UserSerializer
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
tests UserTestController
|
29
|
+
|
30
|
+
def test_default_scope_name
|
31
|
+
get :render_new_user
|
32
|
+
assert_equal '{"user":{"admin":false}}', @response.body
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class SerializationScopeNameTest < ActionController::TestCase
|
37
|
+
TestUser = Struct.new(:name, :admin)
|
38
|
+
|
39
|
+
class AdminUserSerializer < ActiveModel::Serializer
|
40
|
+
attributes :admin?
|
41
|
+
def admin?
|
42
|
+
current_admin.admin
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class AdminUserTestController < ActionController::Base
|
47
|
+
protect_from_forgery
|
48
|
+
|
49
|
+
serialization_scope :current_admin
|
50
|
+
before_filter { request.format = :json }
|
51
|
+
|
52
|
+
def current_admin
|
53
|
+
TestUser.new('Bob', true)
|
54
|
+
end
|
55
|
+
|
56
|
+
def render_new_user
|
57
|
+
render json: TestUser.new('pete', false), serializer: AdminUserSerializer
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
tests AdminUserTestController
|
62
|
+
|
63
|
+
def test_override_scope_name_with_controller
|
64
|
+
get :render_new_user
|
65
|
+
assert_equal '{"admin_user":{"admin":true}}', @response.body
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class SerializationActionScopeOverrideTest < ActionController::TestCase
|
70
|
+
TestUser = Struct.new(:name, :admin)
|
71
|
+
|
72
|
+
class AdminUserSerializer < ActiveModel::Serializer
|
73
|
+
attributes :admin?
|
74
|
+
def admin?
|
75
|
+
current_admin.admin
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class AdminUserTestController < ActionController::Base
|
80
|
+
protect_from_forgery
|
81
|
+
before_filter { request.format = :json }
|
82
|
+
|
83
|
+
def current_admin
|
84
|
+
TestUser.new('Bob', true)
|
85
|
+
end
|
86
|
+
|
87
|
+
def render_new_user
|
88
|
+
render json: TestUser.new('pete', false), serializer: AdminUserSerializer, scope: current_admin, scope_name: :current_admin
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
tests AdminUserTestController
|
93
|
+
|
94
|
+
def test_override_scope_name_with_controller
|
95
|
+
get :render_new_user
|
96
|
+
assert_equal '{"admin_user":{"admin":true}}', @response.body
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,394 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
class RenderJsonTest < ActionController::TestCase
|
5
|
+
class JsonRenderable
|
6
|
+
def as_json(options={})
|
7
|
+
hash = { a: :b, c: :d, e: :f }
|
8
|
+
hash.except!(*options[:except]) if options[:except]
|
9
|
+
hash
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_json(options = {})
|
13
|
+
super except: [:c, :e]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class JsonSerializer
|
18
|
+
def initialize(object, options={})
|
19
|
+
@object, @options = object, options
|
20
|
+
end
|
21
|
+
|
22
|
+
def as_json(*)
|
23
|
+
hash = { object: serializable_hash, scope: @options[:scope].as_json }
|
24
|
+
hash.merge!(options: true) if @options[:options]
|
25
|
+
hash.merge!(check_defaults: true) if @options[:check_defaults]
|
26
|
+
hash
|
27
|
+
end
|
28
|
+
|
29
|
+
def serializable_hash
|
30
|
+
@object.as_json
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class JsonSerializable
|
35
|
+
def initialize(skip=false)
|
36
|
+
@skip = skip
|
37
|
+
end
|
38
|
+
|
39
|
+
def active_model_serializer
|
40
|
+
JsonSerializer unless @skip
|
41
|
+
end
|
42
|
+
|
43
|
+
def as_json(*)
|
44
|
+
{ serializable_object: true }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class CustomSerializer
|
49
|
+
def initialize(*)
|
50
|
+
end
|
51
|
+
|
52
|
+
def as_json(*)
|
53
|
+
{ hello: true }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class AnotherCustomSerializer
|
58
|
+
def initialize(*)
|
59
|
+
end
|
60
|
+
|
61
|
+
def as_json(*)
|
62
|
+
{ rails: 'rocks' }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class DummyCustomSerializer < ActiveModel::Serializer
|
67
|
+
attributes :id
|
68
|
+
end
|
69
|
+
|
70
|
+
class HypermediaSerializable
|
71
|
+
def active_model_serializer
|
72
|
+
HypermediaSerializer
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class HypermediaSerializer < ActiveModel::Serializer
|
77
|
+
def as_json(*)
|
78
|
+
{ link: hypermedia_url }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class CustomArraySerializer < ActiveModel::ArraySerializer
|
83
|
+
self.root = "items"
|
84
|
+
end
|
85
|
+
|
86
|
+
class TestController < ActionController::Base
|
87
|
+
protect_from_forgery
|
88
|
+
|
89
|
+
serialization_scope :current_user
|
90
|
+
attr_reader :current_user
|
91
|
+
|
92
|
+
def self.controller_path
|
93
|
+
'test'
|
94
|
+
end
|
95
|
+
|
96
|
+
def render_json_nil
|
97
|
+
render json: nil
|
98
|
+
end
|
99
|
+
|
100
|
+
def render_json_render_to_string
|
101
|
+
render text: render_to_string(json: '[]')
|
102
|
+
end
|
103
|
+
|
104
|
+
def render_json_hello_world
|
105
|
+
render json: ActiveSupport::JSON.encode(hello: 'world')
|
106
|
+
end
|
107
|
+
|
108
|
+
def render_json_hello_world_with_status
|
109
|
+
render json: ActiveSupport::JSON.encode(hello: 'world'), status: 401
|
110
|
+
end
|
111
|
+
|
112
|
+
def render_json_hello_world_with_callback
|
113
|
+
render json: ActiveSupport::JSON.encode(hello: 'world'), callback: 'alert'
|
114
|
+
end
|
115
|
+
|
116
|
+
def render_json_with_custom_content_type
|
117
|
+
render json: ActiveSupport::JSON.encode(hello: 'world'), content_type: 'text/javascript'
|
118
|
+
end
|
119
|
+
|
120
|
+
def render_symbol_json
|
121
|
+
render json: ActiveSupport::JSON.encode(hello: 'world')
|
122
|
+
end
|
123
|
+
|
124
|
+
def render_json_nil_with_custom_serializer
|
125
|
+
render json: nil, serializer: DummyCustomSerializer
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
def render_json_with_extra_options
|
130
|
+
render json: JsonRenderable.new, except: [:c, :e]
|
131
|
+
end
|
132
|
+
|
133
|
+
def render_json_without_options
|
134
|
+
render json: JsonRenderable.new
|
135
|
+
end
|
136
|
+
|
137
|
+
def render_json_with_serializer
|
138
|
+
@current_user = Struct.new(:as_json).new(current_user: true)
|
139
|
+
render json: JsonSerializable.new
|
140
|
+
end
|
141
|
+
|
142
|
+
def render_json_with_serializer_and_implicit_root
|
143
|
+
@current_user = Struct.new(:as_json).new(current_user: true)
|
144
|
+
render json: [JsonSerializable.new]
|
145
|
+
end
|
146
|
+
|
147
|
+
def render_json_with_serializer_and_options
|
148
|
+
@current_user = Struct.new(:as_json).new(current_user: true)
|
149
|
+
render json: JsonSerializable.new, options: true
|
150
|
+
end
|
151
|
+
|
152
|
+
def render_json_with_serializer_and_scope_option
|
153
|
+
@current_user = Struct.new(:as_json).new(current_user: true)
|
154
|
+
scope = Struct.new(:as_json).new(current_user: false)
|
155
|
+
render json: JsonSerializable.new, scope: scope
|
156
|
+
end
|
157
|
+
|
158
|
+
def render_json_with_serializer_api_but_without_serializer
|
159
|
+
@current_user = Struct.new(:as_json).new(current_user: true)
|
160
|
+
render json: JsonSerializable.new(true)
|
161
|
+
end
|
162
|
+
|
163
|
+
# To specify a custom serializer for an object, use :serializer.
|
164
|
+
def render_json_with_custom_serializer
|
165
|
+
render json: Object.new, serializer: CustomSerializer
|
166
|
+
end
|
167
|
+
|
168
|
+
# To specify a custom serializer for each item in the Array, use :each_serializer.
|
169
|
+
def render_json_array_with_custom_serializer
|
170
|
+
render json: [Object.new], each_serializer: CustomSerializer
|
171
|
+
end
|
172
|
+
|
173
|
+
def render_json_array_with_wrong_option
|
174
|
+
render json: [Object.new], serializer: CustomSerializer
|
175
|
+
end
|
176
|
+
|
177
|
+
def render_json_with_links
|
178
|
+
render json: HypermediaSerializable.new
|
179
|
+
end
|
180
|
+
|
181
|
+
def render_json_array_with_no_root
|
182
|
+
render json: [], root: false
|
183
|
+
end
|
184
|
+
|
185
|
+
def render_json_empty_array
|
186
|
+
render json: []
|
187
|
+
end
|
188
|
+
|
189
|
+
def render_json_array_with_custom_array_serializer
|
190
|
+
render json: [], serializer: CustomArraySerializer
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
private
|
195
|
+
def default_serializer_options
|
196
|
+
defaults = {}
|
197
|
+
defaults.merge!(check_defaults: true) if params[:check_defaults]
|
198
|
+
defaults.merge!(root: :awesome) if params[:check_default_root]
|
199
|
+
defaults.merge!(scope: :current_admin) if params[:check_default_scope]
|
200
|
+
defaults.merge!(serializer: AnotherCustomSerializer) if params[:check_default_serializer]
|
201
|
+
defaults.merge!(each_serializer: AnotherCustomSerializer) if params[:check_default_each_serializer]
|
202
|
+
defaults
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
tests TestController
|
207
|
+
|
208
|
+
def setup
|
209
|
+
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
210
|
+
# a more accurate simulation of what happens in "real life".
|
211
|
+
super
|
212
|
+
@controller.logger = Logger.new(nil)
|
213
|
+
|
214
|
+
@request.host = "www.nextangle.com"
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_render_json_nil
|
218
|
+
get :render_json_nil
|
219
|
+
assert_equal 'null', @response.body
|
220
|
+
assert_equal 'application/json', @response.content_type
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_render_json_render_to_string
|
224
|
+
get :render_json_render_to_string
|
225
|
+
assert_equal '[]', @response.body
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_render_json_nil_with_custom_serializer
|
229
|
+
get :render_json_nil_with_custom_serializer
|
230
|
+
assert_equal "{\"dummy_custom\":null}", @response.body
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_render_json
|
234
|
+
get :render_json_hello_world
|
235
|
+
assert_equal '{"hello":"world"}', @response.body
|
236
|
+
assert_equal 'application/json', @response.content_type
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_render_json_with_status
|
240
|
+
get :render_json_hello_world_with_status
|
241
|
+
assert_equal '{"hello":"world"}', @response.body
|
242
|
+
assert_equal 401, @response.status
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_render_json_with_callback
|
246
|
+
get :render_json_hello_world_with_callback
|
247
|
+
assert_equal 'alert({"hello":"world"})', @response.body
|
248
|
+
# For JSONP, Rails 3 uses application/json, but Rails 4 uses text/javascript
|
249
|
+
assert_match %r(application/json|text/javascript), @response.content_type.to_s
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_render_json_with_custom_content_type
|
253
|
+
get :render_json_with_custom_content_type
|
254
|
+
assert_equal '{"hello":"world"}', @response.body
|
255
|
+
assert_equal 'text/javascript', @response.content_type
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_render_symbol_json
|
259
|
+
get :render_symbol_json
|
260
|
+
assert_equal '{"hello":"world"}', @response.body
|
261
|
+
assert_equal 'application/json', @response.content_type
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_render_json_forwards_extra_options
|
265
|
+
get :render_json_with_extra_options
|
266
|
+
assert_equal '{"a":"b"}', @response.body
|
267
|
+
assert_equal 'application/json', @response.content_type
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_render_json_calls_to_json_from_object
|
271
|
+
get :render_json_without_options
|
272
|
+
assert_equal '{"a":"b"}', @response.body
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_render_json_with_serializer
|
276
|
+
get :render_json_with_serializer
|
277
|
+
assert_match '"scope":{"current_user":true}', @response.body
|
278
|
+
assert_match '"object":{"serializable_object":true}', @response.body
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_render_json_with_serializer_checking_defaults
|
282
|
+
get :render_json_with_serializer, check_defaults: true
|
283
|
+
assert_match '"scope":{"current_user":true}', @response.body
|
284
|
+
assert_match '"object":{"serializable_object":true}', @response.body
|
285
|
+
assert_match '"check_defaults":true', @response.body
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_render_json_with_serializer_checking_default_serailizer
|
289
|
+
get :render_json_with_serializer, check_default_serializer: true
|
290
|
+
assert_match '{"rails":"rocks"}', @response.body
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_render_json_with_serializer_checking_default_scope
|
294
|
+
get :render_json_with_serializer, check_default_scope: true
|
295
|
+
assert_match '"scope":"current_admin"', @response.body
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_render_json_with_serializer_and_implicit_root
|
299
|
+
get :render_json_with_serializer_and_implicit_root
|
300
|
+
assert_match '"test":[{"serializable_object":true}]', @response.body
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_render_json_with_serializer_and_implicit_root_checking_default_each_serailizer
|
304
|
+
get :render_json_with_serializer_and_implicit_root, check_default_each_serializer: true
|
305
|
+
assert_match '"test":[{"rails":"rocks"}]', @response.body
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_render_json_with_serializer_and_options
|
309
|
+
get :render_json_with_serializer_and_options
|
310
|
+
assert_match '"scope":{"current_user":true}', @response.body
|
311
|
+
assert_match '"object":{"serializable_object":true}', @response.body
|
312
|
+
assert_match '"options":true', @response.body
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_render_json_with_serializer_and_scope_option
|
316
|
+
get :render_json_with_serializer_and_scope_option
|
317
|
+
assert_match '"scope":{"current_user":false}', @response.body
|
318
|
+
end
|
319
|
+
|
320
|
+
def test_render_json_with_serializer_and_scope_option_checking_default_scope
|
321
|
+
get :render_json_with_serializer_and_scope_option, check_default_scope: true
|
322
|
+
assert_match '"scope":{"current_user":false}', @response.body
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_render_json_with_serializer_api_but_without_serializer
|
326
|
+
get :render_json_with_serializer_api_but_without_serializer
|
327
|
+
assert_match '{"serializable_object":true}', @response.body
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_render_json_with_custom_serializer
|
331
|
+
get :render_json_with_custom_serializer
|
332
|
+
assert_match '{"hello":true}', @response.body
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_render_json_with_custom_serializer_checking_default_serailizer
|
336
|
+
get :render_json_with_custom_serializer, check_default_serializer: true
|
337
|
+
assert_match '{"hello":true}', @response.body
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_render_json_array_with_custom_serializer
|
341
|
+
get :render_json_array_with_custom_serializer
|
342
|
+
assert_match '{"test":[{"hello":true}]}', @response.body
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_render_json_array_with_wrong_option
|
346
|
+
assert_raise ArgumentError do
|
347
|
+
get :render_json_array_with_wrong_option
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_render_json_array_with_custom_serializer_checking_default_each_serailizer
|
352
|
+
get :render_json_array_with_custom_serializer, check_default_each_serializer: true
|
353
|
+
assert_match '{"test":[{"hello":true}]}', @response.body
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_render_json_with_links
|
357
|
+
get :render_json_with_links
|
358
|
+
assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_render_json_array_with_no_root
|
362
|
+
get :render_json_array_with_no_root
|
363
|
+
assert_equal '[]', @response.body
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_render_json_array_with_no_root_checking_default_root
|
367
|
+
get :render_json_array_with_no_root, check_default_root: true
|
368
|
+
assert_equal '[]', @response.body
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_render_json_empty_array
|
372
|
+
get :render_json_empty_array
|
373
|
+
assert_equal '{"test":[]}', @response.body
|
374
|
+
end
|
375
|
+
|
376
|
+
def test_render_json_empty_array_checking_default_root
|
377
|
+
get :render_json_empty_array, check_default_root: true
|
378
|
+
assert_equal '{"awesome":[]}', @response.body
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_render_json_empty_array_with_array_serializer_root_false
|
382
|
+
ActiveModel::ArraySerializer.root = false
|
383
|
+
get :render_json_empty_array
|
384
|
+
assert_equal '[]', @response.body
|
385
|
+
ensure # teardown
|
386
|
+
ActiveModel::ArraySerializer.root = nil
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_render_json_array_with_custom_array_serializer
|
390
|
+
get :render_json_array_with_custom_array_serializer
|
391
|
+
assert_equal '{"items":[]}', @response.body
|
392
|
+
end
|
393
|
+
|
394
|
+
end
|