collection_json_rails 0.4.1 → 0.4.2

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: 288b63d473c62125ccf2e7c6a4e80484c7d74398
4
- data.tar.gz: 775ecb12f1a7b150ec590108922ffacc031592cf
3
+ metadata.gz: 410b0a8ded42f67f0a3cc2ad82ab1415498ab730
4
+ data.tar.gz: 7b6bdb1f51b54bcc318efb0f7382fc0f3342e28f
5
5
  SHA512:
6
- metadata.gz: 193a143cd2b270066c5598bb8a2c0b6bdb47b4e469a470a3a13b91ba9b1a1353d4a17e5b48633fe2b3a5f2dcd643279a76301befc24bc5d1e6a9577902c13b70
7
- data.tar.gz: 0e2eac5476864c51c6119da0d06d02559012ee18ecb0037be4399c38d42b52016602153257461cd5e5ea95f8ce20b0a46acb81facce04c0362e7524af4a9fdc8
6
+ metadata.gz: 82cbdf3e7e9b150f4e3f55ee9d31c22f2f14321fa239979606667b7d74d01cc802d4fda6bdeb43bd55198e34ef182219aaeebd3ab2d091a5428b174e7a37e349
7
+ data.tar.gz: c8a74db3391e7e4a4514f604c04f62940f77a027033ff76972d904950934371e996bc62fc0cb92b6116bcbcacb0c1c26f15e4970fb5bb867557cd6cd168d06e8
data/README.md CHANGED
@@ -9,7 +9,7 @@ Serializer](https://github.com/carlesjove/collection_json_serializer).
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'collection_json_rails'
12
+ gem 'collection_json_rails', '~> 0.4.2'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -54,6 +54,13 @@ class PostsController < ApplicationController
54
54
  end
55
55
  ```
56
56
 
57
+ If you use this approach, an empty `Array` will be returned when the collection
58
+ is empty (i.e. when there are no Posts). If you still want to respond with
59
+ Collection+JSON in this scenario, you can provide a `serializer` name:
60
+
61
+ ```ruby
62
+ render json: @posts, serializer: PostSerializer, status: :ok
63
+ ```
57
64
 
58
65
  #### Accepting templates:
59
66
 
@@ -10,7 +10,7 @@ module CollectionJson::Rails
10
10
  def options_for_render(options)
11
11
  result = options
12
12
  if options[:json]
13
- result[:json] = serialize(options[:json])
13
+ result[:json] = serialize(options[:json], options)
14
14
  result[:status] = options[:status] || :ok
15
15
  if is_collection_json?(result[:json])
16
16
  result[:content_type] = CollectionJson::MIME_TYPE
@@ -20,8 +20,8 @@ module CollectionJson::Rails
20
20
  result
21
21
  end
22
22
 
23
- def serialize(resource)
24
- serializer_class = serializer_class_of(resource)
23
+ def serialize(resource, options = {})
24
+ serializer_class = options[:serializer] || serializer_class_of(resource)
25
25
 
26
26
  if serializer_class.respond_to?(:new)
27
27
  serializer = serializer_class.new(resource)
@@ -1,5 +1,5 @@
1
1
  module CollectionJson
2
2
  module Rails
3
- VERSION = "0.4.1"
3
+ VERSION = "0.4.2"
4
4
  end
5
5
  end
@@ -9,6 +9,10 @@ class PostsController < ActionController::Base
9
9
  render json: []
10
10
  end
11
11
 
12
+ def empty_array_with_serializer_name
13
+ render json: [], serializer: PostSerializer
14
+ end
15
+
12
16
  def without_json
13
17
  render nothing: true
14
18
  end
@@ -30,6 +34,13 @@ class TestRender < ActionController::TestCase
30
34
  assert_equal Mime::JSON, response.content_type
31
35
  end
32
36
 
37
+ def test_that_a_serializer_name_can_be_specified
38
+ get :empty_array_with_serializer_name
39
+
40
+ assert_response :ok
41
+ assert_equal "application/vnd.collection+json", response.content_type
42
+ end
43
+
33
44
  def test_that_objects_without_serializer_are_rendered_as_plain_json
34
45
  get :without_serializer
35
46
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collection_json_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carles Jove i Buxeda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: collection_json_serializer