active_model_serializers 0.10.0.rc1 → 0.10.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -2
  3. data/Gemfile +1 -1
  4. data/README.md +6 -3
  5. data/lib/action_controller/serialization.rb +9 -1
  6. data/lib/active_model/serializer.rb +15 -21
  7. data/lib/active_model/serializer/adapter.rb +13 -4
  8. data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
  9. data/lib/active_model/serializer/adapter/fragment_cache.rb +5 -5
  10. data/lib/active_model/serializer/adapter/json.rb +8 -10
  11. data/lib/active_model/serializer/adapter/json_api.rb +34 -30
  12. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +3 -2
  13. data/lib/active_model/serializer/array_serializer.rb +8 -5
  14. data/lib/active_model/serializer/configuration.rb +1 -1
  15. data/lib/active_model/serializer/fieldset.rb +2 -2
  16. data/lib/active_model/serializer/railtie.rb +8 -0
  17. data/lib/active_model/serializer/version.rb +1 -1
  18. data/lib/active_model_serializers.rb +1 -0
  19. data/lib/generators/serializer/resource_override.rb +12 -0
  20. data/test/action_controller/adapter_selector_test.rb +7 -5
  21. data/test/action_controller/explicit_serializer_test.rb +33 -9
  22. data/test/action_controller/json_api_linked_test.rb +25 -19
  23. data/test/action_controller/rescue_from_test.rb +32 -0
  24. data/test/action_controller/serialization_scope_name_test.rb +2 -2
  25. data/test/action_controller/serialization_test.rb +41 -23
  26. data/test/adapter/fragment_cache_test.rb +1 -1
  27. data/test/adapter/json/belongs_to_test.rb +9 -2
  28. data/test/adapter/json/collection_test.rb +16 -2
  29. data/test/adapter/json/has_many_test.rb +1 -1
  30. data/test/adapter/json_api/belongs_to_test.rb +45 -35
  31. data/test/adapter/json_api/collection_test.rb +30 -23
  32. data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
  33. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +14 -14
  34. data/test/adapter/json_api/has_many_test.rb +22 -18
  35. data/test/adapter/json_api/has_one_test.rb +8 -6
  36. data/test/adapter/json_api/linked_test.rb +97 -71
  37. data/test/adapter/json_test.rb +1 -1
  38. data/test/adapter_test.rb +1 -1
  39. data/test/array_serializer_test.rb +14 -0
  40. data/test/fixtures/poro.rb +31 -7
  41. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  42. data/test/{serializers/generators_test.rb → generators/serializer_generator_test.rb} +2 -10
  43. data/test/serializers/adapter_for_test.rb +1 -1
  44. data/test/serializers/associations_test.rb +21 -0
  45. data/test/serializers/attribute_test.rb +16 -1
  46. data/test/serializers/attributes_test.rb +35 -0
  47. data/test/serializers/cache_test.rb +14 -4
  48. data/test/serializers/configuration_test.rb +1 -1
  49. data/test/serializers/meta_test.rb +38 -9
  50. data/test/serializers/serializer_for_test.rb +9 -0
  51. data/test/test_helper.rb +10 -7
  52. metadata +12 -5
@@ -8,7 +8,7 @@ module ActiveModel
8
8
  @role = Role.new(name: 'Great Author', description:nil)
9
9
  @role.author = [@author]
10
10
  @role_serializer = RoleSerializer.new(@role)
11
- @role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {}, nil)
11
+ @role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
12
12
  end
13
13
 
14
14
  def test_fragment_fetch_with_virtual_attributes
@@ -25,14 +25,21 @@ module ActiveModel
25
25
  end
26
26
 
27
27
  def test_includes_post
28
- assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:post])
28
+ assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post])
29
29
  end
30
30
 
31
31
  def test_include_nil_author
32
32
  serializer = PostSerializer.new(@anonymous_post)
33
33
  adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
34
34
 
35
- assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
35
+ assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}}, adapter.serializable_hash)
36
+ end
37
+
38
+ def test_include_nil_author_with_specified_serializer
39
+ serializer = PostPreviewSerializer.new(@anonymous_post)
40
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
41
+
42
+ assert_equal({posts: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
36
43
  end
37
44
  end
38
45
  end
@@ -22,8 +22,22 @@ module ActiveModel
22
22
  ActionController::Base.cache_store.clear
23
23
  end
24
24
 
25
+ def test_with_serializer_option
26
+ @blog.special_attribute = "Special"
27
+ @blog.articles = [@first_post, @second_post]
28
+ @serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
29
+ @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
30
+
31
+ expected = {custom_blogs:[{
32
+ id: 1,
33
+ special_attribute: "Special",
34
+ articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
35
+ }]}
36
+ assert_equal expected, @adapter.serializable_hash
37
+ end
38
+
25
39
  def test_include_multiple_posts
26
- expected = [{
40
+ expected = { posts: [{
27
41
  title: "Hello!!",
28
42
  body: "Hello, world!!",
29
43
  id: 1,
@@ -49,7 +63,7 @@ module ActiveModel
49
63
  id: 999,
50
64
  name: "Custom blog"
51
65
  }
52
- }]
66
+ }]}
53
67
  assert_equal expected, @adapter.serializable_hash
54
68
  end
55
69
  end
@@ -26,7 +26,7 @@ module ActiveModel
26
26
  assert_equal([
27
27
  {id: 1, body: 'ZOMG A COMMENT'},
28
28
  {id: 2, body: 'ZOMG ANOTHER COMMENT'}
29
- ], @adapter.serializable_hash[:comments])
29
+ ], @adapter.serializable_hash[:post][:comments])
30
30
  end
31
31
  end
32
32
  end
@@ -32,9 +32,9 @@ module ActiveModel
32
32
  end
33
33
 
34
34
  def test_includes_post_id
35
- expected = { linkage: { type: "posts", id: "42" } }
35
+ expected = { data: { type: "posts", id: "42" } }
36
36
 
37
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:post])
37
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
38
38
  end
39
39
 
40
40
  def test_includes_linked_post
@@ -42,12 +42,14 @@ module ActiveModel
42
42
  expected = [{
43
43
  id: "42",
44
44
  type: "posts",
45
- title: 'New Post',
46
- body: 'Body',
47
- links: {
48
- comments: { linkage: [ { type: "comments", id: "1" } ] },
49
- blog: { linkage: { type: "blogs", id: "999" } },
50
- author: { linkage: { type: "authors", id: "1" } }
45
+ attributes: {
46
+ title: 'New Post',
47
+ body: 'Body',
48
+ },
49
+ relationships: {
50
+ comments: { data: [ { type: "comments", id: "1" } ] },
51
+ blog: { data: { type: "blogs", id: "999" } },
52
+ author: { data: { type: "authors", id: "1" } }
51
53
  }
52
54
  }]
53
55
  assert_equal expected, @adapter.serializable_hash[:included]
@@ -58,11 +60,13 @@ module ActiveModel
58
60
  expected = [{
59
61
  id: "42",
60
62
  type: "posts",
61
- title: 'New Post',
62
- links: {
63
- comments: { linkage: [ { type: "comments", id: "1" } ] },
64
- blog: { linkage: { type: "blogs", id: "999" } },
65
- author: { linkage: { type: "authors", id: "1" } }
63
+ attributes: {
64
+ title: 'New Post'
65
+ },
66
+ relationships: {
67
+ comments: { data: [ { type: "comments", id: "1" } ] },
68
+ blog: { data: { type: "blogs", id: "999" } },
69
+ author: { data: { type: "authors", id: "1" } }
66
70
  }
67
71
  }]
68
72
  assert_equal expected, @adapter.serializable_hash[:included]
@@ -72,22 +76,22 @@ module ActiveModel
72
76
  serializer = PostSerializer.new(@anonymous_post)
73
77
  adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
74
78
 
75
- assert_equal({comments: { linkage: [] }, blog: { linkage: { type: "blogs", id: "999" } }, author: { linkage: nil }}, adapter.serializable_hash[:data][:links])
79
+ assert_equal({comments: { data: [] }, blog: { data: { type: "blogs", id: "999" } }, author: { data: nil }}, adapter.serializable_hash[:data][:relationships])
76
80
  end
77
81
 
78
82
  def test_include_type_for_association_when_different_than_name
79
83
  serializer = BlogSerializer.new(@blog)
80
84
  adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
81
- links = adapter.serializable_hash[:data][:links]
85
+ relationships = adapter.serializable_hash[:data][:relationships]
82
86
  expected = {
83
87
  writer: {
84
- linkage: {
88
+ data: {
85
89
  type: "authors",
86
90
  id: "1"
87
91
  }
88
92
  },
89
93
  articles: {
90
- linkage: [
94
+ data: [
91
95
  {
92
96
  type: "posts",
93
97
  id: "42"
@@ -99,7 +103,7 @@ module ActiveModel
99
103
  ]
100
104
  }
101
105
  }
102
- assert_equal expected, links
106
+ assert_equal expected, relationships
103
107
  end
104
108
 
105
109
  def test_include_linked_resources_with_type_name
@@ -110,31 +114,37 @@ module ActiveModel
110
114
  {
111
115
  id: "1",
112
116
  type: "authors",
113
- name: "Steve K.",
114
- links: {
115
- posts: { linkage: [] },
116
- roles: { linkage: [] },
117
- bio: { linkage: nil }
117
+ attributes: {
118
+ name: "Steve K."
119
+ },
120
+ relationships: {
121
+ posts: { data: [] },
122
+ roles: { data: [] },
123
+ bio: { data: nil }
118
124
  }
119
125
  },{
120
126
  id: "42",
121
127
  type: "posts",
122
- title: "New Post",
123
- body: "Body",
124
- links: {
125
- comments: { linkage: [ { type: "comments", id: "1" } ] },
126
- blog: { linkage: { type: "blogs", id: "999" } },
127
- author: { linkage: { type: "authors", id: "1" } }
128
+ attributes: {
129
+ title: "New Post",
130
+ body: "Body"
131
+ },
132
+ relationships: {
133
+ comments: { data: [ { type: "comments", id: "1" } ] },
134
+ blog: { data: { type: "blogs", id: "999" } },
135
+ author: { data: { type: "authors", id: "1" } }
128
136
  }
129
137
  }, {
130
138
  id: "43",
131
139
  type: "posts",
132
- title: "Hello!!",
133
- body: "Hello, world!!",
134
- links: {
135
- comments: { linkage: [] },
136
- blog: { linkage: { type: "blogs", id: "999" } },
137
- author: { linkage: nil }
140
+ attributes: {
141
+ title: "Hello!!",
142
+ body: "Hello, world!!"
143
+ },
144
+ relationships: {
145
+ comments: { data: [] },
146
+ blog: { data: { type: "blogs", id: "999" } },
147
+ author: { data: nil }
138
148
  }
139
149
  }
140
150
  ]
@@ -29,23 +29,27 @@ module ActiveModel
29
29
  {
30
30
  id: "1",
31
31
  type: "posts",
32
- title: "Hello!!",
33
- body: "Hello, world!!",
34
- links: {
35
- comments: { linkage: [] },
36
- blog: { linkage: { type: "blogs", id: "999" } },
37
- author: { linkage: { type: "authors", id: "1" } }
32
+ attributes: {
33
+ title: "Hello!!",
34
+ body: "Hello, world!!"
35
+ },
36
+ relationships: {
37
+ comments: { data: [] },
38
+ blog: { data: { type: "blogs", id: "999" } },
39
+ author: { data: { type: "authors", id: "1" } }
38
40
  }
39
41
  },
40
42
  {
41
43
  id: "2",
42
44
  type: "posts",
43
- title: "New Post",
44
- body: "Body",
45
- links: {
46
- comments: { linkage: [] },
47
- blog: { linkage: { type: "blogs", id: "999" } },
48
- author: { linkage: { type: "authors", id: "1" } }
45
+ attributes: {
46
+ title: "New Post",
47
+ body: "Body"
48
+ },
49
+ relationships: {
50
+ comments: { data: [] },
51
+ blog: { data: { type: "blogs", id: "999" } },
52
+ author: { data: { type: "authors", id: "1" } }
49
53
  }
50
54
  }
51
55
  ]
@@ -60,25 +64,28 @@ module ActiveModel
60
64
  {
61
65
  id: "1",
62
66
  type: "posts",
63
- title: "Hello!!",
64
- links: {
65
- comments: { linkage: [] },
66
- blog: { linkage: { type: "blogs", id: "999" } },
67
- author: { linkage: { type: "authors", id: "1" } }
67
+ attributes: {
68
+ title: "Hello!!"
69
+ },
70
+ relationships: {
71
+ comments: { data: [] },
72
+ blog: { data: { type: "blogs", id: "999" } },
73
+ author: { data: { type: "authors", id: "1" } }
68
74
  }
69
75
  },
70
76
  {
71
77
  id: "2",
72
78
  type: "posts",
73
- title: "New Post",
74
- links: {
75
- comments: { linkage: [] },
76
- blog: { linkage: { type: "blogs", id: "999" } },
77
- author: { linkage: { type: "authors", id: "1" } }
79
+ attributes: {
80
+ title: "New Post"
81
+ },
82
+ relationships: {
83
+ comments: { data: [] },
84
+ blog: { data: { type: "blogs", id: "999" } },
85
+ author: { data: { type: "authors", id: "1" } }
78
86
  }
79
87
  }
80
88
  ]
81
-
82
89
  assert_equal(expected, @adapter.serializable_hash[:data])
83
90
  end
84
91
 
@@ -26,13 +26,13 @@ module ActiveModel
26
26
 
27
27
  def test_includes_comment_ids
28
28
  expected = {
29
- linkage: [
29
+ data: [
30
30
  { type: "posts", id: "1"},
31
31
  { type: "posts", id: "2"}
32
32
  ]
33
33
  }
34
34
 
35
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:posts])
35
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
36
36
  end
37
37
 
38
38
  def test_no_includes_linked_comments
@@ -30,13 +30,13 @@ module ActiveModel
30
30
 
31
31
  def test_includes_comment_ids
32
32
  expected = {
33
- linkage: [
33
+ data: [
34
34
  { type: 'comments', id: '1' },
35
35
  { type: 'comments', id: '2' }
36
36
  ]
37
37
  }
38
38
 
39
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
39
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
40
40
  end
41
41
 
42
42
  def test_includes_linked_data
@@ -45,22 +45,22 @@ module ActiveModel
45
45
  {
46
46
  id: '1',
47
47
  type: 'comments',
48
- links: {
49
- post: { linkage: { type: 'posts', id: @post.id.to_s } }
48
+ relationships: {
49
+ post: { data: { type: 'posts', id: @post.id.to_s } }
50
50
  }
51
51
  },
52
52
  {
53
53
  id: '2',
54
54
  type: 'comments',
55
- links: {
56
- post: { linkage: { type: 'posts', id: @post.id.to_s } }
55
+ relationships: {
56
+ post: { data: { type: 'posts', id: @post.id.to_s } }
57
57
  }
58
58
  },
59
59
  {
60
60
  id: @author.id.to_s,
61
61
  type: "authors",
62
- links: {
63
- posts: { linkage: [ {type: "posts", id: @post.id.to_s } ] }
62
+ relationships: {
63
+ posts: { data: [ {type: "posts", id: @post.id.to_s } ] }
64
64
  }
65
65
  }
66
66
  ]
@@ -70,26 +70,26 @@ module ActiveModel
70
70
 
71
71
  def test_includes_author_id
72
72
  expected = {
73
- linkage: { type: "authors", id: @author.id.to_s }
73
+ data: { type: "authors", id: @author.id.to_s }
74
74
  }
75
75
 
76
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
76
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
77
77
  end
78
78
 
79
79
  def test_explicit_serializer_with_null_resource
80
80
  @post.author = nil
81
81
 
82
- expected = { linkage: nil }
82
+ expected = { data: nil }
83
83
 
84
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:author])
84
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
85
85
  end
86
86
 
87
87
  def test_explicit_serializer_with_null_collection
88
88
  @post.comments = []
89
89
 
90
- expected = { linkage: [] }
90
+ expected = { data: [] }
91
91
 
92
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
92
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
93
93
  end
94
94
  end
95
95
  end
@@ -33,9 +33,9 @@ module ActiveModel
33
33
  end
34
34
 
35
35
  def test_includes_comment_ids
36
- expected = { linkage: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
36
+ expected = { data: [ { type: "comments", id: "1" }, { type: "comments", id: "2" } ] }
37
37
 
38
- assert_equal(expected, @adapter.serializable_hash[:data][:links][:comments])
38
+ assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
39
39
  end
40
40
 
41
41
  def test_includes_linked_comments
@@ -43,18 +43,22 @@ module ActiveModel
43
43
  expected = [{
44
44
  id: "1",
45
45
  type: "comments",
46
- body: 'ZOMG A COMMENT',
47
- links: {
48
- post: { linkage: { type: "posts", id: "1" } },
49
- author: { linkage: nil }
46
+ attributes: {
47
+ body: 'ZOMG A COMMENT'
48
+ },
49
+ relationships: {
50
+ post: { data: { type: "posts", id: "1" } },
51
+ author: { data: nil }
50
52
  }
51
53
  }, {
52
54
  id: "2",
53
55
  type: "comments",
54
- body: 'ZOMG ANOTHER COMMENT',
55
- links: {
56
- post: { linkage: { type: "posts", id: "1" } },
57
- author: { linkage: nil }
56
+ attributes: {
57
+ body: 'ZOMG ANOTHER COMMENT'
58
+ },
59
+ relationships: {
60
+ post: { data: { type: "posts", id: "1" } },
61
+ author: { data: nil }
58
62
  }
59
63
  }]
60
64
  assert_equal expected, @adapter.serializable_hash[:included]
@@ -65,16 +69,16 @@ module ActiveModel
65
69
  expected = [{
66
70
  id: "1",
67
71
  type: "comments",
68
- links: {
69
- post: { linkage: { type: "posts", id: "1" } },
70
- author: { linkage: nil }
72
+ relationships: {
73
+ post: { data: { type: "posts", id: "1" } },
74
+ author: { data: nil }
71
75
  }
72
76
  }, {
73
77
  id: "2",
74
78
  type: "comments",
75
- links: {
76
- post: { linkage: { type: "posts", id: "1" } },
77
- author: { linkage: nil }
79
+ relationships: {
80
+ post: { data: { type: "posts", id: "1" } },
81
+ author: { data: nil }
78
82
  }
79
83
  }]
80
84
  assert_equal expected, @adapter.serializable_hash[:included]
@@ -90,9 +94,9 @@ module ActiveModel
90
94
  def test_include_type_for_association_when_different_than_name
91
95
  serializer = BlogSerializer.new(@blog)
92
96
  adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
93
- actual = adapter.serializable_hash[:data][:links][:articles]
97
+ actual = adapter.serializable_hash[:data][:relationships][:articles]
94
98
  expected = {
95
- linkage: [{
99
+ data: [{
96
100
  type: "posts",
97
101
  id: "1"
98
102
  }]