class JSONAPI::Configuration

Attributes

allow_filter[RW]
allow_sort[RW]
allow_transactions[RW]
always_include_to_many_linkage_data[RW]
always_include_to_one_linkage_data[RW]
cache_formatters[R]
default_allow_include_to_many[RW]
default_allow_include_to_one[RW]
default_caching[RW]
default_page_size[RW]
default_paginator[RW]
default_processor_klass_name[R]
default_resource_cache_field[RW]
exception_class_whitelist[RW]
include_application_backtraces_in_errors[RW]
include_backtraces_in_errors[RW]
json_key_format[R]
maximum_page_size[RW]
raise_if_parameters_not_allowed[RW]
resource_cache[RW]
resource_cache_digest_function[RW]
resource_cache_usage_report_function[RW]
resource_key_type[R]
route_format[R]
top_level_meta_include_page_count[RW]
top_level_meta_include_record_count[RW]
top_level_meta_page_count_key[RW]
top_level_meta_record_count_key[RW]
use_relationship_reflection[RW]
use_text_errors[RW]
warn_on_missing_routes[RW]
warn_on_performance_issues[RW]
warn_on_route_setup_issues[RW]
whitelist_all_exceptions[RW]

Public Class Methods

new() click to toggle source
# File lib/jsonapi/configuration.rb, line 44
def initialize
  #:underscored_key, :camelized_key, :dasherized_key, or custom
  self.json_key_format = :dasherized_key

  #:underscored_route, :camelized_route, :dasherized_route, or custom
  self.route_format = :dasherized_route

  #:integer, :uuid, :string, or custom (provide a proc)
  self.resource_key_type = :integer

  # optional request features
  self.default_allow_include_to_one = true
  self.default_allow_include_to_many = true
  self.allow_sort = true
  self.allow_filter = true

  self.raise_if_parameters_not_allowed = true

  self.warn_on_route_setup_issues = true
  self.warn_on_missing_routes = true
  self.warn_on_performance_issues = true

  # :none, :offset, :paged, or a custom paginator name
  self.default_paginator = :none

  # Output pagination links at top level
  self.top_level_links_include_pagination = true

  self.default_page_size = 10
  self.maximum_page_size = 20

  # Metadata
  # Output record count in top level meta for find operation
  self.top_level_meta_include_record_count = false
  self.top_level_meta_record_count_key = :record_count

  self.top_level_meta_include_page_count = false
  self.top_level_meta_page_count_key = :page_count

  self.use_text_errors = false

  # Whether or not to include exception backtraces in JSONAPI error
  # responses.  Defaults to `false` in anything other than development or test.
  self.include_backtraces_in_errors = (Rails.env.development? || Rails.env.test?)

  # Whether or not to include exception application backtraces in JSONAPI error
  # responses.  Defaults to `false` in anything other than development or test.
  self.include_application_backtraces_in_errors = (Rails.env.development? || Rails.env.test?)

  # List of classes that should not be rescued by the operations processor.
  # For example, if you use Pundit for authorization, you might
  # raise a Pundit::NotAuthorizedError at some point during operations
  # processing. If you want to use Rails' `rescue_from` macro to
  # catch this error and render a 403 status code, you should add
  # the `Pundit::NotAuthorizedError` to the `exception_class_whitelist`.
  self.exception_class_whitelist = []

  # If enabled, will override configuration option `exception_class_whitelist`
  # and whitelist all exceptions.
  self.whitelist_all_exceptions = false

  # Resource Linkage
  # Controls the serialization of resource linkage for non compound documents
  # NOTE: always_include_to_many_linkage_data is not currently implemented
  self.always_include_to_one_linkage_data = false
  self.always_include_to_many_linkage_data = false

  # The default Operation Processor to use if one is not defined specifically
  # for a Resource.
  self.default_processor_klass_name = 'JSONAPI::Processor'

  # Allows transactions for creating and updating records
  # Set this to false if your backend does not support transactions (e.g. Mongodb)
  self.allow_transactions = true

  # Formatter Caching
  # Set to false to disable caching of string operations on keys and links.
  # Note that unlike the resource cache, formatter caching is always done
  # internally in-memory and per-thread; no ActiveSupport::Cache is used.
  self.cache_formatters = true

  # Relationship reflection invokes the related resource when updates
  # are made to a has_many relationship. By default relationship_reflection
  # is turned off because it imposes a small performance penalty.
  self.use_relationship_reflection = false

  # Resource cache
  # An ActiveSupport::Cache::Store or similar, used by Resources with caching enabled.
  # Set to `nil` (the default) to disable caching, or to `Rails.cache` to use the
  # Rails cache store.
  self.resource_cache = nil

  # Cache resources by default
  # Cache resources by default. Individual resources can be excluded from caching by calling:
  # `caching false`
  self.default_caching = false

  # Default resource cache field
  # On Resources with caching enabled, this field will be used to check for out-of-date
  # cache entries, unless overridden on a specific Resource. Defaults to "updated_at".
  self.default_resource_cache_field = :updated_at

  # Resource cache digest function
  # Provide a callable that returns a unique value for string inputs with
  # low chance of collision. The default is SHA256 base64.
  self.resource_cache_digest_function = Digest::SHA2.new.method(:base64digest)

  # Resource cache usage reporting
  # Optionally provide a callable which JSONAPI will call with information about cache
  # performance. Should accept three arguments: resource name, hits count, misses count.
  self.resource_cache_usage_report_function = nil

  # Global configuration for links exclusion
  # Controls whether to generate links like `self`, `related` with all the resources
  # and relationships. Accepts either `:default`, `:none`, or array containing the
  # specific default links to exclude, which may be `:self` and `:related`.
  self.default_exclude_links = :none
end

Public Instance Methods

allow_include=(allow_include) click to toggle source
# File lib/jsonapi/configuration.rb, line 241
def allow_include=(allow_include)
  ActiveSupport::Deprecation.warn('`allow_include` has been replaced by `default_allow_include_to_one` and `default_allow_include_to_many` options.')
  @default_allow_include_to_one = allow_include
  @default_allow_include_to_many = allow_include
end
cache_formatters=(bool) click to toggle source
# File lib/jsonapi/configuration.rb, line 163
def cache_formatters=(bool)
  @cache_formatters = bool
  if bool
    @key_formatter_tlv = Concurrent::ThreadLocalVar.new
    @route_formatter_tlv = Concurrent::ThreadLocalVar.new
  else
    @key_formatter_tlv = nil
    @route_formatter_tlv = nil
  end
end
default_processor_klass() click to toggle source
# File lib/jsonapi/configuration.rb, line 232
def default_processor_klass
  @default_processor_klass ||= default_processor_klass_name.safe_constantize
end
default_processor_klass=(default_processor_klass) click to toggle source
# File lib/jsonapi/configuration.rb, line 227
def default_processor_klass=(default_processor_klass)
  ActiveSupport::Deprecation.warn('`default_processor_klass` has been replaced by `default_processor_klass_name`.')
  @default_processor_klass = default_processor_klass
end
default_processor_klass_name=(default_processor_klass_name) click to toggle source
# File lib/jsonapi/configuration.rb, line 236
def default_processor_klass_name=(default_processor_klass_name)
  @default_processor_klass = nil
  @default_processor_klass_name = default_processor_klass_name
end
exception_class_whitelisted?(e) click to toggle source
# File lib/jsonapi/configuration.rb, line 222
def exception_class_whitelisted?(e)
  @whitelist_all_exceptions ||
    @exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
end
json_key_format=(format) click to toggle source
# File lib/jsonapi/configuration.rb, line 174
def json_key_format=(format)
  @json_key_format = format
  if defined?(@cache_formatters)
    @key_formatter_tlv = Concurrent::ThreadLocalVar.new
  end
end
key_formatter() click to toggle source
# File lib/jsonapi/configuration.rb, line 188
def key_formatter
  if self.cache_formatters
    formatter = @key_formatter_tlv.value
    return formatter if formatter
  end

  formatter = JSONAPI::Formatter.formatter_for(self.json_key_format)

  if self.cache_formatters
    formatter = @key_formatter_tlv.value = formatter.cached
  end

  return formatter
end
resource_key_type=(key_type) click to toggle source
# File lib/jsonapi/configuration.rb, line 203
def resource_key_type=(key_type)
  @resource_key_type = key_type
end
route_format=(format) click to toggle source
# File lib/jsonapi/configuration.rb, line 181
def route_format=(format)
  @route_format = format
  if defined?(@cache_formatters)
    @route_formatter_tlv = Concurrent::ThreadLocalVar.new
  end
end
route_formatter() click to toggle source
# File lib/jsonapi/configuration.rb, line 207
def route_formatter
  if self.cache_formatters
    formatter = @route_formatter_tlv.value
    return formatter if formatter
  end

  formatter = JSONAPI::Formatter.formatter_for(self.route_format)

  if self.cache_formatters
    formatter = @route_formatter_tlv.value = formatter.cached
  end

  return formatter
end