Class: Alchemy::JsonApi::PagesController

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

Constant Summary collapse

THREE_HOURS =
10800
JSONAPI_STALEMAKERS =
%i[include fields sort filter page]

Instance Method Summary collapse

Instance Method Details

#indexObject



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

def index
  allowed = Alchemy::Page.ransackable_attributes

  jsonapi_filter(page_scope, allowed) do |filtered_pages|
    @pages = filtered_pages.result

    if !@pages.all?(&:cache_page?)
      render_pages_json(allowed) && return
    elsif stale?(etag: etag(@pages))
      render_pages_json(allowed)
    end
  end

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

#showObject



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

def show
  if !@page.cache_page?
    render(jsonapi: api_page(load_page)) && return
  end

  if stale?(etag: etag(@page))
    # 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