module JsonapiCompliable::Base::ClassMethods

@!classmethods

Public Instance Methods

jsonapi(foo = 'bar', resource: nil, &blk) click to toggle source

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]

# 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) click to toggle source

Set the sideload whitelist. You may want to omit sideloads for security or performance reasons.

Uses JSONAPI::IncludeDirective from {{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

# File lib/jsonapi_compliable/base.rb, line 89
def sideload_whitelist(hash)
  self._sideload_whitelist = JSONAPI::IncludeDirective.new(hash).to_hash
end