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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3935cd3e895377d3abf8138b854c6aa54148da1a77d7fd7c9942b59a467117d9
4
- data.tar.gz: 9c2c96ba95625c68e186558a5316eedf087d375784026d50ef93a7e4f2e4edec
3
+ metadata.gz: a4f9847b4b3ba3f36b06e079974f16b12a7469513edc62a819669da8acd05d9a
4
+ data.tar.gz: 9c3586a4aa8c5fb04aab2fd3bcfd9e535ef2e91b44fee242a8a2dd9e30ec92cc
5
5
  SHA512:
6
- metadata.gz: fceba1d386c35489b4145a0becdffccdd4f00614546b15781038df613b5dea27d5dad7104ff00ad2920f52728ddb729f184d40b97542b36d96c2b05b0fb77216
7
- data.tar.gz: f01bfc78e0d1911abf74666a4b8f19a6e0288d287125f0072364263c8450038b4bf6b8a808bdab65ed49c8265fdcdb9db2df883f60674707298e173144e11992
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
@@ -6,7 +6,7 @@ module Alchemy
6
6
  private
7
7
 
8
8
  def page_scope
9
- page_scope_with_includes.layoutpages
9
+ base_page_scope.layoutpages
10
10
  end
11
11
  end
12
12
  end
@@ -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
@@ -5,7 +5,7 @@ module Alchemy
5
5
  private
6
6
 
7
7
  def page_scope
8
- page_scope_with_includes.layoutpages
8
+ base_page_scope.layoutpages
9
9
  end
10
10
  end
11
11
  end
@@ -3,26 +3,57 @@
3
3
  module Alchemy
4
4
  module JsonApi
5
5
  class PagesController < JsonApi::BaseController
6
- before_action :load_page, only: :show
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 |filtered|
12
- # decorate with our page model that has a eager loaded elements collection
13
- pages = filtered.result.map { |page| api_page(page) }
14
- jsonapi_paginate(pages) do |paginated|
15
- render jsonapi: paginated
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
- render jsonapi: api_page(@page)
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
- page_scope.find_by(id: params[:path])
72
+ page_scope_with_includes.find_by(id: params[:path])
42
73
  end
43
74
 
44
75
  def load_page_by_urlname
45
- page_scope.find_by(urlname: params[:path])
76
+ page_scope_with_includes.find_by(urlname: params[:path])
46
77
  end
47
78
 
48
79
  def page_scope
49
- page_scope_with_includes.contentpages
80
+ base_page_scope.contentpages
50
81
  end
51
82
 
52
83
  def page_scope_with_includes
53
- base_page_scope.
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::Page.all.joins(page_version_type)
113
+ Alchemy::Language.current.pages.joins(page_version_type)
84
114
  else
85
- Alchemy::Page.published.joins(page_version_type)
115
+ Alchemy::Language.current.pages.published.joins(page_version_type)
86
116
  end
87
117
  end
88
118
 
@@ -12,6 +12,8 @@ module Alchemy
12
12
  :updated_at,
13
13
  )
14
14
 
15
+ cache_options store: Rails.cache, namespace: "alchemy-jsonapi"
16
+
15
17
  attribute :deprecated do |element|
16
18
  !!element.definition[:deprecated]
17
19
  end
@@ -18,6 +18,8 @@ module Alchemy
18
18
  :updated_at,
19
19
  )
20
20
 
21
+ cache_options store: Rails.cache, namespace: "alchemy-jsonapi"
22
+
21
23
  attribute :legacy_urls do |page|
22
24
  page.legacy_urls.map(&:urlname)
23
25
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Alchemy
3
3
  module JsonApi
4
- VERSION = "0.20.0"
4
+ VERSION = "0.21.0"
5
5
  end
6
6
  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.20.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-07-07 00:00:00.000000000 Z
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.1.4
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: []