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