Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
10 changes: 10 additions & 0 deletions lib/generators/jsonapi/initializer/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions lib/jsonapi/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions lib/jsonapi/rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol>}]
def jsonapi_fields
Expand Down
1 change: 1 addition & 0 deletions lib/jsonapi/rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down