alchemy-json_api 0.10.1 → 0.11.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: 566f8ea63b33d368120cc948fa2c61b4b0039957249e85718f650e8e7ca8d322
4
- data.tar.gz: 6816b1a2b645b9711a77bd8916814cd245b319e8144c6de278ec3f780c03f36f
3
+ metadata.gz: 97db79acd9c6dfb19a865b855f4fe4849fda6ed306684aebc0006f6e8a0c491a
4
+ data.tar.gz: 3863316005bf0fa85f03a2605ef6f0e295e3115b7cb17f9dd4de9e866a2b50a6
5
5
  SHA512:
6
- metadata.gz: 6041f03d5b9264d25312b7e4e1e50d9e5aae32154a47d8e8178bf7ab1dbd5362127e683d40ecba95f1fd05bb1d503afb6444286a97e99bcf5514fa4d48ade95c
7
- data.tar.gz: 5355f10ab5dc5ae2ff7b2fb9412358467e4d351c23efbb37e8a9fbe6e300e6848d6e2b3a01c58fae8d1fbf8df00749835d55b65c43b2705dfcf2431281ef6a8a
6
+ metadata.gz: 8c52e8ec6c28505682e6469990e4b24c6eb365de7f8ddc05b5b8d0e1a4831e7852e7bb502e95c475ac19af4bafd4402148a0693cdacb7f0f1be715b891b686aa
7
+ data.tar.gz: 554481f252201637ce0daf5ca3429fd456c0ec82b34928dc9f0f959f4f4aaee7ee85923f5a9151548725130d6a5a21c03bc2a2e108163ad16e8a60c5a68b259c
@@ -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]}, all_elements: [:parent_element, :nested_elements, { contents: { essence: :ingredient_association } }])
53
+ preload(language: { nodes: [:parent, :page] }, 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.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.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,48 @@
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 ||= all_elements.select do |element|
27
+ !element.fixed? || element.parent_element_id.nil?
28
+ end
29
+ end
30
+
31
+ # The top level public, fixed elements of this page that - if present -
32
+ # contains their nested_elements.
33
+ def fixed_elements
34
+ @_fixed_elements ||= all_elements.select do |element|
35
+ element.fixed? || element.parent_element_id.nil?
36
+ end
37
+ end
38
+
39
+ def element_ids
40
+ @_element_ids ||= elements.map(&:id)
41
+ end
42
+
43
+ def fixed_element_ids
44
+ @_fixed_element_ids ||= fixed_elements.map(&:id)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -11,22 +11,20 @@ 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
17
22
 
18
23
  has_many :essences, polymorphic: true do |element|
19
- element.contents.reject { |c| !!c.try(:deprecated?) }.map!(&:essence)
24
+ element.contents.map(&:essence)
20
25
  end
21
26
 
22
- has_many :nested_elements, record_type: :element, serializer: self do |element|
23
- element.nested_elements.reject { |c| !!c.try(:deprecated?) }
24
- end
25
-
26
- with_options if: ->(_, params) { params[:admin] == true } do
27
- attribute :tag_list
28
- attribute :display_name, &:display_name_with_preview_text
29
- end
27
+ has_many :nested_elements, record_type: :element, serializer: self
30
28
  end
31
29
  end
32
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,22 +20,17 @@ module Alchemy
18
20
 
19
21
  belongs_to :language, record_type: :language, serializer: ::Alchemy::JsonApi::LanguageSerializer
20
22
 
21
- has_many :elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
22
- page.elements.reject { |e| !!e.try(:deprecated?) }
23
- end
24
-
25
- has_many :fixed_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
26
- page.fixed_elements.reject { |c| !!c.try(:deprecated?) }
27
- end
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
28
26
 
29
- has_many :all_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
30
- page.all_elements.select { |e| e.public? && !e.trashed? && !e.try(:deprecated?) }
31
- end
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
32
30
 
33
- with_options if: ->(_, params) { params[:admin] == true } do
34
- attribute :tag_list
35
- attribute :status
36
- 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
37
34
  end
38
35
  end
39
36
  end
@@ -11,6 +11,9 @@ module Alchemy
11
11
  klass.attribute :role do |essence|
12
12
  essence.content.name
13
13
  end
14
+ klass.attribute :deprecated do |essence|
15
+ !!essence.content.definition[:deprecated]
16
+ end
14
17
  end
15
18
  end
16
19
  end
@@ -5,6 +5,15 @@ 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
+ it "has deprecated attribute set to true" do
15
+ expect(subject[:deprecated]).to eq(true)
16
+ end
8
17
  end
9
18
  end
10
19
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Alchemy
3
3
  module JsonApi
4
- VERSION = "0.10.1"
4
+ VERSION = "0.11.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.10.1
4
+ version: 0.11.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-19 00:00:00.000000000 Z
11
+ date: 2021-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: alchemy_cms
@@ -100,6 +100,20 @@ dependencies:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
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'
103
117
  description: A JSONAPI compliant API for AlchemyCMS
104
118
  email:
105
119
  - mamhoff@gmail.com
@@ -113,6 +127,8 @@ files:
113
127
  - app/controllers/alchemy/json_api/base_controller.rb
114
128
  - app/controllers/alchemy/json_api/layout_pages_controller.rb
115
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
116
132
  - app/serializers/alchemy/json_api/element_serializer.rb
117
133
  - app/serializers/alchemy/json_api/essence_boolean_serializer.rb
118
134
  - app/serializers/alchemy/json_api/essence_date_serializer.rb