Module: JsonapiCompliable::Base
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/jsonapi_compliable/base.rb
Overview
Provides main interface to jsonapi_compliable
This gets mixed in to a “context” class, such as a Rails controller.
Class Method Summary collapse
-
.jsonapi(foo = 'bar', resource: nil, &blk) ⇒ void
Define your JSONAPI configuration.
-
.sideload_whitelist(hash) ⇒ Object
Set the sideload whitelist.
Instance Method Summary collapse
-
#default_jsonapi_render_options ⇒ Hash
Define a hash that will be automatically merged into your render_jsonapi call.
- #deserialized_params ⇒ Deserializer
-
#jsonapi_context ⇒ Object
Override if you’d like the “context” to be something other than an instance of this class.
-
#jsonapi_create ⇒ Util::ValidationResponse
Create the resource model and process all nested relationships via the serialized parameters.
-
#jsonapi_destroy ⇒ Util::ValidationResponse
Delete the model Any error, including validation errors, will roll back the transaction.
-
#jsonapi_resource ⇒ Resource
Returns an instance of the associated Resource.
-
#jsonapi_scope(scope, opts = {}) ⇒ Scope
Use when direct, low-level access to the scope is required.
-
#jsonapi_update ⇒ Util::ValidationResponse
Update the resource model and process all nested relationships via the serialized parameters.
-
#query ⇒ Query
Instantiates the relevant Query object.
-
#query_hash ⇒ Hash
The normalized query hash for only the current resource.
-
#render_jsonapi(scope, opts = {}) ⇒ Object
Similar to render :json or render :jsonapi.
- #sideload_whitelist ⇒ Object private
-
#wrap_context ⇒ Object
private
Tracks the current context so we can refer to it within any random object.
Class Method Details
.jsonapi(foo = 'bar', resource: nil, &blk) ⇒ void
This method returns an undefined value.
Define your JSONAPI configuration
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonapi_compliable/base.rb', line 45 def jsonapi(foo = 'bar', resource: nil, &blk) if resource self._jsonapi_compliable = resource else if !self._jsonapi_compliable self._jsonapi_compliable = Class.new(JsonapiCompliable::Resource) end end self._jsonapi_compliable.class_eval(&blk) if blk end |
.sideload_whitelist(hash) ⇒ Object
Set the sideload whitelist. You may want to omit sideloads for security or performance reasons.
Uses JSONAPI::IncludeDirective from href="http://jsonapi-rb.org">jsonapi-rb.org jsonapi-rb}
89 90 91 |
# File 'lib/jsonapi_compliable/base.rb', line 89 def sideload_whitelist(hash) self._sideload_whitelist = JSONAPI::IncludeDirective.new(hash).to_hash end |
Instance Method Details
#default_jsonapi_render_options ⇒ Hash
Define a hash that will be automatically merged into your render_jsonapi call
328 329 330 331 |
# File 'lib/jsonapi_compliable/base.rb', line 328 def {}.tap do || end end |
#deserialized_params ⇒ Deserializer
188 189 190 |
# File 'lib/jsonapi_compliable/base.rb', line 188 def deserialized_params @deserialized_params ||= JsonapiCompliable::Deserializer.new(params, request.env) end |
#jsonapi_context ⇒ Object
Override if you’d like the “context” to be something other than an instance of this class. In Rails, context is the controller instance.
161 162 163 |
# File 'lib/jsonapi_compliable/base.rb', line 161 def jsonapi_context self end |
#jsonapi_create ⇒ Util::ValidationResponse
Create the resource model and process all nested relationships via the serialized parameters. Any error, including validation errors, will roll back the transaction.
216 217 218 219 220 221 222 223 |
# File 'lib/jsonapi_compliable/base.rb', line 216 def jsonapi_create _persist do jsonapi_resource.persist_with_relationships \ deserialized_params., deserialized_params.attributes, deserialized_params.relationships end end |
#jsonapi_destroy ⇒ Util::ValidationResponse
Delete the model Any error, including validation errors, will roll back the transaction.
Note: before_commit
hooks still run unless excluded
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/jsonapi_compliable/base.rb', line 262 def jsonapi_destroy jsonapi_resource.transaction do model = jsonapi_resource.destroy(params[:id]) validator = ::JsonapiCompliable::Util::ValidationResponse.new \ model, deserialized_params validator.validate! jsonapi_resource.before_commit(model, :destroy) validator end end |
#jsonapi_resource ⇒ Resource
Returns an instance of the associated Resource
In other words, if you configured your controller as:
jsonapi resource: MyResource
This returns MyResource.new
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jsonapi_compliable/base.rb', line 108 def jsonapi_resource @jsonapi_resource ||= begin resource = self.class._jsonapi_compliable if resource.is_a?(Hash) resource[action_name.to_sym].new else resource.new end end end |
#jsonapi_scope(scope, opts = {}) ⇒ Scope
Use when direct, low-level access to the scope is required.
182 183 184 |
# File 'lib/jsonapi_compliable/base.rb', line 182 def jsonapi_scope(scope, opts = {}) jsonapi_resource.build_scope(scope, query, opts) end |
#jsonapi_update ⇒ Util::ValidationResponse
Update the resource model and process all nested relationships via the serialized parameters. Any error, including validation errors, will roll back the transaction.
247 248 249 250 251 252 253 254 |
# File 'lib/jsonapi_compliable/base.rb', line 247 def jsonapi_update _persist do jsonapi_resource.persist_with_relationships \ deserialized_params., deserialized_params.attributes, deserialized_params.relationships end end |
#query ⇒ Query
Instantiates the relevant Query object
123 124 125 |
# File 'lib/jsonapi_compliable/base.rb', line 123 def query @query ||= Query.new(jsonapi_resource, params) end |
#query_hash ⇒ Hash
Returns the normalized query hash for only the current resource.
129 130 131 |
# File 'lib/jsonapi_compliable/base.rb', line 129 def query_hash @query_hash ||= query.to_hash[jsonapi_resource.type] end |
#render_jsonapi(scope, opts = {}) ⇒ Object
Similar to render :json or render :jsonapi
By default, this will “build” the scope via #jsonapi_scope
. To avoid this, pass scope: false
This builds relevant options and sends them to JSONAPI::Serializable::SuccessRenderer#renderfrom jsonapi-rb
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/jsonapi_compliable/base.rb', line 304 def render_jsonapi(scope, opts = {}) scope = jsonapi_scope(scope) unless opts[:scope] == false || scope.is_a?(JsonapiCompliable::Scope) opts = .merge(opts) opts = Util::RenderOptions.generate(scope, query_hash, opts) opts[:expose][:context] = self if opts[:include].empty? && force_includes? opts[:include] = deserialized_params.include_directive end perform_render_jsonapi(opts) end |
#sideload_whitelist ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jsonapi_compliable/base.rb', line 21 module ClassMethods # Define your JSONAPI configuration # # @example Inline Resource # # 'Quick and Dirty' solution that does not require a separate # # Resource object # class PostsController < ApplicationController # jsonapi do # type :posts # use_adapter JsonapiCompliable::Adapters::ActiveRecord # # allow_filter :title # end # end # # @example Resource Class (preferred) # # Make code reusable by encapsulating it in a Resource class # class PostsController < ApplicationController # jsonapi resource: PostResource # end # # @see Resource # @param resource [Resource] the Resource class associated to this endpoint # @return [void] def jsonapi(foo = 'bar', resource: nil, &blk) if resource self._jsonapi_compliable = resource else if !self._jsonapi_compliable self._jsonapi_compliable = Class.new(JsonapiCompliable::Resource) end end self._jsonapi_compliable.class_eval(&blk) if blk end # Set the sideload whitelist. You may want to omit sideloads for # security or performance reasons. # # Uses JSONAPI::IncludeDirective from {{http://jsonapi-rb.org jsonapi-rb}} # # @example Whitelisting Relationships # # Given the following whitelist # class PostsController < ApplicationResource # jsonapi resource: MyResource # # sideload_whitelist({ # index: [:blog], # show: [:blog, { comments: :author }] # }) # # # ... code ... # end # # # A request to sideload 'tags' # # # # GET /posts/1?include=tags # # # # ...will silently fail. # # # # A request for comments and tags: # # # # GET /posts/1?include=tags,comments # # # # ...will only sideload comments # # @param [Hash, Array, Symbol] whitelist # @see Query#include_hash def sideload_whitelist(hash) self._sideload_whitelist = JSONAPI::IncludeDirective.new(hash).to_hash end end |
#wrap_context ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Tracks the current context so we can refer to it within any random object. Helpful for easy-access to things like the current user.
139 140 141 142 143 |
# File 'lib/jsonapi_compliable/base.rb', line 139 def wrap_context jsonapi_resource.with_context(jsonapi_context, action_name.to_sym) do yield end end |