swagger-serializer 0.6.2 → 0.7.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
  SHA256:
3
- metadata.gz: bda748bb32d9308010609db0d3e86aa515ec85207d8ebba302f90e897139e500
4
- data.tar.gz: a32eea14539ec58fcdbf3f9ef491ffc844a18247bf3c812e6e43a0be3e772f3a
3
+ metadata.gz: c981a97501496cce000677d6bb67c02db75b948d00541c683ef56c0fcf738d47
4
+ data.tar.gz: 1f53440f04121534346790081311a9e8907ffce11c8244c9bc8ca5dc4c405889
5
5
  SHA512:
6
- metadata.gz: e8b3dd2abbbaed3fc208dc38fb5c92033eadce081a6ee031a759654c6ae6711b06d57c5ddab36251d5210e1e21052024b0802fb7a662bfbd25a09b2484626a2a
7
- data.tar.gz: 4d313ee5fb65c7de7510f7e6e16143d41deff79e93d7411e78c52acc5d3afe72f30b9bb2c547f273849af3fe3ca8446bfbafb0f134bff60d58c39fa251440fb3
6
+ metadata.gz: 2da0855070adbcaf65cd2e52569bff14fdb30b106af975acbcfbf33c18cd61a37a4e0b934580599c8479dda4de3e0b0c8baf67cd6aef786608ced9991821a9b7
7
+ data.tar.gz: 56a07032f129c2fa4d624347fd4ce60adff1ff0397afaaeb53a8c04b84afa39fa275b8391d5e13e70e7c49e148f0e4f9becf0621f41a560d9904f2e4979c2806
data/README.md CHANGED
@@ -54,12 +54,12 @@ class ApplicationController < ActionController::Base
54
54
  include Swagger::Serializer::RailsController
55
55
  extend Swagger::DSL::RailsController
56
56
 
57
- def render_ok(data)
58
- render_as_schema 200, :json, data
57
+ def render_ok(data, context = nil)
58
+ render_as_schema 200, :json, data, context
59
59
  end
60
60
 
61
- def render_bad(data)
62
- render_as_schema 400, :json, data
61
+ def render_bad(data, context = nil)
62
+ render_as_schema 400, :json, data, context
63
63
  end
64
64
  end
65
65
  ```
@@ -265,8 +265,8 @@ Use it in controllers.
265
265
  class ApplicationController < ActionController::Base
266
266
  include Swagger::Serializer::RailsController
267
267
 
268
- def render_ok(data)
269
- render_as_schema 200, :json, data
268
+ def render_ok(data, context = nil)
269
+ render_as_schema 200, :json, data, context
270
270
  end
271
271
  end
272
272
  ```
@@ -317,6 +317,43 @@ end
317
317
 
318
318
  Now you can get serialized result by `p User.first.serialize`.
319
319
 
320
+ ### @context
321
+
322
+ Collection serialization sometimes needs context data for avoiding N+1 access or some other reason.
323
+
324
+ `render_as_schema`'s 4th parameter is the context that will be passed to serializers `@context`.
325
+
326
+ ```ruby
327
+ # app/serializers/item_serializer.rb
328
+ class ItemSerializer < BaseSerializer
329
+ swagger do
330
+ id :integer
331
+ rate :integer
332
+ end
333
+
334
+ def rate
335
+ @context[:rates][id]
336
+ end
337
+ end
338
+
339
+ # app/controllers/items_controller.rb
340
+ class ItemsController < ApplicationController
341
+ swagger :index do
342
+ render 200 do
343
+ array! do
344
+ cref! ItemSerializer
345
+ end
346
+ end
347
+ end
348
+
349
+ def index
350
+ items = Item.all
351
+ rates = ItemRate.calc_item_rates(items)
352
+ render_ok items, { rates: rates }
353
+ end
354
+ end
355
+ ```
356
+
320
357
  ## Development
321
358
 
322
359
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -7,8 +7,9 @@ require "yaml"
7
7
 
8
8
  module Swagger
9
9
  module Serializer
10
- def initialize(model)
10
+ def initialize(model, context = nil)
11
11
  @model = model
12
+ @context = context
12
13
  end
13
14
 
14
15
  def respond_to_missing?(name, include_private)
@@ -3,13 +3,13 @@ module Swagger::Serializer::RailsController
3
3
 
4
4
  # render
5
5
 
6
- def render_as_schema(code, format, data)
7
- render format => render_serializer(code, format).serialize(data)
6
+ def render_as_schema(code, format, data, context = nil)
7
+ render format => render_serializer(code, format, context).serialize(data)
8
8
  end
9
9
 
10
- def render_serializer(code, format)
10
+ def render_serializer(code, format, context = nil)
11
11
  response_obj = swagger_operation.responses[code]
12
- response_obj.content.send(format).serializer(Swagger::Serializer::Store.current.serialize_options)
12
+ response_obj.content.send(format).serializer(Swagger::Serializer::Store.current.serialize_options.merge(inject_context: context))
13
13
  end
14
14
 
15
15
  def swagger_operation
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  module Serializer
3
- VERSION = "0.6.2".freeze
3
+ VERSION = "0.7.0".freeze
4
4
  end
5
5
  end
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = %w[lib]
29
29
 
30
- spec.add_dependency "json-schema-serializer", "~> 1.4"
30
+ spec.add_dependency "json-schema-serializer", "~> 1.6"
31
31
  spec.add_dependency "snake_camel", "~> 1.1"
32
32
  spec.add_development_dependency "bundler", "~> 2"
33
33
  spec.add_development_dependency "rake", "~> 13.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-09 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema-serializer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.4'
19
+ version: '1.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.4'
26
+ version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: snake_camel
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -234,7 +234,7 @@ metadata:
234
234
  homepage_uri: https://github.com/Narazaka/swagger-serializer
235
235
  source_code_uri: https://github.com/Narazaka/swagger-serializer.git
236
236
  changelog_uri: https://github.com/Narazaka/swagger-serializer/blob/master/CHANGELOG.md
237
- documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.6.2
237
+ documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.7.0
238
238
  post_install_message:
239
239
  rdoc_options: []
240
240
  require_paths: