active_model_serializers 0.8.3 → 0.9.0.alpha1

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -5
  3. data/CONTRIBUTING.md +20 -0
  4. data/DESIGN.textile +4 -4
  5. data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
  6. data/README.md +187 -96
  7. data/lib/action_controller/serialization.rb +30 -16
  8. data/lib/active_model/array_serializer.rb +36 -82
  9. data/lib/active_model/default_serializer.rb +22 -0
  10. data/lib/active_model/serializable.rb +25 -0
  11. data/lib/active_model/serializer/associations.rb +53 -211
  12. data/lib/active_model/serializer/config.rb +31 -0
  13. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  14. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  15. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  16. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  17. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  18. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  19. data/lib/active_model/serializer/railtie.rb +10 -0
  20. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  21. data/lib/active_model/serializer.rb +126 -448
  22. data/lib/active_model/serializer_support.rb +5 -0
  23. data/lib/active_model_serializers.rb +7 -86
  24. data/test/coverage_setup.rb +15 -0
  25. data/test/fixtures/active_record.rb +92 -0
  26. data/test/fixtures/poro.rb +64 -0
  27. data/test/integration/action_controller/serialization_test.rb +234 -0
  28. data/test/integration/active_record/active_record_test.rb +77 -0
  29. data/test/integration/generators/resource_generator_test.rb +26 -0
  30. data/test/integration/generators/scaffold_controller_generator_test.rb +67 -0
  31. data/test/integration/generators/serializer_generator_test.rb +41 -0
  32. data/test/test_app.rb +11 -0
  33. data/test/test_helper.rb +8 -19
  34. data/test/tmp/app/serializers/account_serializer.rb +3 -0
  35. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  36. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  37. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  38. data/test/unit/active_model/array_serializer/serialization_test.rb +83 -0
  39. data/test/unit/active_model/default_serializer_test.rb +13 -0
  40. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
  41. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  42. data/test/unit/active_model/serializer/attributes_test.rb +41 -0
  43. data/test/unit/active_model/serializer/config_test.rb +86 -0
  44. data/test/unit/active_model/serializer/filter_test.rb +49 -0
  45. data/test/unit/active_model/serializer/has_many_test.rb +173 -0
  46. data/test/unit/active_model/serializer/has_one_test.rb +151 -0
  47. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  48. data/test/unit/active_model/serializer/root_test.rb +117 -0
  49. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  50. metadata +80 -65
  51. data/.gitignore +0 -18
  52. data/.travis.yml +0 -28
  53. data/Gemfile +0 -4
  54. data/Gemfile.edge +0 -9
  55. data/Rakefile +0 -18
  56. data/active_model_serializers.gemspec +0 -24
  57. data/bench/perf.rb +0 -43
  58. data/cruft.md +0 -19
  59. data/lib/active_record/serializer_override.rb +0 -16
  60. data/lib/generators/resource_override.rb +0 -13
  61. data/lib/generators/serializer/serializer_generator.rb +0 -42
  62. data/lib/generators/serializer/templates/serializer.rb +0 -19
  63. data/test/array_serializer_test.rb +0 -75
  64. data/test/association_test.rb +0 -592
  65. data/test/caching_test.rb +0 -96
  66. data/test/generators_test.rb +0 -85
  67. data/test/no_serialization_scope_test.rb +0 -34
  68. data/test/serialization_scope_name_test.rb +0 -67
  69. data/test/serialization_test.rb +0 -392
  70. data/test/serializer_support_test.rb +0 -51
  71. data/test/serializer_test.rb +0 -1465
  72. data/test/test_fakes.rb +0 -217
@@ -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,21 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
- require "pry"
5
-
6
- require "active_model_serializers"
7
- require "active_support/json"
8
- require "minitest/autorun"
9
-
10
- require 'rails'
1
+ require 'bundler/setup'
2
+ require 'coverage_setup'
3
+ require 'minitest/autorun'
4
+ require 'active_model_serializers'
5
+ require 'fixtures/poro'
11
6
 
12
7
  module TestHelper
13
8
  Routes = ActionDispatch::Routing::RouteSet.new
14
9
  Routes.draw do
15
- resource :hypermedia
16
10
  get ':controller(/:action(/:id))'
17
11
  get ':controller(/:action)'
18
12
  end
19
13
 
20
14
  ActionController::Base.send :include, Routes.url_helpers
21
- ActiveModel::Serializer.send :include, Routes.url_helpers
22
15
  end
23
16
 
24
- ActiveSupport::TestCase.class_eval do
25
- setup do
26
- @routes = ::TestHelper::Routes
17
+ ActionController::TestCase.class_eval do
18
+ def setup
19
+ @routes = TestHelper::Routes
27
20
  end
28
21
  end
29
-
30
- class Object
31
- undef_method :id if respond_to?(:id)
32
- end
@@ -0,0 +1,3 @@
1
+ class AccountSerializer < MySerializer
2
+ attributes :id
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+ require 'active_model/serializer'
3
+
4
+ module ActiveModel
5
+ class ArraySerializer
6
+ class MetaTest < ActiveModel::TestCase
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,102 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class RootAsOptionTest < ActiveModel::TestCase
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 < ActiveModel::TestCase
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 < ActiveModel::TestCase
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
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class BasicObjectsSerializationTest < ActiveModel::TestCase
6
+ def setup
7
+ array = [1, 2, 3]
8
+ @serializer = ActiveModel::Serializer.serializer_for(array).new(array)
9
+ end
10
+
11
+ def test_serializer_for_array_returns_appropriate_type
12
+ assert_kind_of ArraySerializer, @serializer
13
+ end
14
+
15
+ def test_array_serializer_serializes_simple_objects
16
+ assert_equal [1, 2, 3], @serializer.serializable_array
17
+ assert_equal [1, 2, 3], @serializer.as_json
18
+ end
19
+ end
20
+
21
+ class ModelSerializationTest < ActiveModel::TestCase
22
+ def test_array_serializer_serializes_models
23
+ array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
24
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
25
+ serializer = ArraySerializer.new(array)
26
+
27
+ expected = [{ name: 'Name 1', description: 'Description 1' },
28
+ { name: 'Name 2', description: 'Description 2' }]
29
+
30
+ assert_equal expected, serializer.serializable_array
31
+ assert_equal expected, serializer.as_json
32
+ end
33
+
34
+ def test_array_serializers_each_serializer
35
+ array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
36
+ ::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
37
+ serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
38
+
39
+ expected = [{ name: 'Name 1', description: 'Description 1' },
40
+ { name: 'Name 2', description: 'Description 2' }]
41
+
42
+ assert_equal expected, serializer.serializable_array
43
+ assert_equal expected, serializer.as_json
44
+ end
45
+
46
+ def test_associated_objects_of_multiple_instances_embedded_in_root
47
+ @association = PostSerializer._associations[:comments]
48
+ @old_association = @association.dup
49
+
50
+ @association.embed = :ids
51
+ @association.embed_in_root = true
52
+
53
+ @post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
54
+ @post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
55
+
56
+ class << @post2
57
+ attr_writer :comments
58
+ end
59
+
60
+ @post2.comments = [
61
+ Comment.new(content: 'C3'),
62
+ Comment.new(content: 'C4')
63
+ ]
64
+
65
+ @serializer = ArraySerializer.new([@post1, @post2], root: :posts)
66
+ assert_equal({
67
+ posts: [
68
+ {title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
69
+ {title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
70
+ ],
71
+ comments: [
72
+ {content: "C1"},
73
+ {content: "C2"},
74
+ {content: "C3"},
75
+ {content: "C4"}
76
+ ]
77
+ }, @serializer.as_json)
78
+ ensure
79
+ PostSerializer._associations[:comments] = @old_association
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class DefaultSerializer
5
+ class Test < ActiveModel::TestCase
6
+ def test_serialize_objects
7
+ assert_equal(nil, DefaultSerializer.new(nil).serializable_object)
8
+ assert_equal(1, DefaultSerializer.new(1).serializable_object)
9
+ assert_equal('hi', DefaultSerializer.new('hi').serializable_object)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Association
6
+ class BuildSerializerTest < ActiveModel::TestCase
7
+ def setup
8
+ @association = Association::HasOne.new('post', serializer: PostSerializer)
9
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
10
+ end
11
+
12
+ def test_build_serializer_for_array_called_twice
13
+ 2.times do
14
+ serializer = @association.build_serializer(@post)
15
+ assert_instance_of(PostSerializer, serializer)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AssociationsTest < ActiveModel::TestCase
6
+ def test_associations_inheritance
7
+ inherited_serializer_klass = Class.new(PostSerializer) do
8
+ has_many :users
9
+ end
10
+ another_inherited_serializer_klass = Class.new(PostSerializer)
11
+
12
+ assert_equal([:comments, :users],
13
+ inherited_serializer_klass._associations.keys)
14
+ assert_equal([:comments],
15
+ another_inherited_serializer_klass._associations.keys)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AttributesTest < ActiveModel::TestCase
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ @profile_serializer = ProfileSerializer.new(@profile)
9
+ end
10
+
11
+ def test_attributes_definition
12
+ assert_equal([:name, :description],
13
+ @profile_serializer.class._attributes)
14
+ end
15
+
16
+ def test_attributes_serialization_using_serializable_hash
17
+ assert_equal({
18
+ name: 'Name 1', description: 'Description 1'
19
+ }, @profile_serializer.serializable_hash)
20
+ end
21
+
22
+ def test_attributes_serialization_using_as_json
23
+ assert_equal({
24
+ 'profile' => { name: 'Name 1', description: 'Description 1' }
25
+ }, @profile_serializer.as_json)
26
+ end
27
+
28
+ def test_attributes_inheritance
29
+ inherited_serializer_klass = Class.new(ProfileSerializer) do
30
+ attributes :comments
31
+ end
32
+ another_inherited_serializer_klass = Class.new(ProfileSerializer)
33
+
34
+ assert_equal([:name, :description, :comments],
35
+ inherited_serializer_klass._attributes)
36
+ assert_equal([:name, :description],
37
+ another_inherited_serializer_klass._attributes)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Config
6
+ class Test < ActiveModel::TestCase
7
+ def test_config_const_is_an_instance_of_config
8
+ assert_kind_of Config, CONFIG
9
+ end
10
+
11
+ def test_config_instance
12
+ config = Config.new
13
+ config.setting1 = 'value1'
14
+
15
+ assert_equal 'value1', config.setting1
16
+ end
17
+
18
+ def test_each_config
19
+ config = Config.new
20
+ config.setting1 = 'value1'
21
+ config.setting2 = 'value2'
22
+
23
+ actual = {}
24
+
25
+ config.each do |k, v|
26
+ actual[k] = v
27
+ end
28
+
29
+ assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual)
30
+ end
31
+ end
32
+
33
+ class ConfigTest < ActiveModel::TestCase
34
+ def test_setup
35
+ ActiveModel::Serializer.setup do |config|
36
+ config.a = 'v1'
37
+ config.b = 'v2'
38
+ end
39
+
40
+ assert_equal 'v1', CONFIG.a
41
+ assert_equal 'v2', CONFIG.b
42
+ ensure
43
+ CONFIG.clear
44
+ end
45
+
46
+ def test_config_accessors
47
+ ActiveModel::Serializer.setup do |config|
48
+ config.foo = 'v1'
49
+ config.bar = 'v2'
50
+ end
51
+
52
+ assert_equal 'v1', CONFIG.foo
53
+ assert_equal 'v2', CONFIG.bar
54
+ ensure
55
+ CONFIG.clear
56
+ end
57
+
58
+ def test_acessor_when_nil
59
+ assert_nil CONFIG.foo
60
+ CONFIG.foo = 1
61
+ assert_equal 1, CONFIG.foo
62
+ assert_nil CONFIG.bar
63
+ end
64
+ end
65
+
66
+ class ApplyConfigTest < ActiveModel::TestCase
67
+ def test_apply_config_to_associations
68
+ CONFIG.embed = :ids
69
+ CONFIG.embed_in_root = true
70
+
71
+ association = PostSerializer._associations[:comments]
72
+ old_association = association.dup
73
+
74
+ association.send :initialize, association.name, association.options
75
+
76
+ assert association.embed_ids?
77
+ assert !association.embed_objects?
78
+ assert association.embed_in_root
79
+ ensure
80
+ PostSerializer._associations[:comments] = old_association
81
+ CONFIG.clear
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class FilterAttributesTest < ActiveModel::TestCase
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ @profile_serializer = ProfileSerializer.new(@profile)
9
+ @profile_serializer.instance_eval do
10
+ def filter(keys)
11
+ keys - [:description]
12
+ end
13
+ end
14
+ end
15
+
16
+ def test_filtered_attributes_serialization
17
+ assert_equal({
18
+ 'profile' => { name: 'Name 1' }
19
+ }, @profile_serializer.as_json)
20
+ end
21
+ end
22
+
23
+ class FilterAssociationsTest < ActiveModel::TestCase
24
+ def setup
25
+ @association = PostSerializer._associations[:comments]
26
+ @old_association = @association.dup
27
+ @association.embed = :ids
28
+ @association.embed_in_root = true
29
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
30
+ @post_serializer = PostSerializer.new(@post)
31
+ @post_serializer.instance_eval do
32
+ def filter(keys)
33
+ keys - [:body, :comments]
34
+ end
35
+ end
36
+ end
37
+
38
+ def teardown
39
+ PostSerializer._associations[:comments] = @old_association
40
+ end
41
+
42
+ def test_filtered_associations_serialization
43
+ assert_equal({
44
+ 'post' => { title: 'Title 1' }
45
+ }, @post_serializer.as_json)
46
+ end
47
+ end
48
+ end
49
+ end