alchemy-json_api 0.20.0 → 0.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +2 -0
- data/app/controllers/alchemy/json_api/admin/layout_pages_controller.rb +1 -1
- data/app/controllers/alchemy/json_api/admin/pages_controller.rb +17 -0
- data/app/controllers/alchemy/json_api/layout_pages_controller.rb +1 -1
- data/app/controllers/alchemy/json_api/pages_controller.rb +46 -16
- data/app/serializers/alchemy/json_api/element_serializer.rb +2 -0
- data/app/serializers/alchemy/json_api/page_serializer.rb +2 -0
- data/lib/alchemy/json_api/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4f9847b4b3ba3f36b06e079974f16b12a7469513edc62a819669da8acd05d9a
|
4
|
+
data.tar.gz: 9c3586a4aa8c5fb04aab2fd3bcfd9e535ef2e91b44fee242a8a2dd9e30ec92cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a097ebe662c11540dda9e3233549fe471cb19c17694dc8ba74577e789ca949b86439a2b488461bfc8355041575f7a8c8527b024a9d7e45448c5a91ecadb37de
|
7
|
+
data.tar.gz: b8413c86022ec12c4193e8914ff2ad5c29fb0353409364eb445fbd78950d07c180a2c970138cf226eb40c328ac195fe1d8694b554cd1d5d0ead25841b6c3c4ae
|
data/Rakefile
CHANGED
@@ -20,6 +20,8 @@ task :test_setup do
|
|
20
20
|
system <<-SETUP.strip_heredoc
|
21
21
|
export RAILS_ENV=test && \
|
22
22
|
bin/rake db:environment:set db:drop && \
|
23
|
+
bin/rake gutentag:install:migrations && \
|
24
|
+
bin/rails g gutentag:migration_versions && \
|
23
25
|
bin/rake railties:install:migrations && \
|
24
26
|
bin/rails g alchemy:devise:install --force && \
|
25
27
|
bin/rake db:migrate
|
@@ -4,9 +4,26 @@ module Alchemy
|
|
4
4
|
module Admin
|
5
5
|
class PagesController < JsonApi::PagesController
|
6
6
|
prepend_before_action { authorize! :edit_content, Alchemy::Page }
|
7
|
+
before_action :set_current_preview, only: :show
|
7
8
|
|
8
9
|
private
|
9
10
|
|
11
|
+
def cache_duration
|
12
|
+
0
|
13
|
+
end
|
14
|
+
|
15
|
+
def caching_options
|
16
|
+
{ public: false, must_revalidate: true }
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_current_preview
|
20
|
+
Alchemy::Page.current_preview = @page
|
21
|
+
end
|
22
|
+
|
23
|
+
def last_modified_for(page)
|
24
|
+
page.updated_at
|
25
|
+
end
|
26
|
+
|
10
27
|
def page_version_type
|
11
28
|
:draft_version
|
12
29
|
end
|
@@ -3,26 +3,57 @@
|
|
3
3
|
module Alchemy
|
4
4
|
module JsonApi
|
5
5
|
class PagesController < JsonApi::BaseController
|
6
|
-
before_action :
|
6
|
+
before_action :load_page_for_cache_key, only: :show
|
7
7
|
|
8
8
|
def index
|
9
|
-
allowed = [:page_layout]
|
10
|
-
|
11
|
-
jsonapi_filter(page_scope, allowed) do |
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
allowed = [:page_layout, :urlname]
|
10
|
+
|
11
|
+
jsonapi_filter(page_scope, allowed) do |filtered_pages|
|
12
|
+
@pages = filtered_pages.result
|
13
|
+
if stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key).cache_key)
|
14
|
+
# Only load pages with all includes when browser cache is stale
|
15
|
+
jsonapi_filter(page_scope_with_includes, allowed) do |filtered|
|
16
|
+
# decorate with our page model that has a eager loaded elements collection
|
17
|
+
filtered_pages = filtered.result.map { |page| api_page(page) }
|
18
|
+
jsonapi_paginate(filtered_pages) do |paginated|
|
19
|
+
render jsonapi: paginated
|
20
|
+
end
|
21
|
+
end
|
16
22
|
end
|
17
23
|
end
|
24
|
+
|
25
|
+
expires_in cache_duration, { public: @pages.none?(&:restricted?) }.merge(caching_options)
|
18
26
|
end
|
19
27
|
|
20
28
|
def show
|
21
|
-
|
29
|
+
if stale?(last_modified: last_modified_for(@page), etag: @page.cache_key)
|
30
|
+
# Only load page with all includes when browser cache is stale
|
31
|
+
render jsonapi: api_page(load_page)
|
32
|
+
end
|
33
|
+
|
34
|
+
expires_in cache_duration, { public: !@page.restricted? }.merge(caching_options)
|
22
35
|
end
|
23
36
|
|
24
37
|
private
|
25
38
|
|
39
|
+
def cache_duration
|
40
|
+
ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 3).to_i.hours
|
41
|
+
end
|
42
|
+
|
43
|
+
def caching_options
|
44
|
+
{ must_revalidate: true }
|
45
|
+
end
|
46
|
+
|
47
|
+
# Get page w/o includes to get cache key
|
48
|
+
def load_page_for_cache_key
|
49
|
+
@page = page_scope.where(id: params[:path]).
|
50
|
+
or(page_scope.where(urlname: params[:path])).first!
|
51
|
+
end
|
52
|
+
|
53
|
+
def last_modified_for(page)
|
54
|
+
page.published_at
|
55
|
+
end
|
56
|
+
|
26
57
|
def jsonapi_meta(pages)
|
27
58
|
pagination = jsonapi_pagination_meta(pages)
|
28
59
|
|
@@ -38,20 +69,19 @@ module Alchemy
|
|
38
69
|
|
39
70
|
def load_page_by_id
|
40
71
|
return unless params[:path] =~ /\A\d+\z/
|
41
|
-
|
72
|
+
page_scope_with_includes.find_by(id: params[:path])
|
42
73
|
end
|
43
74
|
|
44
75
|
def load_page_by_urlname
|
45
|
-
|
76
|
+
page_scope_with_includes.find_by(urlname: params[:path])
|
46
77
|
end
|
47
78
|
|
48
79
|
def page_scope
|
49
|
-
|
80
|
+
base_page_scope.contentpages
|
50
81
|
end
|
51
82
|
|
52
83
|
def page_scope_with_includes
|
53
|
-
|
54
|
-
where(language: Language.current).
|
84
|
+
page_scope.
|
55
85
|
includes(
|
56
86
|
[
|
57
87
|
:legacy_urls,
|
@@ -80,9 +110,9 @@ module Alchemy
|
|
80
110
|
def base_page_scope
|
81
111
|
# cancancan is not able to merge our complex AR scopes for logged in users
|
82
112
|
if can?(:edit_content, ::Alchemy::Page)
|
83
|
-
Alchemy::
|
113
|
+
Alchemy::Language.current.pages.joins(page_version_type)
|
84
114
|
else
|
85
|
-
Alchemy::
|
115
|
+
Alchemy::Language.current.pages.published.joins(page_version_type)
|
86
116
|
end
|
87
117
|
end
|
88
118
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alchemy-json_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.21.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Meyerhoff
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: alchemy_cms
|
@@ -175,7 +175,7 @@ homepage: https://github.com/AlchemyCMS/alchemy-json_api
|
|
175
175
|
licenses:
|
176
176
|
- MIT
|
177
177
|
metadata: {}
|
178
|
-
post_install_message:
|
178
|
+
post_install_message:
|
179
179
|
rdoc_options: []
|
180
180
|
require_paths:
|
181
181
|
- lib
|
@@ -190,8 +190,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
190
190
|
- !ruby/object:Gem::Version
|
191
191
|
version: '0'
|
192
192
|
requirements: []
|
193
|
-
rubygems_version: 3.
|
194
|
-
signing_key:
|
193
|
+
rubygems_version: 3.2.26
|
194
|
+
signing_key:
|
195
195
|
specification_version: 4
|
196
196
|
summary: A JSONAPI compliant API for AlchemyCMS
|
197
197
|
test_files: []
|