Module: CacheCrispies::Controller
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/cache_crispies/controller.rb
Overview
A Rails concern designed to be used in Rails controllers to provide access to the #cache_render method
Constant Summary collapse
- OJ_MODE =
The serialization mode that should be used with the oj gem
:rails
Instance Method Summary collapse
-
#cache_render(serializer, cacheable, options = {}) ⇒ void
Renders the provided cacheable object to JSON using the provided serializer.
Instance Method Details
#cache_render(serializer, cacheable, options = {}) ⇒ void
This method returns an undefined value.
Renders the provided cacheable object to JSON using the provided serializer
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cache_crispies/controller.rb', line 28 def cache_render(serializer, cacheable, = {}) plan = CacheCrispies::Plan.new(serializer, cacheable, ) if CacheCrispies.config. response.weak_etag = plan.etag end serializer_json = if plan.collection? plan.cache do CacheCrispies::Collection.new( cacheable, serializer, ).as_json end else plan.cache { serializer.new(cacheable, ).as_json } end render_hash = { json: Oj.dump(plan.wrap(serializer_json), mode: OJ_MODE) } render_hash[:status] = [:status] if .key?(:status) render render_hash end |