alchemy-json_api 0.9.0 → 0.12.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: 60e3517753943623b96dc3b70e5f6fe1ae0d9ffceaeeac3045935e4f49ac0c3f
4
- data.tar.gz: 2988b73369eff64ce7c99e09ca4ff0bdc2a0fb3789965d761c9264ff96b9ba48
3
+ metadata.gz: ec8c154e4f6ac5ba72d6d96b39a60475acf9b0d411d3a4e2d34c81fc271d1489
4
+ data.tar.gz: c0cc273e02b2038e5462428fa1f7d0960be37ccf8225abd76af7343ca4f76ec0
5
5
  SHA512:
6
- metadata.gz: a71f11fc71138caa5a2d2337e8545a602f9ed6fd96ab7cad65b6768ecb1f321e445a16ffab7d1fbfe99cca71270286e86f38d64f4d2cbaaec31472de32a86f81
7
- data.tar.gz: d5d3e534e20c9ec196050873d7c0331f16a4a86c8a173f9f8e37ca9c1b3782e2a56740bdbcf1eb846529b37a67f81889f7b8a246f0aa4a8046d42fcd039f4d18
6
+ metadata.gz: 2f0ebd08816a2d97347555eaf114735b429ec7c1e2592f9c1ff0b652be9f0799c782489987eac5d3142d5f5246ac6f02375460d62931ef4cfcca4f8a3e9ad809
7
+ data.tar.gz: '08c7a5e5570c995b3495844bcceb9a145173cb3e224fce0051a5b672eae408aee58850e1b612fb25051c0896c571365481daabe28d7e4766b62742e1b0363e34'
@@ -4,8 +4,12 @@ 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
+
7
11
  def page_scope
8
- base_page_scope.layoutpages
12
+ page_scope_with_includes.layoutpages
9
13
  end
10
14
  end
11
15
  end
@@ -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
 
@@ -42,19 +44,22 @@ module Alchemy
42
44
  end
43
45
 
44
46
  def page_scope
45
- base_page_scope.contentpages
47
+ page_scope_with_includes.contentpages
48
+ end
49
+
50
+ def page_scope_with_includes
51
+ base_page_scope.
52
+ with_language(Language.current).
53
+ preload(language: { nodes: [:parent, :page, :children] }, all_elements: { contents: { essence: :ingredient_association } })
46
54
  end
47
55
 
48
56
  def base_page_scope
49
57
  # cancancan is not able to merge our complex AR scopes for logged in users
50
- if can?(:edit_content, Page)
51
- pages = Page.all
58
+ if can?(:edit_content, ::Alchemy::Page)
59
+ ::Alchemy::JsonApi::Page.all
52
60
  else
53
- pages = Page.published
61
+ ::Alchemy::JsonApi::Page.published
54
62
  end
55
- pages.
56
- with_language(Language.current).
57
- preload(language: {nodes: [:parent, :page]}, all_elements: [:parent_element, :nested_elements, { contents: { essence: :ingredient_association } }])
58
63
  end
59
64
 
60
65
  def jsonapi_serializer_class(_resource, _is_collection)
@@ -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
- 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? }
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,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.9.0"
4
+ VERSION = "0.12.0"
5
5
  end
6
6
  end
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.9.0
4
+ version: 0.12.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: 2020-11-30 00:00:00.000000000 Z
11
+ date: 2021-01-25 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