alchemy-json_api 0.10.0 → 0.12.2

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: bf4587d53cdd7cc5122e9cfff386da6534132339134bbe77d45261fae3355e25
4
- data.tar.gz: d04f9543b7906bec3ed4960340a3b94ce0e24b9220a2ac039b136d81c5bada01
3
+ metadata.gz: ebad1c20790bcb1426adbae64d0ed2083b3c9e6ee65744600016411cd62082b6
4
+ data.tar.gz: 5b0e2ae68275c4b523318c2b4bde1e89ff9f1a5507bfd61af37df4fa13a9c943
5
5
  SHA512:
6
- metadata.gz: d0b6a8b5911435aba0ab48c23e6a88b5b47d3133be15077b7862e072a8ac5238676407a504acec855c8aae98c84fb68e8e34e9e795464067c4be9039b8e3030e
7
- data.tar.gz: edb82694d23e2404ae6711c98600707f54e09d49e239b7334b8dea8baab966ebc071d49f6c38302d4231ab19b283f8b6969b1aa9467c81e6cd6eaabc57e198dd
6
+ metadata.gz: 1dda52e430cabb4b9082b82bef691bd1b6265c52830c1f7ccf118035801b4b6c742edbd97c49c887439a46c02c3f9fe3f4d213a036e521472170c2a00d08bcea
7
+ data.tar.gz: 8561bc959fada0966457c3b8f92269cf5413dd7f4a3adda9f85224c14bb3eafd83f192755cf01af5e9ce456f621025c11a1d3a075aecb68325e62f5af390ade8
@@ -5,7 +5,7 @@ module Alchemy
5
5
  private
6
6
 
7
7
  def base_page_scope
8
- Page.all
8
+ ::Alchemy::JsonApi::Page.all
9
9
  end
10
10
 
11
11
  def page_scope
@@ -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, :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,34 @@
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
@@ -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,20 +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
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
- has_many :elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer
22
- has_many :fixed_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer
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
- has_many :all_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
25
- page.all_elements.select { |e| e.public? && !e.trashed? && !e.try(:deprecated?) }
26
- 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
27
30
 
28
- with_options if: ->(_, params) { params[:admin] == true } do
29
- attribute :tag_list
30
- attribute :status
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
@@ -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,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
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Alchemy
3
3
  module JsonApi
4
- VERSION = "0.10.0"
4
+ VERSION = "0.12.2"
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.0
4
+ version: 0.12.2
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-18 00:00:00.000000000 Z
11
+ date: 2021-01-27 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