From ee4017dda9b8bba67297bce6768218fb33602f58 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 12 Sep 2017 16:13:35 +0200 Subject: [PATCH 1/2] Add cache configuration/hook. --- .../jsonapi/initializer/templates/initializer.rb | 10 ++++++++++ lib/jsonapi/rails/configuration.rb | 3 +++ lib/jsonapi/rails/controller.rb | 6 ++++++ lib/jsonapi/rails/renderer.rb | 1 + 4 files changed, 20 insertions(+) diff --git a/lib/generators/jsonapi/initializer/templates/initializer.rb b/lib/generators/jsonapi/initializer/templates/initializer.rb index 720f29b..2bf9117 100644 --- a/lib/generators/jsonapi/initializer/templates/initializer.rb +++ b/lib/generators/jsonapi/initializer/templates/initializer.rb @@ -21,6 +21,16 @@ # version: '1.0' # } # + # # Set default cache. + # # A lambda/proc that will be eval'd in the controller context. + # config.jsonapi_cache = ->() { nil } + # + # # Uncomment the following to enable fragment caching. Make sure you + # # invalidate cache keys accordingly. + # config.jsonapi_cache = lambda { + # Rails.cache + # } + # # # Set default exposures. # # A lambda/proc that will be eval'd in the controller context. # config.jsonapi_expose = lambda { diff --git a/lib/jsonapi/rails/configuration.rb b/lib/jsonapi/rails/configuration.rb index 70e0945..e5690e9 100644 --- a/lib/jsonapi/rails/configuration.rb +++ b/lib/jsonapi/rails/configuration.rb @@ -23,6 +23,8 @@ module Configurable version: '1.0' }.freeze + DEFAULT_JSONAPI_CACHE = ->() { nil } + DEFAULT_JSONAPI_EXPOSE = lambda { { url_helpers: ::Rails.application.routes.url_helpers } } @@ -38,6 +40,7 @@ module Configurable DEFAULT_CONFIG = { jsonapi_class: DEFAULT_JSONAPI_CLASS, jsonapi_errors_class: DEFAULT_JSONAPI_ERRORS_CLASS, + jsonapi_cache: DEFAULT_JSONAPI_CACHE, jsonapi_expose: DEFAULT_JSONAPI_EXPOSE, jsonapi_fields: DEFAULT_JSONAPI_FIELDS, jsonapi_include: DEFAULT_JSONAPI_INCLUDE, diff --git a/lib/jsonapi/rails/controller.rb b/lib/jsonapi/rails/controller.rb index 27c9b3d..f4a172d 100644 --- a/lib/jsonapi/rails/controller.rb +++ b/lib/jsonapi/rails/controller.rb @@ -110,6 +110,12 @@ def jsonapi_expose instance_exec(&JSONAPI::Rails.config[:jsonapi_expose]) end + # Hook for default cache. + # @return [#fetch_multi] + def jsonapi_cache + instance_exec(&JSONAPI::Rails.config[:jsonapi_cache]) + end + # Hook for default fields. # @return [Hash{Symbol=>Array}] def jsonapi_fields diff --git a/lib/jsonapi/rails/renderer.rb b/lib/jsonapi/rails/renderer.rb index 6fc0b6d..423164d 100644 --- a/lib/jsonapi/rails/renderer.rb +++ b/lib/jsonapi/rails/renderer.rb @@ -22,6 +22,7 @@ def render(resources, options, controller) def default_options(options, controller, resources) options.dup.tap do |opts| opts[:class] ||= controller.jsonapi_class + opts[:cache] ||= controller.jsonapi_cache opts[:links] = controller.jsonapi_links .merge!(controller.jsonapi_pagination(resources)) From 963304e3e8580a62182ec9a6a22faeef062d0585 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 12 Sep 2017 16:14:07 +0200 Subject: [PATCH 2/2] [skip ci] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f422a29..08553be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### Added +* Default configuration and hooks for the `cache` parameter. * Default configuration and hooks for the `links` parameter. * Default configuration and hooks for the `fields` parameter. * Default configuration and hooks for the `include` parameter.