From ae4b0c3a608074cdc314352f303fd9526f17c7e3 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 27 Apr 2021 12:30:23 +0200 Subject: [PATCH 1/2] Rename and expose Page#page_version_type The former named argument was misleading as it did not reflected the page version, but a symbol of the type of version we want to use from the Alchemy::Page. As we want to expose the page_version_type, so we can use it and pass on to new instances we needed to clarify. --- .../alchemy/json_api/admin/pages_controller.rb | 2 +- app/controllers/alchemy/json_api/pages_controller.rb | 10 +++++----- app/models/alchemy/json_api/page.rb | 11 +++++++---- spec/models/alchemy/json_api/page_spec.rb | 4 ++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/controllers/alchemy/json_api/admin/pages_controller.rb b/app/controllers/alchemy/json_api/admin/pages_controller.rb index 52409bd..10c05e5 100644 --- a/app/controllers/alchemy/json_api/admin/pages_controller.rb +++ b/app/controllers/alchemy/json_api/admin/pages_controller.rb @@ -7,7 +7,7 @@ class PagesController < JsonApi::PagesController private - def page_version + def page_version_type :draft_version end end diff --git a/app/controllers/alchemy/json_api/pages_controller.rb b/app/controllers/alchemy/json_api/pages_controller.rb index 1902d75..8e6653a 100644 --- a/app/controllers/alchemy/json_api/pages_controller.rb +++ b/app/controllers/alchemy/json_api/pages_controller.rb @@ -57,7 +57,7 @@ def page_scope_with_includes :legacy_urls, { language: { nodes: [:parent, :children, { page: { language: { site: :languages } } }] } }, { - page_version => { + page_version_type => { elements: [ :nested_elements, { contents: { essence: :ingredient_association } }, @@ -68,20 +68,20 @@ def page_scope_with_includes ) end - def page_version + def page_version_type :public_version end def api_page(page) - Alchemy::JsonApi::Page.new(page, page_version: page_version) + Alchemy::JsonApi::Page.new(page, page_version_type: page_version_type) end def base_page_scope # cancancan is not able to merge our complex AR scopes for logged in users if can?(:edit_content, ::Alchemy::Page) - Alchemy::Page.all.joins(page_version) + Alchemy::Page.all.joins(page_version_type) else - Alchemy::Page.published.joins(page_version) + Alchemy::Page.published.joins(page_version_type) end end diff --git a/app/models/alchemy/json_api/page.rb b/app/models/alchemy/json_api/page.rb index c4434f9..210f71c 100644 --- a/app/models/alchemy/json_api/page.rb +++ b/app/models/alchemy/json_api/page.rb @@ -1,8 +1,11 @@ module Alchemy module JsonApi class Page < SimpleDelegator - def initialize(page, page_version: :public_version) - @page_version = page.public_send(page_version) + attr_reader :page_version_type, :page_version + + def initialize(page, page_version_type: :public_version) + @page_version_type = page_version_type + @page_version = page.public_send(page_version_type) super(page) end @@ -36,10 +39,10 @@ def fixed_element_ids private def element_repository - return Alchemy::ElementsRepository.none unless @page_version + return Alchemy::ElementsRepository.none unless page_version # Need to use select here, otherwise rails would not eager load the elements correctly - Alchemy::ElementsRepository.new(@page_version.elements.select(&:public)) + Alchemy::ElementsRepository.new(page_version.elements.select(&:public)) end end end diff --git a/spec/models/alchemy/json_api/page_spec.rb b/spec/models/alchemy/json_api/page_spec.rb index 4179974..0cabd5e 100644 --- a/spec/models/alchemy/json_api/page_spec.rb +++ b/spec/models/alchemy/json_api/page_spec.rb @@ -28,7 +28,7 @@ end context "with draft_version passed as page_version" do - let(:json_api_page) { described_class.new(page, page_version: :draft_version) } + let(:json_api_page) { described_class.new(page, page_version_type: :draft_version) } let!(:element_4) { FactoryBot.create(:alchemy_element, page_version: page.draft_version) } it "contains elements only from draft version" do @@ -39,7 +39,7 @@ context "with page_version not present" do let(:page) { FactoryBot.create(:alchemy_page) } - let(:json_api_page) { described_class.new(page, page_version: :public_version) } + let(:json_api_page) { described_class.new(page, page_version_type: :public_version) } it "contains elements only from draft version" do is_expected.to match([]) From eee59ec42838454da1eba78d457ffc6a6031b3ce Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 27 Apr 2021 12:40:32 +0200 Subject: [PATCH 2/2] Add ancestors relationship to page serializer Useful if you want to serialize the page categorizations in a breadcrumb for instance. --- app/models/alchemy/json_api/page.rb | 4 ++++ app/serializers/alchemy/json_api/page_serializer.rb | 6 ++++++ spec/serializers/alchemy/json_api/page_serializer_spec.rb | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/app/models/alchemy/json_api/page.rb b/app/models/alchemy/json_api/page.rb index 210f71c..3e3eb77 100644 --- a/app/models/alchemy/json_api/page.rb +++ b/app/models/alchemy/json_api/page.rb @@ -36,6 +36,10 @@ def fixed_element_ids @_fixed_element_ids ||= fixed_elements.map(&:id) end + def ancestor_ids + @_ancestor_ids ||= ancestors.map(&:id) + end + private def element_repository diff --git a/app/serializers/alchemy/json_api/page_serializer.rb b/app/serializers/alchemy/json_api/page_serializer.rb index 7eac5da..a47c6c0 100644 --- a/app/serializers/alchemy/json_api/page_serializer.rb +++ b/app/serializers/alchemy/json_api/page_serializer.rb @@ -24,6 +24,12 @@ class PageSerializer belongs_to :language, record_type: :language, serializer: ::Alchemy::JsonApi::LanguageSerializer + has_many :ancestors, record_type: :page, serializer: self do |page| + page.ancestors.map do |ancestor| + Alchemy::JsonApi::Page.new(ancestor, page_version_type: page.page_version_type) + end + end + # All public elements of this page regardless of if they are fixed or nested. # Used for eager loading and should be used as the +include+ parameter of your query has_many :all_elements, record_type: :element, serializer: ELEMENT_SERIALIZER diff --git a/spec/serializers/alchemy/json_api/page_serializer_spec.rb b/spec/serializers/alchemy/json_api/page_serializer_spec.rb index f4cb7cf..ad857ee 100644 --- a/spec/serializers/alchemy/json_api/page_serializer_spec.rb +++ b/spec/serializers/alchemy/json_api/page_serializer_spec.rb @@ -66,5 +66,13 @@ expect(subject[:language]).to eq(data: { id: page.language_id.to_s, type: :language }) end end + + it "has ancestors relationship" do + expect(subject[:ancestors]).to eq( + data: [ + { id: page.parent_id.to_s, type: :page }, + ], + ) + end end end