alchemy-json_api 0.12.2 → 0.13.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: ebad1c20790bcb1426adbae64d0ed2083b3c9e6ee65744600016411cd62082b6
4
- data.tar.gz: 5b0e2ae68275c4b523318c2b4bde1e89ff9f1a5507bfd61af37df4fa13a9c943
3
+ metadata.gz: e8800ea751939b672e2361001cffe2d0da220a6ee61b25c6728c0e0a8db3ca9b
4
+ data.tar.gz: b9a7baa74ef2e0336aa91ad20f03ef26cb8f85c258caa1faf0b8a0c03d665a6f
5
5
  SHA512:
6
- metadata.gz: 1dda52e430cabb4b9082b82bef691bd1b6265c52830c1f7ccf118035801b4b6c742edbd97c49c887439a46c02c3f9fe3f4d213a036e521472170c2a00d08bcea
7
- data.tar.gz: 8561bc959fada0966457c3b8f92269cf5413dd7f4a3adda9f85224c14bb3eafd83f192755cf01af5e9ce456f621025c11a1d3a075aecb68325e62f5af390ade8
6
+ metadata.gz: 3bbefcf52e8b02cad76158b312945bc1a818df8427b658898073bd7cdb8ef09a8d594bb33db0b0d5bcd2a4a9a7b4bd532d2d14a5c5c59f65cd8bbb880394232b
7
+ data.tar.gz: e4a795e0ac64b11930b10d1f582339326f79b9695863930aa69856e93c8ff41f76341710e13dad70074b85fc4a99453605d2725585525c1816fcda19f877c3af
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ module Alchemy
3
+ module JsonApi
4
+ module Admin
5
+ class LayoutPagesController < PagesController
6
+ private
7
+
8
+ def page_scope
9
+ page_scope_with_includes(page_version: :draft_version).layoutpages
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module Alchemy
3
+ module JsonApi
4
+ module Admin
5
+ class PagesController < JsonApi::PagesController
6
+ prepend_before_action { authorize! :edit_content, Alchemy::Page }
7
+
8
+ def show
9
+ render jsonapi: Alchemy::JsonApi::Page.new(@page, page_version: :draft_version)
10
+ end
11
+
12
+ private
13
+
14
+ def page_scope
15
+ page_scope_with_includes(page_version: :draft_version).contentpages
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -9,6 +9,11 @@ module Alchemy
9
9
  include JSONAPI::Filtering
10
10
  include JSONAPI::Pagination
11
11
 
12
+ rescue_from(
13
+ CanCan::AccessDenied,
14
+ with: :render_jsonapi_unauthorized,
15
+ )
16
+
12
17
  private
13
18
 
14
19
  def render_jsonapi_internal_server_error(exception)
@@ -25,6 +30,11 @@ module Alchemy
25
30
  message << " " << exception.backtrace.join("\n ")
26
31
  logger.fatal("#{message}\n\n")
27
32
  end
33
+
34
+ def render_jsonapi_unauthorized(exception)
35
+ error = { status: "401", title: Rack::Utils::HTTP_STATUS_CODES[401] }
36
+ render jsonapi_errors: [error], status: :unauthorized
37
+ end
28
38
  end
29
39
  end
30
40
  end
@@ -4,10 +4,6 @@ module Alchemy
4
4
  class LayoutPagesController < JsonApi::PagesController
5
5
  private
6
6
 
7
- def base_page_scope
8
- ::Alchemy::JsonApi::Page.all
9
- end
10
-
11
7
  def page_scope
12
8
  page_scope_with_includes.layoutpages
13
9
  end
@@ -9,14 +9,16 @@ module Alchemy
9
9
  allowed = [:page_layout]
10
10
 
11
11
  jsonapi_filter(page_scope, allowed) do |filtered|
12
- jsonapi_paginate(filtered.result) do |paginated|
12
+ # decorate with our page model that has a eager loaded elements collection
13
+ pages = filtered.result.map { |p| Alchemy::JsonApi::Page.new(p) }
14
+ jsonapi_paginate(pages) do |paginated|
13
15
  render jsonapi: paginated
14
16
  end
15
17
  end
16
18
  end
17
19
 
18
20
  def show
19
- render jsonapi: @page
21
+ render jsonapi: Alchemy::JsonApi::Page.new(@page)
20
22
  end
21
23
 
22
24
  private
@@ -26,7 +28,7 @@ module Alchemy
26
28
 
27
29
  {
28
30
  pagination: pagination.presence,
29
- total: page_scope.count
31
+ total: page_scope.count,
30
32
  }.compact
31
33
  end
32
34
 
@@ -47,18 +49,31 @@ module Alchemy
47
49
  page_scope_with_includes.contentpages
48
50
  end
49
51
 
50
- def page_scope_with_includes
52
+ def page_scope_with_includes(page_version: :public_version)
51
53
  base_page_scope.
52
- with_language(Language.current).
53
- preload(language: { nodes: [:parent, :page, :children] }, all_elements: { contents: { essence: :ingredient_association } })
54
+ where(language: Language.current).
55
+ includes(
56
+ [
57
+ :legacy_urls,
58
+ { language: { nodes: [:parent, :children, { page: { language: { site: :languages } } }] } },
59
+ {
60
+ page_version => {
61
+ elements: [
62
+ :nested_elements,
63
+ { contents: { essence: :ingredient_association } },
64
+ ],
65
+ },
66
+ },
67
+ ]
68
+ )
54
69
  end
55
70
 
56
71
  def base_page_scope
57
72
  # cancancan is not able to merge our complex AR scopes for logged in users
58
73
  if can?(:edit_content, ::Alchemy::Page)
59
- ::Alchemy::JsonApi::Page.all
74
+ Alchemy::Page.all
60
75
  else
61
- ::Alchemy::JsonApi::Page.published
76
+ Alchemy::Page.published
62
77
  end
63
78
  end
64
79
 
@@ -1,35 +1,28 @@
1
1
  module Alchemy
2
2
  module JsonApi
3
- class Page < BaseRecord
4
- self.table_name = "alchemy_pages"
5
-
6
- belongs_to :language, class_name: "Alchemy::Language"
7
-
8
- has_many :all_elements,
9
- -> { available.order(:position) },
10
- class_name: "Alchemy::JsonApi::Element",
11
- inverse_of: :page
12
-
13
- scope :published, -> {
14
- where("#{table_name}.public_on <= :time AND " \
15
- "(#{table_name}.public_until IS NULL " \
16
- "OR #{table_name}.public_until >= :time)", time: Time.current)
17
- }
3
+ class Page < SimpleDelegator
4
+ def initialize(page, page_version: :public_version)
5
+ @page_version = page.public_send(page_version)
6
+ super(page)
7
+ end
18
8
 
19
- scope :contentpages, -> { where(layoutpage: false) }
20
- scope :layoutpages, -> { where(layoutpage: true) }
21
- scope :with_language, ->(language_id) { where(language_id: language_id) }
9
+ # All elements including nested and fixed elements
10
+ def all_elements
11
+ @_all_elements ||= element_repository
12
+ end
22
13
 
23
- # The top level public, non-fixed elements of this page that - if present -
24
- # contains their nested_elements.
14
+ # Not nested unfixed top level elements
25
15
  def elements
26
- @_elements ||= first_level_elements.reject(&:fixed?)
16
+ @_elements ||= element_repository.not_nested.unfixed
27
17
  end
28
18
 
29
- # The top level public, fixed elements of this page that - if present -
30
- # contains their nested_elements.
19
+ # Not nested fixed top level elements
31
20
  def fixed_elements
32
- @_fixed_elements ||= first_level_elements.select(&:fixed?)
21
+ @_fixed_elements ||= element_repository.not_nested.fixed
22
+ end
23
+
24
+ def all_element_ids
25
+ @_all_element_ids ||= all_elements.map(&:id)
33
26
  end
34
27
 
35
28
  def element_ids
@@ -42,8 +35,11 @@ module Alchemy
42
35
 
43
36
  private
44
37
 
45
- def first_level_elements
46
- @_first_level_elements ||= all_elements.reject(&:parent_element_id)
38
+ def element_repository
39
+ return Alchemy::ElementsRepository.none unless @page_version
40
+
41
+ # Need to use select here, otherwise rails would not eager load the elements correctly
42
+ Alchemy::ElementsRepository.new(@page_version.elements.select(&:public))
47
43
  end
48
44
  end
49
45
  end
@@ -16,10 +16,6 @@ module Alchemy
16
16
  !!element.definition[:deprecated]
17
17
  end
18
18
 
19
- belongs_to :parent_element, record_type: :element, serializer: self
20
-
21
- belongs_to :page, record_type: :page, serializer: ::Alchemy::JsonApi::PageSerializer
22
-
23
19
  has_many :essences, polymorphic: true do |element|
24
20
  element.contents.map(&:essence)
25
21
  end
@@ -18,6 +18,10 @@ module Alchemy
18
18
  :updated_at,
19
19
  )
20
20
 
21
+ attribute :legacy_urls do |page|
22
+ page.legacy_urls.map(&:urlname)
23
+ end
24
+
21
25
  belongs_to :language, record_type: :language, serializer: ::Alchemy::JsonApi::LanguageSerializer
22
26
 
23
27
  # All public elements of this page regardless of if they are fixed or nested.
data/config/routes.rb CHANGED
@@ -4,4 +4,10 @@ Alchemy::JsonApi::Engine.routes.draw do
4
4
  get "pages/*path" => "pages#show", as: :page
5
5
  resources :layout_pages, only: [:index]
6
6
  get "layout_pages/*path" => "layout_pages#show", as: :layout_page
7
+
8
+ namespace :admin do
9
+ get "pages/*path" => "pages#show", as: :page
10
+ resources :layout_pages, only: [:index]
11
+ get "layout_pages/*path" => "layout_pages#show", as: :layout_page
12
+ end
7
13
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Alchemy
3
3
  module JsonApi
4
- VERSION = "0.12.2"
4
+ VERSION = "0.13.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.12.2
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Meyerhoff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-27 00:00:00.000000000 Z
11
+ date: 2021-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: alchemy_cms
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: 6.0.a
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '6.1'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '5.0'
29
+ version: 6.0.a
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '6.1'
@@ -124,10 +124,11 @@ files:
124
124
  - MIT-LICENSE
125
125
  - README.md
126
126
  - Rakefile
127
+ - app/controllers/alchemy/json_api/admin/layout_pages_controller.rb
128
+ - app/controllers/alchemy/json_api/admin/pages_controller.rb
127
129
  - app/controllers/alchemy/json_api/base_controller.rb
128
130
  - app/controllers/alchemy/json_api/layout_pages_controller.rb
129
131
  - app/controllers/alchemy/json_api/pages_controller.rb
130
- - app/models/alchemy/json_api/element.rb
131
132
  - app/models/alchemy/json_api/page.rb
132
133
  - app/serializers/alchemy/json_api/element_serializer.rb
133
134
  - app/serializers/alchemy/json_api/essence_boolean_serializer.rb
@@ -1,34 +0,0 @@
1
- module Alchemy
2
- module JsonApi
3
- class Element < BaseRecord
4
- include Alchemy::Logger
5
- include Alchemy::Element::Definitions
6
- include Alchemy::Element::ElementContents
7
-
8
- self.table_name = "alchemy_elements"
9
-
10
- belongs_to :page, class_name: "Alchemy::JsonApi::Page", inverse_of: :all_elements
11
- has_many :contents, class_name: "Alchemy::Content", inverse_of: :element
12
-
13
- scope :available, -> { where(public: true).where.not(position: nil) }
14
-
15
- def parent_element
16
- page.all_elements.detect do |element|
17
- element.id == parent_element_id
18
- end
19
- end
20
-
21
- def nested_elements
22
- @_nested_elements ||= begin
23
- page.all_elements.select do |element|
24
- element.parent_element_id == id
25
- end
26
- end
27
- end
28
-
29
- def nested_element_ids
30
- nested_elements.map(&:id)
31
- end
32
- end
33
- end
34
- end