active_model_serializers 0.8.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -5
- data/CONTRIBUTING.md +20 -0
- data/DESIGN.textile +4 -4
- data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +307 -99
- data/lib/action_controller/serialization.rb +35 -16
- data/lib/action_controller/serialization_test_case.rb +79 -0
- data/lib/active_model/array_serializer.rb +40 -79
- data/lib/active_model/default_serializer.rb +32 -0
- data/lib/active_model/serializable.rb +40 -0
- data/lib/active_model/serializer/associations.rb +71 -202
- 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.rb +186 -433
- data/lib/active_model/serializer_support.rb +5 -0
- data/lib/active_model_serializers.rb +13 -88
- data/test/fixtures/active_record.rb +92 -0
- data/test/fixtures/poro.rb +75 -0
- data/test/integration/action_controller/serialization_test.rb +287 -0
- data/test/integration/action_controller/serialization_test_case_test.rb +61 -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 +64 -0
- data/test/integration/generators/serializer_generator_test.rb +41 -0
- data/test/test_app.rb +11 -0
- data/test/test_helper.rb +10 -18
- data/test/unit/active_model/array_serializer/except_test.rb +18 -0
- data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
- data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
- data/test/unit/active_model/array_serializer/only_test.rb +18 -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 +199 -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 +88 -0
- data/test/unit/active_model/serializer/filter_test.rb +69 -0
- data/test/unit/active_model/serializer/has_many_test.rb +230 -0
- data/test/unit/active_model/serializer/has_one_test.rb +207 -0
- data/test/unit/active_model/serializer/key_format_test.rb +25 -0
- data/test/unit/active_model/serializer/meta_test.rb +39 -0
- data/test/unit/active_model/serializer/options_test.rb +15 -0
- data/test/unit/active_model/serializer/root_test.rb +117 -0
- data/test/unit/active_model/serializer/scope_test.rb +49 -0
- metadata +86 -62
- data/.gitignore +0 -18
- data/.travis.yml +0 -28
- data/Gemfile +0 -4
- data/Gemfile.edge +0 -9
- data/Rakefile +0 -18
- data/active_model_serializers.gemspec +0 -24
- 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 -96
- 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 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fixtures/active_record'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
class Serializer
|
6
|
+
class ActiveRecordTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@post = ARPost.first
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_serialization_embedding_objects
|
12
|
+
post_serializer = ARPostSerializer.new(@post)
|
13
|
+
|
14
|
+
assert_equal({
|
15
|
+
'ar_post' => {
|
16
|
+
title: 'New post', body: 'A body!!!',
|
17
|
+
ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'happy' }, { name: 'whiny' }] },
|
18
|
+
{ body: 'i liked it', ar_tags: [{:name=>"happy"}, {:name=>"short"}] }],
|
19
|
+
ar_tags: [{ name: 'short' }, { name: 'whiny' }],
|
20
|
+
ar_section: { 'name' => 'ruby' }
|
21
|
+
}
|
22
|
+
}, post_serializer.as_json)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_serialization_embedding_ids
|
26
|
+
post_serializer = ARPostSerializer.new(@post)
|
27
|
+
|
28
|
+
embed(ARPostSerializer, embed: :ids) do
|
29
|
+
assert_equal({
|
30
|
+
'ar_post' => {
|
31
|
+
title: 'New post', body: 'A body!!!',
|
32
|
+
'ar_comment_ids' => [1, 2],
|
33
|
+
'ar_tag_ids' => [1, 2],
|
34
|
+
'ar_section_id' => 1
|
35
|
+
}
|
36
|
+
}, post_serializer.as_json)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_serialization_embedding_ids_including_in_root
|
41
|
+
post_serializer = ARPostSerializer.new(@post)
|
42
|
+
|
43
|
+
embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
|
44
|
+
embed(ARCommentSerializer, embed: :ids, embed_in_root: true) do
|
45
|
+
assert_equal({
|
46
|
+
'ar_post' => {
|
47
|
+
title: 'New post', body: 'A body!!!',
|
48
|
+
'ar_comment_ids' => [1, 2],
|
49
|
+
'ar_tag_ids' => [1, 2],
|
50
|
+
'ar_section_id' => 1
|
51
|
+
},
|
52
|
+
ar_comments: [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
|
53
|
+
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
|
54
|
+
ar_tags: [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
|
55
|
+
'ar_sections' => [{ 'name' => 'ruby' }]
|
56
|
+
}, post_serializer.as_json)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def embed(serializer_class, options = {})
|
64
|
+
old_assocs = Hash[serializer_class._associations.to_a.map { |(name, association)| [name, association.dup] }]
|
65
|
+
|
66
|
+
serializer_class._associations.each_value do |association|
|
67
|
+
association.embed = options[:embed]
|
68
|
+
association.embed_in_root = options[:embed_in_root]
|
69
|
+
end
|
70
|
+
|
71
|
+
yield
|
72
|
+
ensure
|
73
|
+
serializer_class._associations = old_assocs
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails'
|
3
|
+
require 'active_model/serializer/railtie'
|
4
|
+
require 'test_app'
|
5
|
+
|
6
|
+
class ResourceGeneratorTest < Rails::Generators::TestCase
|
7
|
+
destination File.expand_path('../../../tmp', __FILE__)
|
8
|
+
setup :prepare_destination, :copy_routes
|
9
|
+
|
10
|
+
tests Rails::Generators::ResourceGenerator
|
11
|
+
arguments %w(account)
|
12
|
+
|
13
|
+
def test_serializer_file_is_generated
|
14
|
+
run_generator
|
15
|
+
|
16
|
+
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def copy_routes
|
22
|
+
config_dir = File.join(destination_root, 'config')
|
23
|
+
FileUtils.mkdir_p(config_dir)
|
24
|
+
File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }')
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails'
|
3
|
+
require 'active_model/serializer/railtie'
|
4
|
+
require 'test_app'
|
5
|
+
|
6
|
+
class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
7
|
+
destination File.expand_path('../../../tmp', __FILE__)
|
8
|
+
setup :prepare_destination
|
9
|
+
|
10
|
+
tests Rails::Generators::ScaffoldControllerGenerator
|
11
|
+
arguments %w(account name:string description:text business:references)
|
12
|
+
|
13
|
+
def test_generated_controller
|
14
|
+
return true if Rails::VERSION::MAJOR < 4
|
15
|
+
|
16
|
+
run_generator
|
17
|
+
|
18
|
+
assert_file 'app/controllers/accounts_controller.rb' do |content|
|
19
|
+
assert_instance_method :index, content do |m|
|
20
|
+
assert_match /@accounts = Account\.all/, m
|
21
|
+
assert_match /format.html/, m
|
22
|
+
assert_match /format.json \{ render json: @accounts \}/, m
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_instance_method :show, content do |m|
|
26
|
+
assert_match /format.html/, m
|
27
|
+
assert_match /format.json \{ render json: @account \}/, m
|
28
|
+
end
|
29
|
+
|
30
|
+
assert_instance_method :new, content do |m|
|
31
|
+
assert_match /@account = Account\.new/, m
|
32
|
+
end
|
33
|
+
|
34
|
+
assert_instance_method :edit, content do |m|
|
35
|
+
assert m.blank?
|
36
|
+
end
|
37
|
+
|
38
|
+
assert_instance_method :create, content do |m|
|
39
|
+
assert_match /@account = Account\.new\(account_params\)/, m
|
40
|
+
assert_match /@account\.save/, m
|
41
|
+
assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m
|
42
|
+
assert_match /format\.json \{ render json: @account, status: :created \}/, m
|
43
|
+
assert_match /format\.html \{ render action: 'new' \}/, m
|
44
|
+
assert_match /format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_instance_method :update, content do |m|
|
48
|
+
assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m
|
49
|
+
assert_match /format\.json \{ head :no_content \}/, m
|
50
|
+
assert_match /format\.html \{ render action: 'edit' \}/, m
|
51
|
+
assert_match /format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m
|
52
|
+
end
|
53
|
+
|
54
|
+
assert_instance_method :destroy, content do |m|
|
55
|
+
assert_match /@account\.destroy/, m
|
56
|
+
assert_match /format\.html { redirect_to accounts_url \}/, m
|
57
|
+
assert_match /format\.json \{ head :no_content \}/, m
|
58
|
+
end
|
59
|
+
|
60
|
+
assert_match(/def account_params/, content)
|
61
|
+
assert_match(/params\.require\(:account\)\.permit\(:name, :description, :business_id\)/, content)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails'
|
3
|
+
require 'active_model/serializer/railtie'
|
4
|
+
require 'test_app'
|
5
|
+
|
6
|
+
class SerializerGeneratorTest < Rails::Generators::TestCase
|
7
|
+
destination File.expand_path('../../../tmp', __FILE__)
|
8
|
+
setup :prepare_destination
|
9
|
+
|
10
|
+
tests Rails::Generators::SerializerGenerator
|
11
|
+
arguments %w(account name:string description:text business:references)
|
12
|
+
|
13
|
+
def test_generates_a_serializer_with_attributes_and_associations
|
14
|
+
run_generator
|
15
|
+
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ do |serializer|
|
16
|
+
assert_match(/attributes :id, :name, :description/, serializer)
|
17
|
+
assert_match(/has_one :business/, serializer)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_generates_a_namespaced_serializer
|
22
|
+
run_generator ['admin/account']
|
23
|
+
assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_uses_application_serializer_if_one_exists
|
27
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
28
|
+
run_generator
|
29
|
+
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
|
30
|
+
ensure
|
31
|
+
Object.send :remove_const, :ApplicationSerializer
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_uses_given_parent
|
35
|
+
Object.const_set(:ApplicationSerializer, Class.new)
|
36
|
+
run_generator ['Account', '--parent=MySerializer']
|
37
|
+
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/
|
38
|
+
ensure
|
39
|
+
Object.send :remove_const, :ApplicationSerializer
|
40
|
+
end
|
41
|
+
end
|
data/test/test_app.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class TestApp < Rails::Application
|
2
|
+
if Rails.version.to_s.first >= '4'
|
3
|
+
config.eager_load = false
|
4
|
+
config.secret_key_base = 'abc123'
|
5
|
+
end
|
6
|
+
|
7
|
+
# Set up a logger to avoid creating a log directory on every run.
|
8
|
+
config.logger = Logger.new(nil)
|
9
|
+
end
|
10
|
+
|
11
|
+
TestApp.initialize!
|
data/test/test_helper.rb
CHANGED
@@ -1,32 +1,24 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'active_model_serializers'
|
4
|
+
require 'fixtures/poro'
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
require "active_model_serializers"
|
7
|
-
require "active_support/json"
|
8
|
-
require "minitest/autorun"
|
9
|
-
|
10
|
-
require 'rails'
|
6
|
+
# Ensure backward compatibility with Minitest 4
|
7
|
+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
11
8
|
|
12
9
|
module TestHelper
|
13
10
|
Routes = ActionDispatch::Routing::RouteSet.new
|
14
11
|
Routes.draw do
|
15
|
-
resource :hypermedia
|
16
12
|
get ':controller(/:action(/:id))'
|
17
13
|
get ':controller(/:action)'
|
18
14
|
end
|
19
15
|
|
20
16
|
ActionController::Base.send :include, Routes.url_helpers
|
21
|
-
|
17
|
+
ActionController::Base.send :include, ActionController::Serialization
|
22
18
|
end
|
23
19
|
|
24
|
-
|
25
|
-
setup
|
26
|
-
@routes =
|
20
|
+
ActionController::TestCase.class_eval do
|
21
|
+
def setup
|
22
|
+
@routes = TestHelper::Routes
|
27
23
|
end
|
28
24
|
end
|
29
|
-
|
30
|
-
class Object
|
31
|
-
undef_method :id if respond_to?(:id)
|
32
|
-
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class ExceptTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_except_to_items_serializers
|
7
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
9
|
+
serializer = ArraySerializer.new(array, except: [:description])
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1' },
|
12
|
+
{ name: 'Name 2' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class KeyFormatTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_options_to_items_serializers
|
7
|
+
array = [WebLog.new({ name: 'Name 1', display_name: 'Display Name 1'}),
|
8
|
+
WebLog.new({ name: 'Name 2', display_name: 'Display Name 2'})]
|
9
|
+
serializer = ArraySerializer.new(array, key_format: :lower_camel)
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1', displayName: 'Display Name 1' },
|
12
|
+
{ name: 'Name 2', displayName: 'Display Name 2' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_model/serializer'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
class ArraySerializer
|
6
|
+
class MetaTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
10
|
+
@serializer = ArraySerializer.new([@profile1, @profile2], root: 'profiles')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_meta
|
14
|
+
@serializer.meta = { total: 10 }
|
15
|
+
|
16
|
+
assert_equal({
|
17
|
+
'profiles' => [
|
18
|
+
{
|
19
|
+
name: 'Name 1',
|
20
|
+
description: 'Description 1'
|
21
|
+
}, {
|
22
|
+
name: 'Name 2',
|
23
|
+
description: 'Description 2'
|
24
|
+
}
|
25
|
+
],
|
26
|
+
meta: {
|
27
|
+
total: 10
|
28
|
+
}
|
29
|
+
}, @serializer.as_json)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_meta_using_meta_key
|
33
|
+
@serializer.meta_key = :my_meta
|
34
|
+
@serializer.meta = { total: 10 }
|
35
|
+
|
36
|
+
assert_equal({
|
37
|
+
'profiles' => [
|
38
|
+
{
|
39
|
+
name: 'Name 1',
|
40
|
+
description: 'Description 1'
|
41
|
+
}, {
|
42
|
+
name: 'Name 2',
|
43
|
+
description: 'Description 2'
|
44
|
+
}
|
45
|
+
],
|
46
|
+
my_meta: {
|
47
|
+
total: 10
|
48
|
+
}
|
49
|
+
}, @serializer.as_json)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class OnlyTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_only_to_items_serializers
|
7
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
9
|
+
serializer = ArraySerializer.new(array, only: [:name])
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1' },
|
12
|
+
{ name: 'Name 2' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class RootAsOptionTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@old_root = ArraySerializer._root
|
8
|
+
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
10
|
+
@serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
ArraySerializer._root = @old_root
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_root_is_not_displayed_using_serializable_array
|
18
|
+
assert_equal([
|
19
|
+
{ name: 'Name 1', description: 'Description 1' },
|
20
|
+
{ name: 'Name 2', description: 'Description 2' }
|
21
|
+
], @serializer.serializable_array)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_root_using_as_json
|
25
|
+
assert_equal({
|
26
|
+
initialize: [
|
27
|
+
{ name: 'Name 1', description: 'Description 1' },
|
28
|
+
{ name: 'Name 2', description: 'Description 2' }
|
29
|
+
]
|
30
|
+
}, @serializer.as_json)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_root_as_argument_takes_precedence
|
34
|
+
assert_equal({
|
35
|
+
argument: [
|
36
|
+
{ name: 'Name 1', description: 'Description 1' },
|
37
|
+
{ name: 'Name 2', description: 'Description 2' }
|
38
|
+
]
|
39
|
+
}, @serializer.as_json(root: :argument))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_using_false_root_in_initialize_takes_precedence
|
43
|
+
ArraySerializer._root = 'root'
|
44
|
+
@serializer = ArraySerializer.new([@profile1, @profile2], root: false)
|
45
|
+
|
46
|
+
assert_equal([
|
47
|
+
{ name: 'Name 1', description: 'Description 1' },
|
48
|
+
{ name: 'Name 2', description: 'Description 2' }
|
49
|
+
], @serializer.as_json)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class RootInSerializerTest < Minitest::Test
|
54
|
+
def setup
|
55
|
+
@old_root = ArraySerializer._root
|
56
|
+
ArraySerializer._root = :in_serializer
|
57
|
+
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
58
|
+
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
59
|
+
@serializer = ArraySerializer.new([@profile1, @profile2])
|
60
|
+
@rooted_serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
61
|
+
end
|
62
|
+
|
63
|
+
def teardown
|
64
|
+
ArraySerializer._root = @old_root
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_root_is_not_displayed_using_serializable_hash
|
68
|
+
assert_equal([
|
69
|
+
{ name: 'Name 1', description: 'Description 1' },
|
70
|
+
{ name: 'Name 2', description: 'Description 2' }
|
71
|
+
], @serializer.serializable_array)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_root_using_as_json
|
75
|
+
assert_equal({
|
76
|
+
in_serializer: [
|
77
|
+
{ name: 'Name 1', description: 'Description 1' },
|
78
|
+
{ name: 'Name 2', description: 'Description 2' }
|
79
|
+
]
|
80
|
+
}, @serializer.as_json)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_root_in_initializer_takes_precedence
|
84
|
+
assert_equal({
|
85
|
+
initialize: [
|
86
|
+
{ name: 'Name 1', description: 'Description 1' },
|
87
|
+
{ name: 'Name 2', description: 'Description 2' }
|
88
|
+
]
|
89
|
+
}, @rooted_serializer.as_json)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_root_as_argument_takes_precedence
|
93
|
+
assert_equal({
|
94
|
+
argument: [
|
95
|
+
{ name: 'Name 1', description: 'Description 1' },
|
96
|
+
{ name: 'Name 2', description: 'Description 2' }
|
97
|
+
]
|
98
|
+
}, @rooted_serializer.as_json(root: :argument))
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class ScopeTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_options_to_items_serializers
|
7
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
9
|
+
serializer = ArraySerializer.new(array, scope: current_user)
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
12
|
+
{ name: 'Name 2', description: 'Description 2 - user' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def current_user
|
20
|
+
'user'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|