alchemy-json_api 0.9.1 → 0.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/alchemy/json_api/layout_pages_controller.rb +1 -1
- data/app/controllers/alchemy/json_api/pages_controller.rb +6 -4
- data/app/models/alchemy/json_api/element.rb +33 -0
- data/app/models/alchemy/json_api/page.rb +50 -0
- data/app/serializers/alchemy/json_api/element_serializer.rb +5 -5
- data/app/serializers/alchemy/json_api/language_serializer.rb +0 -6
- data/app/serializers/alchemy/json_api/page_serializer.rb +11 -9
- data/lib/alchemy/json_api/essence_serializer.rb +3 -0
- data/lib/alchemy/json_api/test_support/essence_serializer_behaviour.rb +18 -0
- data/lib/alchemy/json_api/version.rb +1 -1
- metadata +26 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50b6ab14e92599ee97e7e337688a6cc8ff36e7f82d0eccb589caa97a529e78ab
|
4
|
+
data.tar.gz: 4e031aef41f45e7733e1a07ff47dc058f8b083e53da25315e7c5eefa638c86f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29faa1ddb7bcf46660148824c14d8af8b59a89f41d93df308c26bf0eba08bb975d85906bf9438691aba39142353dfd1ce4a4d97bfca9f0933ee2a3cc738ddacd
|
7
|
+
data.tar.gz: d9bec282c23b43dcdc4ac24b001d2c862e60c6bff3d0c9b92e335fe1dc303803c3dcf4ffcdd2e0ee3b160c770e8a5c07f066b6e8e787057cdb3ec019bc5d1adf
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Alchemy
|
3
4
|
module JsonApi
|
4
5
|
class PagesController < JsonApi::BaseController
|
@@ -34,6 +35,7 @@ module Alchemy
|
|
34
35
|
end
|
35
36
|
|
36
37
|
def load_page_by_id
|
38
|
+
return unless params[:path] =~ /\A\d+\z/
|
37
39
|
page_scope.find_by(id: params[:path])
|
38
40
|
end
|
39
41
|
|
@@ -48,15 +50,15 @@ module Alchemy
|
|
48
50
|
def page_scope_with_includes
|
49
51
|
base_page_scope.
|
50
52
|
with_language(Language.current).
|
51
|
-
preload(language: {nodes: [:parent, :page
|
53
|
+
preload(language: { nodes: [:parent, :page, :children] }, all_elements: { contents: { essence: :ingredient_association } })
|
52
54
|
end
|
53
55
|
|
54
56
|
def base_page_scope
|
55
57
|
# cancancan is not able to merge our complex AR scopes for logged in users
|
56
|
-
if can?(:edit_content, Page)
|
57
|
-
Page.all
|
58
|
+
if can?(:edit_content, ::Alchemy::Page)
|
59
|
+
::Alchemy::JsonApi::Page.all
|
58
60
|
else
|
59
|
-
Page.published
|
61
|
+
::Alchemy::JsonApi::Page.published
|
60
62
|
end
|
61
63
|
end
|
62
64
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Alchemy
|
2
|
+
module JsonApi
|
3
|
+
class Element < BaseRecord
|
4
|
+
include Alchemy::Element::Definitions
|
5
|
+
include Alchemy::Element::ElementContents
|
6
|
+
|
7
|
+
self.table_name = "alchemy_elements"
|
8
|
+
|
9
|
+
belongs_to :page, class_name: "Alchemy::JsonApi::Page", inverse_of: :all_elements
|
10
|
+
has_many :contents, class_name: "Alchemy::Content", inverse_of: :element
|
11
|
+
|
12
|
+
scope :available, -> { where(public: true).where.not(position: nil) }
|
13
|
+
|
14
|
+
def parent_element
|
15
|
+
page.all_elements.detect do |element|
|
16
|
+
element.id == parent_element_id
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def nested_elements
|
21
|
+
@_nested_elements ||= begin
|
22
|
+
page.all_elements.select do |element|
|
23
|
+
element.parent_element_id == id
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def nested_element_ids
|
29
|
+
nested_elements.map(&:id)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Alchemy
|
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
|
+
}
|
18
|
+
|
19
|
+
scope :contentpages, -> { where(layoutpage: false) }
|
20
|
+
scope :layoutpages, -> { where(layoutpage: true) }
|
21
|
+
scope :with_language, ->(language_id) { where(language_id: language_id) }
|
22
|
+
|
23
|
+
# The top level public, non-fixed elements of this page that - if present -
|
24
|
+
# contains their nested_elements.
|
25
|
+
def elements
|
26
|
+
@_elements ||= first_level_elements.reject(&:fixed?)
|
27
|
+
end
|
28
|
+
|
29
|
+
# The top level public, fixed elements of this page that - if present -
|
30
|
+
# contains their nested_elements.
|
31
|
+
def fixed_elements
|
32
|
+
@_fixed_elements ||= first_level_elements.select(&:fixed?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def element_ids
|
36
|
+
@_element_ids ||= elements.map(&:id)
|
37
|
+
end
|
38
|
+
|
39
|
+
def fixed_element_ids
|
40
|
+
@_fixed_element_ids ||= fixed_elements.map(&:id)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def first_level_elements
|
46
|
+
@_first_level_elements ||= all_elements.reject(&:parent_element_id)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -11,6 +11,11 @@ module Alchemy
|
|
11
11
|
:created_at,
|
12
12
|
:updated_at,
|
13
13
|
)
|
14
|
+
|
15
|
+
attribute :deprecated do |element|
|
16
|
+
!!element.definition[:deprecated]
|
17
|
+
end
|
18
|
+
|
14
19
|
belongs_to :parent_element, record_type: :element, serializer: self
|
15
20
|
|
16
21
|
belongs_to :page, record_type: :page, serializer: ::Alchemy::JsonApi::PageSerializer
|
@@ -20,11 +25,6 @@ module Alchemy
|
|
20
25
|
end
|
21
26
|
|
22
27
|
has_many :nested_elements, record_type: :element, serializer: self
|
23
|
-
|
24
|
-
with_options if: ->(_, params) { params[:admin] == true } do
|
25
|
-
attribute :tag_list
|
26
|
-
attribute :display_name, &:display_name_with_preview_text
|
27
|
-
end
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -18,12 +18,6 @@ module Alchemy
|
|
18
18
|
end
|
19
19
|
has_many :pages
|
20
20
|
has_one :root_page, record_type: :page, serializer: ::Alchemy::JsonApi::PageSerializer
|
21
|
-
|
22
|
-
with_options if: ->(_, params) { params[:admin] == true } do
|
23
|
-
attribute :created_at
|
24
|
-
attribute :updated_at
|
25
|
-
attribute :public
|
26
|
-
end
|
27
21
|
end
|
28
22
|
end
|
29
23
|
end
|
@@ -4,6 +4,8 @@ module Alchemy
|
|
4
4
|
class PageSerializer
|
5
5
|
include JSONAPI::Serializer
|
6
6
|
|
7
|
+
ELEMENT_SERIALIZER = ::Alchemy::JsonApi::ElementSerializer
|
8
|
+
|
7
9
|
attributes(
|
8
10
|
:name,
|
9
11
|
:urlname,
|
@@ -18,17 +20,17 @@ module Alchemy
|
|
18
20
|
|
19
21
|
belongs_to :language, record_type: :language, serializer: ::Alchemy::JsonApi::LanguageSerializer
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
# All public elements of this page regardless of if they are fixed or nested.
|
24
|
+
# Used for eager loading and should be used as the +include+ parameter of your query
|
25
|
+
has_many :all_elements, record_type: :element, serializer: ELEMENT_SERIALIZER
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
# The top level public, non-fixed elements of this page that - if present -
|
28
|
+
# contains their nested_elements.
|
29
|
+
has_many :elements, record_type: :element, serializer: ELEMENT_SERIALIZER
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
31
|
+
# The top level public, fixed elements of this page that - if present -
|
32
|
+
# contains their nested_elements.
|
33
|
+
has_many :fixed_elements, record_type: :element, serializer: ELEMENT_SERIALIZER
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
@@ -5,6 +5,24 @@ RSpec.shared_examples "an essence serializer" do
|
|
5
5
|
|
6
6
|
it "has the right keys and values" do
|
7
7
|
expect(subject).to have_key(:ingredient)
|
8
|
+
expect(subject[:deprecated]).to be(false)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "a deprecated content" do
|
12
|
+
let(:content) { FactoryBot.create(:alchemy_content, name: "intro", element: element) }
|
13
|
+
|
14
|
+
before do
|
15
|
+
expect(content).to receive(:definition).at_least(:once) do
|
16
|
+
{
|
17
|
+
name: "intro",
|
18
|
+
deprecated: true,
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "has deprecated attribute set to true" do
|
24
|
+
expect(subject[:deprecated]).to eq(true)
|
25
|
+
end
|
8
26
|
end
|
9
27
|
end
|
10
28
|
|
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
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.12.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Meyerhoff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: alchemy_cms
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '5.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.1'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '5.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.1'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: jsonapi.rb
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +100,20 @@ dependencies:
|
|
94
100
|
- - ">="
|
95
101
|
- !ruby/object:Gem::Version
|
96
102
|
version: '0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: shoulda-matchers
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
97
117
|
description: A JSONAPI compliant API for AlchemyCMS
|
98
118
|
email:
|
99
119
|
- mamhoff@gmail.com
|
@@ -107,6 +127,8 @@ files:
|
|
107
127
|
- app/controllers/alchemy/json_api/base_controller.rb
|
108
128
|
- app/controllers/alchemy/json_api/layout_pages_controller.rb
|
109
129
|
- app/controllers/alchemy/json_api/pages_controller.rb
|
130
|
+
- app/models/alchemy/json_api/element.rb
|
131
|
+
- app/models/alchemy/json_api/page.rb
|
110
132
|
- app/serializers/alchemy/json_api/element_serializer.rb
|
111
133
|
- app/serializers/alchemy/json_api/essence_boolean_serializer.rb
|
112
134
|
- app/serializers/alchemy/json_api/essence_date_serializer.rb
|