jsonapi-realizer 4.0.1 → 4.1.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: c4a1e03486bb8663b51ad6ea08cbb64c33fe80027183ef2639e70f8460680138
4
- data.tar.gz: 680d9e08548505ac0c7bb26b948a46d2f66328979a41524622789074e58bbd56
3
+ metadata.gz: 8cb7f214558b3e9926ec20b9156c10d8e4b00bf29c1b8ef2bbade489a066ece2
4
+ data.tar.gz: 29abee993505d8728b4092542207541864baec6dfee5e8d5739996b6d58367d8
5
5
  SHA512:
6
- metadata.gz: ad18e205c934375d6052faf8220a217e81e7f0c3d09dae35aaa8310807023cd259f4c7b7eece4b93737ec03398a7906ee0f4d23efd32837cd2461de3cb6300ff
7
- data.tar.gz: 614fc08428ce221a595b026811add7e8d8c88f5139588c5f96f460cbee670c552339a8c8b1151ee0eb8218caa56ddaa5ff37c13acdfe46868987cf5ec42e3567
6
+ metadata.gz: 8ff7e820e1a2b5222afd5076e4822d31f706302501a1a020c9dd4ae5cb8c327d2cb741b3f687d2e376b876166d1aadd85bfab770542604f76e3ea4ab9d32f26f
7
+ data.tar.gz: cf2485faba6a11e199580c619aed6eb888be5a72cb2582ace2c3c29efda84fd6859c4ebc1cde89af841395e59594fa524e94ddf4c874154fd5da0d6a3cbbcbc9
data/README.md CHANGED
@@ -63,7 +63,7 @@ class PhotosController < ApplicationController
63
63
 
64
64
  ProcessPhotosService.new(realization.model)
65
65
 
66
- render json: JSONAPI::Serializer.serialize(record)
66
+ render json: JSONAPI::Serializer.serialize(realization.model)
67
67
  end
68
68
 
69
69
  def index
@@ -128,6 +128,7 @@ end
128
128
  ``` ruby
129
129
  class PhotosController < ApplicationController
130
130
  def index
131
+ # See: pundit for `policy_scope()`
131
132
  realization = JSONAPI::Realizer.index(
132
133
  policy(Photo).sanitize(:index, params),
133
134
  headers: request.headers,
@@ -135,9 +136,10 @@ class PhotosController < ApplicationController
135
136
  relation: policy_scope(Photo)
136
137
  )
137
138
 
138
- # See: pundit for `policy_scope()`
139
139
  # See: pundit for `authorize()`
140
- render json: JSONAPI::Serializer.serialize(authorize(realization.models), is_collection: true)
140
+ authorize(realization.relation)
141
+
142
+ render json: JSONAPI::Serializer.serialize(realization.models, is_collection: true)
141
143
  end
142
144
  end
143
145
  ```
@@ -181,6 +183,121 @@ class PhotoRealizer
181
183
  end
182
184
  ```
183
185
 
186
+ ### rails and jsonapi-realizer and jsonapi-serializers and pundit
187
+
188
+ While this gem contains nothing specifically targeting Rails or pundit or [jsonapi-serializers](https://github.com/fotinakis/jsonapi-serializers) (a fantastic gem) I've already written some seamless integration code. This root controller will handle exceptions in a graceful way and also give you access to a clean interface for serializing:
189
+
190
+ ``` ruby
191
+ module V1
192
+ class ApplicationController < ::ApplicationController
193
+ include Pundit
194
+
195
+ after_action :verify_authorized, except: :index
196
+ after_action :verify_policy_scoped, only: :index
197
+
198
+ rescue_from JSONAPI::Realizer::Error::MissingAcceptHeader, with: :missing_accept_header
199
+ rescue_from JSONAPI::Realizer::Error::InvalidAcceptHeader, with: :invalid_accept_header
200
+ rescue_from Pundit::NotAuthorizedError, with: :access_not_authorized
201
+
202
+ private def missing_accept_header
203
+ head :not_acceptable
204
+ end
205
+
206
+ private def invalid_accept_header
207
+ head :not_acceptable
208
+ end
209
+
210
+ private def access_not_authorized
211
+ head :unauthorized
212
+ end
213
+
214
+ private def pundit_user
215
+ current_account
216
+ end
217
+
218
+ private def serialize(realization)
219
+ JSONAPI::Serializer.serialize(
220
+ if realization.respond_to?(:models) then realization.models else realization.model end,
221
+ is_collection: realization.respond_to?(:models),
222
+ meta: serialized_metadata,
223
+ links: serialized_links,
224
+ jsonapi: serialized_jsonapi,
225
+ fields: serialized_fields(realization),
226
+ include: serialized_includes(realization),
227
+ namespace: ::V1
228
+ )
229
+ end
230
+
231
+ private def serialized_metadata
232
+ {
233
+ api: {
234
+ version: "1"
235
+ }
236
+ }
237
+ end
238
+
239
+ private def serialized_links
240
+ {
241
+ discovery: {
242
+ href: "/"
243
+ }
244
+ }
245
+ end
246
+
247
+ private def serialized_jsonapi
248
+ {
249
+ version: "1.0"
250
+ }
251
+ end
252
+
253
+ private def serialized_fields(realization)
254
+ realization.fields if realization.fields.any?
255
+ end
256
+
257
+ private def serialized_includes(realization)
258
+ realization.includes if realization.includes.any?
259
+ end
260
+ end
261
+ end
262
+ ```
263
+
264
+ You can see this resource controller used below:
265
+
266
+ ``` ruby
267
+ module V1
268
+ class AccountsController < ::V1::ApplicationController
269
+ def index
270
+ realization = JSONAPI::Realizer.index(
271
+ policy(Account).sanitize(:index, params),
272
+ headers: request.headers,
273
+ scope: policy_scope(Account),
274
+ type: :accounts
275
+ )
276
+
277
+ authorize realization.relation
278
+
279
+ render json: serialize(realization)
280
+ end
281
+
282
+ def create
283
+ realization = JSONAPI::Realizer.create(
284
+ policy(Account).sanitize(:create, params),
285
+ headers: request.headers,
286
+ scope: policy_scope(Account)
287
+ )
288
+
289
+ authorize realization.relation
290
+
291
+ render json: serialize(realization)
292
+ end
293
+ end
294
+ end
295
+ ```
296
+
297
+ ### jsonapi-home
298
+
299
+ I'm already using jsonapi-realizer and it's sister project jsonapi-serializers in a new gem of mine that allows services to be discoverable: [jsonapi-home](https://github.com/krainboltgreene/jsonapi-home).
300
+
184
301
  ### Notes
185
302
 
186
303
  A successful JSON:API request can be annotated as:
@@ -206,7 +323,7 @@ BusinessLayer -> JSONAPIRequest -> (Record | Array<Record>)
206
323
 
207
324
  Add this line to your application's Gemfile:
208
325
 
209
- gem "jsonapi-realizer", "3.0.0"
326
+ gem "jsonapi-realizer", "4.1.0"
210
327
 
211
328
  And then execute:
212
329
 
@@ -4,7 +4,7 @@ require "active_support/core_ext/enumerable"
4
4
  require "active_support/core_ext/string"
5
5
 
6
6
  module JSONAPI
7
- MEDIA_TYPE = "application/vnd.api+json"
7
+ MEDIA_TYPE = "application/vnd.api+json" unless const_defined?("MEDIA_TYPE")
8
8
 
9
9
  module Realizer
10
10
  require_relative "realizer/version"
@@ -51,7 +51,7 @@ module JSONAPI
51
51
  end
52
52
  end
53
53
 
54
- private def relation
54
+ def relation
55
55
  relation_after_fields(
56
56
  relation_after_inclusion(
57
57
  @scope || model_class
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Realizer
3
- VERSION = "4.0.1"
3
+ VERSION = "4.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-realizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kurtis Rainbolt-Greene