jsonapi-serializers 0.2.3 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ecd4a38af955857a58bcadcdf88ef7e2f658f82
4
- data.tar.gz: b47bc0a6ce3a5732d3f76bea5f87d425664afc9c
3
+ metadata.gz: 96dd9615a9ec47ce6f61ba580121e59c363f9ecb
4
+ data.tar.gz: c73a9d9a8562c78be05103036321ccd65beb6000
5
5
  SHA512:
6
- metadata.gz: 84839b9fa483ad6cff15f7c152045ba5e78c0aed12e354898b80d1532350b4d5eefa05d45ee686ac402adf8c7e613fcb7cbb96aadd80b8d0c6b50b498e0ce6e7
7
- data.tar.gz: afe84502165a840780bd94f54974a41a568f13e1c55efce92d21f7accb26c7aa07fbe30b2badff09eba60e4ca0b63637daa5864c4360fac3e8dfbdefe6b253e6
6
+ metadata.gz: fbe1da4cf4f43dc2a65aa4e7961a51f93a36e4df79c2c40cfd954d21dff648bce5f7ded25a963cc116cbba65d6e6c883d68524f0b79179642fb825024cfba7c7
7
+ data.tar.gz: e3c49d17d6cfbc2560369f2aec9f045d568daf78a63234d914979e5d56fb1deb2858bf8a52cf7994bb5a1978799f18f706f22ce38c0ec59fb0974e46ad8dc5c7
data/README.md CHANGED
@@ -149,12 +149,20 @@ Note that the JSON:API spec distinguishes in how null/empty is handled for singl
149
149
 
150
150
  ### Custom attributes
151
151
 
152
- By default the serializer looks for the same name of the attribute on the object it is given. You can customize this behavior by providing a block to the attribute:
152
+ By default the serializer looks for the same name of the attribute on the object it is given. You can customize this behavior by providing a block to `attribute`, `has_one`, or `has_many`:
153
153
 
154
154
  ```ruby
155
155
  attribute :content do
156
156
  object.body
157
157
  end
158
+
159
+ has_one :comment do
160
+ Comment.where(post: object).take!
161
+ end
162
+
163
+ has_many :authors do
164
+ Author.where(post: object)
165
+ end
158
166
  ```
159
167
 
160
168
  The block is evaluated within the serializer instance, so it has access to the `object` and `context` instance variables.
@@ -215,6 +223,8 @@ def relationship_related_link(attribute_name)
215
223
  end
216
224
  ```
217
225
 
226
+ If you override `self_link`, `relationship_self_link`, or `relationship_related_link` to return `nil`, the link will be excluded from the serialized object.
227
+
218
228
  ## Relationships
219
229
 
220
230
  You can easily specify relationships with the `has_one` and `has_many` directives.
@@ -409,7 +419,7 @@ class Api::V1::ReposController < Api::V1::BaseController
409
419
  end
410
420
  end
411
421
 
412
- # lib/jsonapi_mimetypes.rb
422
+ # config/initializers/jsonapi_mimetypes.rb
413
423
  # Without this mimetype registration, controllers will not automatically parse JSON API params.
414
424
  module JSONAPI
415
425
  MIMETYPE = "application/vnd.api+json"
@@ -86,12 +86,14 @@ module JSONAPI
86
86
  # Merge in data for has_one relationships.
87
87
  has_one_relationships.each do |attribute_name, object|
88
88
  formatted_attribute_name = format_name(attribute_name)
89
- data[formatted_attribute_name] = {
90
- 'links' => {
91
- 'self' => relationship_self_link(attribute_name),
92
- 'related' => relationship_related_link(attribute_name),
93
- },
94
- }
89
+
90
+ data[formatted_attribute_name] = {}
91
+ links_self = relationship_self_link(attribute_name)
92
+ links_related = relationship_related_link(attribute_name)
93
+ data[formatted_attribute_name]['links'] = {} if links_self || links_related
94
+ data[formatted_attribute_name]['links']['self'] = links_self if links_self
95
+ data[formatted_attribute_name]['links']['related'] = links_related if links_related
96
+
95
97
  if @_include_linkages.include?(formatted_attribute_name)
96
98
  if object.nil?
97
99
  # Spec: Resource linkage MUST be represented as one of the following:
@@ -113,12 +115,14 @@ module JSONAPI
113
115
  # Merge in data for has_many relationships.
114
116
  has_many_relationships.each do |attribute_name, objects|
115
117
  formatted_attribute_name = format_name(attribute_name)
116
- data[formatted_attribute_name] = {
117
- 'links' => {
118
- 'self' => relationship_self_link(attribute_name),
119
- 'related' => relationship_related_link(attribute_name),
120
- },
121
- }
118
+
119
+ data[formatted_attribute_name] = {}
120
+ links_self = relationship_self_link(attribute_name)
121
+ links_related = relationship_related_link(attribute_name)
122
+ data[formatted_attribute_name]['links'] = {} if links_self || links_related
123
+ data[formatted_attribute_name]['links']['self'] = links_self if links_self
124
+ data[formatted_attribute_name]['links']['related'] = links_related if links_related
125
+
122
126
  # Spec: Resource linkage MUST be represented as one of the following:
123
127
  # - an empty array ([]) for empty to-many relationships.
124
128
  # - an array of linkage objects for non-empty to-many relationships.
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Serializer
3
- VERSION = '0.2.3'
3
+ VERSION = '0.2.4'
4
4
  end
5
5
  end
@@ -107,6 +107,27 @@ describe JSONAPI::Serializer do
107
107
  },
108
108
  })
109
109
  end
110
+ it 'does not include links if relationship_self_link and relationship_related_link are nil' do
111
+ post = create(:post)
112
+ primary_data = serialize_primary(post, {serializer: MyApp::PostSerializerWithoutLinks})
113
+ expect(primary_data).to eq({
114
+ 'id' => '1',
115
+ 'type' => 'posts',
116
+ 'attributes' => {
117
+ 'title' => 'Title for Post 1',
118
+ 'long-content' => 'Body for Post 1',
119
+ },
120
+ 'links' => {
121
+ 'self' => '/posts/1',
122
+ },
123
+ # This is technically invalid since relationships MUST contain at least one of links,
124
+ # data, or meta, but we leave that up to the user.
125
+ 'relationships' => {
126
+ 'author' => {},
127
+ 'long-comments' => {},
128
+ },
129
+ })
130
+ end
110
131
  end
111
132
  context 'with linkage includes' do
112
133
  it 'can serialize primary data for a null to-one relationship' do
@@ -64,6 +64,7 @@ module MyApp
64
64
 
65
65
  class PostSerializerWithMetadata
66
66
  include JSONAPI::Serializer
67
+ include JSONAPI::Serializer
67
68
 
68
69
  attribute :title
69
70
  attribute :long_content do
@@ -83,8 +84,6 @@ module MyApp
83
84
  end
84
85
 
85
86
  class PostSerializerWithContextHandling < SimplestPostSerializer
86
- include JSONAPI::Serializer
87
-
88
87
  attribute :body, if: :show_body?, unless: :hide_body?
89
88
 
90
89
  def show_body?
@@ -96,6 +95,26 @@ module MyApp
96
95
  end
97
96
  end
98
97
 
98
+ class PostSerializerWithoutLinks
99
+ include JSONAPI::Serializer
100
+
101
+ attribute :title
102
+ attribute :long_content do
103
+ object.body
104
+ end
105
+
106
+ has_one :author
107
+ has_many :long_comments
108
+
109
+ def relationship_self_link(attribute_name)
110
+ nil
111
+ end
112
+
113
+ def relationship_related_link(attribute_name)
114
+ nil
115
+ end
116
+ end
117
+
99
118
  class EmptySerializer
100
119
  include JSONAPI::Serializer
101
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Fotinakis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-10 00:00:00.000000000 Z
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  requirements: []
125
125
  rubyforge_project:
126
- rubygems_version: 2.2.2
126
+ rubygems_version: 2.4.5
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: Pure Ruby readonly serializers for the JSON:API spec.
@@ -132,4 +132,3 @@ test_files:
132
132
  - spec/spec_helper.rb
133
133
  - spec/support/factory.rb
134
134
  - spec/support/serializers.rb
135
- has_rdoc: