Class: Alchemy::JsonApi::PagesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/alchemy/json_api/pages_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/alchemy/json_api/pages_controller.rb', line 8

def index
  allowed = [:page_layout, :urlname]

  jsonapi_filter(page_scope, allowed) do |filtered_pages|
    @pages = filtered_pages.result
    if stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key)&.cache_key)
      # Only load pages with all includes when browser cache is stale
      jsonapi_filter(page_scope_with_includes, allowed) do |filtered|
        # decorate with our page model that has a eager loaded elements collection
        filtered_pages = filtered.result.map { |page| api_page(page) }
        jsonapi_paginate(filtered_pages) do |paginated|
          render jsonapi: paginated
        end
      end
    end
  end

  expires_in cache_duration, { public: @pages.none?(&:restricted?) }.merge(caching_options)
end

#showObject



28
29
30
31
32
33
34
35
# File 'app/controllers/alchemy/json_api/pages_controller.rb', line 28

def show
  if stale?(last_modified: last_modified_for(@page), etag: @page.cache_key)
    # Only load page with all includes when browser cache is stale
    render jsonapi: api_page(load_page)
  end

  expires_in cache_duration, { public: !@page.restricted? }.merge(caching_options)
end