jsonapi-serializers 0.4.0 → 0.5.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: 492ef52b3e744f06e226ebe911fd6671dfc637b6
4
- data.tar.gz: 19f25d49bb9bb8450b9538bdc31aaf6ee4bf3a12
3
+ metadata.gz: 42699af4d36a1e0a2293c857393c2dc3536d8d82
4
+ data.tar.gz: 92ef3e0b867d350c32fe4145c01958e38ceb2a8b
5
5
  SHA512:
6
- metadata.gz: 1ad6cd857ffccc11bccf9ee0d33aa0c35dddb8f6da4b188984ac56609c3e38d9373c66fb12518045dec75ebf26473a7624002dc8d1ecb16ea9eb784f0ace8dac
7
- data.tar.gz: b6212c674cd0d71bf345aeca14c6e8215e13b9b06614a03510cee65311ef09164b8b7f7effa460a729083963c8d60ae141acf1a62650e4016aa705dd49e5c937
6
+ metadata.gz: 06c90bbb58c2910078140c46592ee8f6ce81d20504b530e1ca853484b7573a198fd1da7e2f98d54cbb8f76eb471eda50cc41599b5e99cd76733c6e66a7280c57
7
+ data.tar.gz: 696761f2e0863825c0b232d506795758e4eda93a166d32f05095e59b412cde1531dc992e7c89e83afe86b1b568712a57fd9bb5a44d4fad628f181022a4b290c4
data/README.md CHANGED
@@ -19,6 +19,7 @@ This library is up-to-date with the finalized v1 JSON API spec.
19
19
  * [More customizations](#more-customizations)
20
20
  * [Base URL](#base-url)
21
21
  * [Root metadata](#root-metadata)
22
+ * [Explicit serializer discovery](#explicit-serializer-discovery)
22
23
  * [Relationships](#relationships)
23
24
  * [Compound documents and includes](#compound-documents-and-includes)
24
25
  * [Relationship path handling](#relationship-path-handling)
@@ -313,6 +314,20 @@ You can pass a `meta` argument to specify top-level metadata:
313
314
  JSONAPI::Serializer.serialize(post, meta: {copyright: 'Copyright 2015 Example Corp.'})
314
315
  ```
315
316
 
317
+ ### Explicit serializer discovery
318
+
319
+ By default, jsonapi-serializers assumes that the serializer class for `Namespace::User` is `Namespace::UserSerializer`. You can override this behavior on a per-object basis by implementing the `jsonapi_serializer_class_name` method.
320
+
321
+ ```ruby
322
+ class User
323
+ def jsonapi_serializer_class_name
324
+ 'SomeOtherNamespace::CustomUserSerializer'
325
+ end
326
+ end
327
+ ```
328
+
329
+ Now, when a `User` object is serialized, it will use the `SomeOtherNamespace::CustomUserSerializer`.
330
+
316
331
  ## Relationships
317
332
 
318
333
  You can easily specify relationships with the `has_one` and `has_many` directives.
@@ -513,9 +528,16 @@ module JSONAPI
513
528
  MIMETYPE = "application/vnd.api+json"
514
529
  end
515
530
  Mime::Type.register(JSONAPI::MIMETYPE, :api_json)
531
+
532
+ # Rails 4
516
533
  ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MIMETYPE)] = lambda do |body|
517
534
  JSON.parse(body)
518
535
  end
536
+
537
+ # Rails 5 moved DEFAULT_PARSERS
538
+ ActionDispatch::Http::Parameters::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MIMETYPE)] = lambda do |body|
539
+ JSON.parse(body)
540
+ end
519
541
  ```
520
542
 
521
543
  ## Unfinished business
@@ -527,6 +549,7 @@ end
527
549
 
528
550
  ## Release notes
529
551
 
552
+ * v0.5.0: Support for explicit serializer discovery.
530
553
  * v0.4.0: Support for declaring multiple `attributes`.
531
554
  * v0.3.1: Improve performance of loading included relationships.
532
555
  * v0.3.0: Add top-level `meta` support.
@@ -211,6 +211,9 @@ module JSONAPI
211
211
  end
212
212
 
213
213
  def self.find_serializer_class_name(object)
214
+ if object.respond_to?(:jsonapi_serializer_class_name)
215
+ return object.jsonapi_serializer_class_name.to_s
216
+ end
214
217
  "#{object.class.name}Serializer"
215
218
  end
216
219
 
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Serializer
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -423,7 +423,7 @@ describe JSONAPI::Serializer do
423
423
  expect(JSONAPI::Serializer.serialize(post, include: ['author'])).to eq({
424
424
  'data' => expected_primary_data,
425
425
  'included' => [
426
- serialize_primary(post.author, {serializer: MyApp::UserSerializer}),
426
+ serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
427
427
  ],
428
428
  })
429
429
  end
@@ -518,7 +518,7 @@ describe JSONAPI::Serializer do
518
518
  'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
519
519
  'included' => [
520
520
  # Only the author is included:
521
- serialize_primary(post.author, {serializer: MyApp::UserSerializer}),
521
+ serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
522
522
  ],
523
523
  }
524
524
  includes = ['long-comments.post.author']
@@ -542,8 +542,8 @@ describe JSONAPI::Serializer do
542
542
  # Same note about primary data linkages as above.
543
543
  'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
544
544
  'included' => [
545
- serialize_primary(first_user, {serializer: MyApp::UserSerializer}),
546
- serialize_primary(second_user, {serializer: MyApp::UserSerializer}),
545
+ serialize_primary(first_user, {serializer: MyAppOtherNamespace::UserSerializer}),
546
+ serialize_primary(second_user, {serializer: MyAppOtherNamespace::UserSerializer}),
547
547
  ],
548
548
  }
549
549
  includes = ['long-comments.user']
@@ -571,7 +571,7 @@ describe JSONAPI::Serializer do
571
571
  include_linkages: ['user'],
572
572
  }),
573
573
  # Note: post.author does not show up here because it was not included.
574
- serialize_primary(comment_user, {serializer: MyApp::UserSerializer}),
574
+ serialize_primary(comment_user, {serializer: MyAppOtherNamespace::UserSerializer}),
575
575
  ],
576
576
  }
577
577
  includes = ['long-comments', 'long-comments.user']
@@ -598,7 +598,7 @@ describe JSONAPI::Serializer do
598
598
  'included' => [
599
599
  serialize_primary(long_comments.first, {serializer: MyApp::LongCommentSerializer}),
600
600
  serialize_primary(long_comments.last, {serializer: MyApp::LongCommentSerializer}),
601
- serialize_primary(post.author, {serializer: MyApp::UserSerializer}),
601
+ serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
602
602
  ],
603
603
  }
604
604
  # Also test that it handles string include arguments.
@@ -17,6 +17,10 @@ module MyApp
17
17
  class User
18
18
  attr_accessor :id
19
19
  attr_accessor :name
20
+
21
+ def jsonapi_serializer_class_name
22
+ 'MyAppOtherNamespace::UserSerializer'
23
+ end
20
24
  end
21
25
 
22
26
  class PostSerializer
@@ -41,12 +45,6 @@ module MyApp
41
45
  has_one :post
42
46
  end
43
47
 
44
- class UserSerializer
45
- include JSONAPI::Serializer
46
-
47
- attribute :name
48
- end
49
-
50
48
  # More customized, one-off serializers to test particular behaviors:
51
49
 
52
50
  class SimplestPostSerializer
@@ -141,3 +139,13 @@ module MyApp
141
139
  attributes :title, :body
142
140
  end
143
141
  end
142
+
143
+ # Test the `jsonapi_serializer_class_name` override method for serializers in different namespaces.
144
+ # There is no explicit test for this, just implicit tests that correctly serialize User objects.
145
+ module MyAppOtherNamespace
146
+ class UserSerializer
147
+ include JSONAPI::Serializer
148
+
149
+ attribute :name
150
+ end
151
+ 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.4.0
4
+ version: 0.5.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-01-15 00:00:00.000000000 Z
11
+ date: 2016-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport