-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples_controller.rb
42 lines (34 loc) · 1.12 KB
/
examples_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# This is an example of how to use AsyncCache in a Rails API controller; it
# is loosely based on patterns currently in use in production at Everlane.
class ExamplesController < ApplicationController
def index
locator = 'examples'
version = Example.maximum :updated_at
options = {
expires_in: 1.week,
# Force it to synchronously regenerate for admin users so that they
# can see their changes reflected immediately
synchronous_regen: current_user.admin?
}
json = async_cache.fetch(locator, version, options) do
ExamplesPresenter.new.to_json
end
self.response_body = json
self.content_type = 'application/json'
end
private
def async_cache
Rails.application.config.async_cache
end
end
# We use presenters at Everlane as a sort of view-model. Controllers handle
# application logic such as writing to the database or redirecting clients
# while presenters handle loading data for the views and rendering views.
class ExamplesPresenter < Presenter
def initialize
@examples = Example.all
end
def to_json
@examples.map(&:as_json).to_json
end
end