jsonapi-serializers 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41b4f33131c66343d6703be670fe9a9e58f32c91
4
- data.tar.gz: 5660da86b24a037e99e5cb21b7d60f484a8e6884
3
+ metadata.gz: cd3e1e9535b28c3bd4aa6649bce141d36c312edc
4
+ data.tar.gz: 44a9e5b6079c4b8fc61a871f28e63914df7b6d94
5
5
  SHA512:
6
- metadata.gz: dc1f3bd07a774a7e2703a00f700280b3013ada8eda1977b12fd9525c8f5fee9f162a831d0c0d6c91bd0c01db383e52904bd8916179d523aa9a5fd5e9f16173a8
7
- data.tar.gz: 940b4a24eedd64d9f5e568592972bd3c2f6c4e3e04ddefeedbaefd977f02016e707f5fb672629720def6db6b82cb2ea3e64fac95178188a65e0cc492a30697c6
6
+ metadata.gz: a70aa04850d27c037bed576a368f74d76b4e8eda1ef40b65b933e04b1abcf2151245026461114dd19fe61b3f0e13265a9ba513e7606acbe4d10927d20bc48d87
7
+ data.tar.gz: d41530712d4454f0e73dead332a0e657853fe74d5f3a1f1ee2c400b7fb7c150d57e074af0c628b30106ced93ef2ed41d5c44a98d90fe195964ec37be2a369ecf
@@ -28,6 +28,7 @@ module JSONAPI
28
28
 
29
29
  def initialize(object, options = {})
30
30
  @object = object
31
+ @options = options
31
32
  @context = options[:context] || {}
32
33
  @base_url = options[:base_url]
33
34
 
@@ -110,7 +111,7 @@ module JSONAPI
110
111
  # http://jsonapi.org/format/#document-structure-resource-relationships
111
112
  data[formatted_attribute_name]['data'] = nil
112
113
  else
113
- related_object_serializer = JSONAPI::Serializer.find_serializer(object)
114
+ related_object_serializer = JSONAPI::Serializer.find_serializer(object, @options)
114
115
  data[formatted_attribute_name]['data'] = {
115
116
  'type' => related_object_serializer.type.to_s,
116
117
  'id' => related_object_serializer.id.to_s,
@@ -138,7 +139,7 @@ module JSONAPI
138
139
  data[formatted_attribute_name]['data'] = []
139
140
  objects = has_many_relationship(attribute_name, attr_data) || []
140
141
  objects.each do |obj|
141
- related_object_serializer = JSONAPI::Serializer.find_serializer(obj)
142
+ related_object_serializer = JSONAPI::Serializer.find_serializer(obj, @options)
142
143
  data[formatted_attribute_name]['data'] << {
143
144
  'type' => related_object_serializer.type.to_s,
144
145
  'id' => related_object_serializer.id.to_s,
@@ -221,8 +222,8 @@ module JSONAPI
221
222
  class_name.constantize
222
223
  end
223
224
 
224
- def self.find_serializer(object)
225
- find_serializer_class(object).new(object)
225
+ def self.find_serializer(object, options)
226
+ find_serializer_class(object).new(object, options)
226
227
  end
227
228
 
228
229
  def self.serialize(objects, options = {})
@@ -298,12 +299,13 @@ module JSONAPI
298
299
  objects.compact.each do |obj|
299
300
  # Use the mutability of relationship_data as the return datastructure to take advantage
300
301
  # of the internal special merging logic.
301
- find_recursive_relationships(obj, inclusion_tree, relationship_data)
302
+ find_recursive_relationships(obj, inclusion_tree, relationship_data, passthrough_options)
302
303
  end
303
304
 
304
305
  result['included'] = relationship_data.map do |_, data|
305
306
  included_passthrough_options = {}
306
307
  included_passthrough_options[:base_url] = passthrough_options[:base_url]
308
+ included_passthrough_options[:context] = passthrough_options[:context]
307
309
  included_passthrough_options[:serializer] = find_serializer_class(data[:object])
308
310
  included_passthrough_options[:include_linkages] = data[:include_linkages]
309
311
  serialize_primary(data[:object], included_passthrough_options)
@@ -358,12 +360,12 @@ module JSONAPI
358
360
  # ['users', '1'] => {object: <User>, include_linkages: []},
359
361
  # ['users', '2'] => {object: <User>, include_linkages: []},
360
362
  # }
361
- def self.find_recursive_relationships(root_object, root_inclusion_tree, results)
363
+ def self.find_recursive_relationships(root_object, root_inclusion_tree, results, options)
362
364
  root_inclusion_tree.each do |attribute_name, child_inclusion_tree|
363
365
  # Skip the sentinal value, but we need to preserve it for siblings.
364
366
  next if attribute_name == :_include
365
367
 
366
- serializer = JSONAPI::Serializer.find_serializer(root_object)
368
+ serializer = JSONAPI::Serializer.find_serializer(root_object, options)
367
369
  unformatted_attr_name = serializer.unformat_name(attribute_name).to_sym
368
370
 
369
371
  # We know the name of this relationship, but we don't know where it is stored internally.
@@ -409,7 +411,7 @@ module JSONAPI
409
411
  # If it is not set, that indicates that this is an inner path and not a leaf and will
410
412
  # be followed by the recursion below.
411
413
  objects.each do |obj|
412
- obj_serializer = JSONAPI::Serializer.find_serializer(obj)
414
+ obj_serializer = JSONAPI::Serializer.find_serializer(obj, options)
413
415
  # Use keys of ['posts', '1'] for the results to enforce uniqueness.
414
416
  # Spec: A compound document MUST NOT include more than one resource object for each
415
417
  # type and id pair.
@@ -443,7 +445,7 @@ module JSONAPI
443
445
  if !child_inclusion_tree.empty?
444
446
  # For each object we just loaded, find all deeper recursive relationships.
445
447
  objects.each do |obj|
446
- find_recursive_relationships(obj, child_inclusion_tree, results)
448
+ find_recursive_relationships(obj, child_inclusion_tree, results, options)
447
449
  end
448
450
  end
449
451
  end
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Serializer
3
- VERSION = '0.7.0'
3
+ VERSION = '0.8.0'
4
4
  end
5
5
  end
@@ -753,7 +753,7 @@ describe JSONAPI::Serializer do
753
753
  describe 'if/unless handling with contexts' do
754
754
  it 'can be used to show/hide attributes' do
755
755
  post = create(:post)
756
- options = {serializer: MyApp::PostSerializerWithContextHandling}
756
+ options = {serializer: MyApp::PostSerializerWithContext}
757
757
 
758
758
  options[:context] = {show_body: false}
759
759
  data = JSONAPI::Serializer.serialize(post, options)
@@ -794,7 +794,36 @@ describe JSONAPI::Serializer do
794
794
  end
795
795
  end
796
796
  describe 'context' do
797
- xit 'is correctly passed through all serializers' do
797
+ it 'is passed through all relationship serializers' do
798
+ # Force long_comments to be serialized by the context-sensitive serializer.
799
+ expect_any_instance_of(MyApp::LongComment).to receive(:jsonapi_serializer_class_name)
800
+ .at_least(:once)
801
+ .and_return('MyApp::LongCommentsSerializerWithContext')
802
+
803
+ user = create(:user, name: 'Long Comment Author -- Should Not Be Serialized')
804
+ long_comment = create(:long_comment, user: user)
805
+ post = create(:post, :with_author, long_comments: [long_comment])
806
+
807
+ context = {show_body: false, show_comments_user: false}
808
+ expected_data = {
809
+ 'data' => serialize_primary(post, {
810
+ serializer: MyApp::PostSerializerWithContext,
811
+ include_linkages: ['long-comments'],
812
+ context: context,
813
+ }),
814
+ 'included' => [
815
+ serialize_primary(long_comment, {
816
+ serializer: MyApp::LongCommentsSerializerWithContext,
817
+ context: context,
818
+ }),
819
+ ],
820
+ }
821
+ includes = ['long-comments']
822
+ actual_data = JSONAPI::Serializer.serialize(post, context: context, include: includes)
823
+ # Multiple expectations for better diff output for debugging.
824
+ expect(actual_data['data']).to eq(expected_data['data'])
825
+ expect(actual_data['included']).to eq(expected_data['included'])
826
+ expect(actual_data).to eq(expected_data)
798
827
  end
799
828
  end
800
829
 
@@ -21,6 +21,11 @@ module MyApp
21
21
  attr_accessor :body
22
22
  attr_accessor :user
23
23
  attr_accessor :post
24
+
25
+ # Just a copy of the default implementation, we need this to exist to be able to stub in tests.
26
+ def jsonapi_serializer_class_name
27
+ 'MyApp::LongCommentSerializer'
28
+ end
24
29
  end
25
30
 
26
31
  class User
@@ -108,7 +113,7 @@ module MyApp
108
113
  end
109
114
  end
110
115
 
111
- class PostSerializerWithContextHandling < SimplestPostSerializer
116
+ class PostSerializerWithContext < PostSerializer
112
117
  attribute :body, if: :show_body?, unless: :hide_body?
113
118
 
114
119
  def show_body?
@@ -120,6 +125,22 @@ module MyApp
120
125
  end
121
126
  end
122
127
 
128
+ class LongCommentsSerializerWithContext
129
+ include JSONAPI::Serializer
130
+
131
+ attribute :body, if: :show_body?
132
+ has_one :user, if: :show_comments_user?
133
+
134
+ def show_body?
135
+ context.fetch(:show_body, true)
136
+ end
137
+
138
+ def show_comments_user?
139
+ context.fetch(:show_comments_user, true)
140
+ end
141
+ end
142
+
143
+
123
144
  class PostSerializerWithoutLinks
124
145
  include JSONAPI::Serializer
125
146
 
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.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Fotinakis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-30 00:00:00.000000000 Z
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport