Skip to content

Commit f994722

Browse files
authored
Add configuration/hook for cache. (#52)
1 parent 57caae9 commit f994722

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Added
22

3+
* Default configuration and hooks for the `cache` parameter.
34
* Default configuration and hooks for the `links` parameter.
45
* Default configuration and hooks for the `fields` parameter.
56
* Default configuration and hooks for the `include` parameter.

lib/generators/jsonapi/initializer/templates/initializer.rb

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
# version: '1.0'
2222
# }
2323
#
24+
# # Set default cache.
25+
# # A lambda/proc that will be eval'd in the controller context.
26+
# config.jsonapi_cache = ->() { nil }
27+
#
28+
# # Uncomment the following to enable fragment caching. Make sure you
29+
# # invalidate cache keys accordingly.
30+
# config.jsonapi_cache = lambda {
31+
# Rails.cache
32+
# }
33+
#
2434
# # Set default exposures.
2535
# # A lambda/proc that will be eval'd in the controller context.
2636
# config.jsonapi_expose = lambda {

lib/jsonapi/rails/configuration.rb

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module Configurable
2323
version: '1.0'
2424
}.freeze
2525

26+
DEFAULT_JSONAPI_CACHE = ->() { nil }
27+
2628
DEFAULT_JSONAPI_EXPOSE = lambda {
2729
{ url_helpers: ::Rails.application.routes.url_helpers }
2830
}
@@ -38,6 +40,7 @@ module Configurable
3840
DEFAULT_CONFIG = {
3941
jsonapi_class: DEFAULT_JSONAPI_CLASS,
4042
jsonapi_errors_class: DEFAULT_JSONAPI_ERRORS_CLASS,
43+
jsonapi_cache: DEFAULT_JSONAPI_CACHE,
4144
jsonapi_expose: DEFAULT_JSONAPI_EXPOSE,
4245
jsonapi_fields: DEFAULT_JSONAPI_FIELDS,
4346
jsonapi_include: DEFAULT_JSONAPI_INCLUDE,

lib/jsonapi/rails/controller.rb

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ def jsonapi_expose
110110
instance_exec(&JSONAPI::Rails.config[:jsonapi_expose])
111111
end
112112

113+
# Hook for default cache.
114+
# @return [#fetch_multi]
115+
def jsonapi_cache
116+
instance_exec(&JSONAPI::Rails.config[:jsonapi_cache])
117+
end
118+
113119
# Hook for default fields.
114120
# @return [Hash{Symbol=>Array<Symbol>}]
115121
def jsonapi_fields

lib/jsonapi/rails/renderer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def render(resources, options, controller)
2222
def default_options(options, controller, resources)
2323
options.dup.tap do |opts|
2424
opts[:class] ||= controller.jsonapi_class
25+
opts[:cache] ||= controller.jsonapi_cache
2526
opts[:links] =
2627
controller.jsonapi_links
2728
.merge!(controller.jsonapi_pagination(resources))

0 commit comments

Comments
 (0)