alchemy-json_api 0.20.0 → 0.24.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/essence_picture_serializer.rb +21 -0
- data/app/serializers/alchemy/json_api/ingredient_picture_serializer.rb +21 -0
- data/app/serializers/alchemy/json_api/page_serializer.rb +3 -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: 6c6090af6d711e4a909628207f0b5f7d916944c60a8482b5b7ff958a38633d73
|
4
|
+
data.tar.gz: 4e70e1de9b2ab1a68ce359c32973deac67a0965ade5021a7cf398ea29bf83a82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56d2a3623ad9e3a8585d5487f94f08c3d4a31f17a177f300252056eec68f5f4223eb86f32eca26238c5d52ecc6481a39d845b8082ea3d5cb4daec2fc25004a92
|
7
|
+
data.tar.gz: 30ca7d8ca1e07d9e924e395ee42c0d4cbf693180052a70f003ccd592267564c2e701aea50462f24f659aad8f39c687d42fa90f05da2633ce2c90a82228a9a3c0
|
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
|
|
@@ -33,6 +33,27 @@ module Alchemy
|
|
33
33
|
}
|
34
34
|
end
|
35
35
|
|
36
|
+
attribute :srcset do |essence|
|
37
|
+
essence.content.settings.fetch(:srcset, []).map do |src|
|
38
|
+
case src
|
39
|
+
when Hash
|
40
|
+
url = essence.picture_url(src)
|
41
|
+
size = src[:size]
|
42
|
+
else
|
43
|
+
url = essence.picture_url(size: src)
|
44
|
+
size = src
|
45
|
+
end
|
46
|
+
width, height = size.split("x", 2)
|
47
|
+
|
48
|
+
{
|
49
|
+
url: url,
|
50
|
+
desc: "#{width}w",
|
51
|
+
width: width,
|
52
|
+
height: height,
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
36
57
|
attribute :image_name do |essence|
|
37
58
|
essence.picture.name
|
38
59
|
end
|
@@ -39,6 +39,27 @@ module Alchemy
|
|
39
39
|
}
|
40
40
|
end
|
41
41
|
|
42
|
+
attribute :srcset do |ingredient|
|
43
|
+
ingredient.settings.fetch(:srcset, []).map do |src|
|
44
|
+
case src
|
45
|
+
when Hash
|
46
|
+
url = ingredient.picture_url(src)
|
47
|
+
size = src[:size]
|
48
|
+
else
|
49
|
+
url = ingredient.picture_url(size: src)
|
50
|
+
size = src
|
51
|
+
end
|
52
|
+
width, height = size.split("x", 2)
|
53
|
+
|
54
|
+
{
|
55
|
+
url: url,
|
56
|
+
desc: "#{width}w",
|
57
|
+
width: width,
|
58
|
+
height: height,
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
42
63
|
attribute :image_name do |ingredient|
|
43
64
|
ingredient.picture.name
|
44
65
|
end
|
@@ -9,6 +9,7 @@ module Alchemy
|
|
9
9
|
attributes(
|
10
10
|
:name,
|
11
11
|
:urlname,
|
12
|
+
:url_path,
|
12
13
|
:page_layout,
|
13
14
|
:title,
|
14
15
|
:language_code,
|
@@ -18,6 +19,8 @@ module Alchemy
|
|
18
19
|
:updated_at,
|
19
20
|
)
|
20
21
|
|
22
|
+
cache_options store: Rails.cache, namespace: "alchemy-jsonapi"
|
23
|
+
|
21
24
|
attribute :legacy_urls do |page|
|
22
25
|
page.legacy_urls.map(&:urlname)
|
23
26
|
end
|
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.24.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-11-05 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: []
|