Skip to content

Commit

Permalink
Merge pull request #49 from AlchemyCMS/page-for-element
Browse files Browse the repository at this point in the history
Add ancestors relationship to page serializer
  • Loading branch information
tvdeyen authored Apr 27, 2021
2 parents eb98fbd + eee59ec commit 8018b56
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/json_api/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PagesController < JsonApi::PagesController

private

def page_version
def page_version_type
:draft_version
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
Expand All @@ -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

Expand Down
15 changes: 11 additions & 4 deletions app/models/alchemy/json_api/page.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -33,13 +36,17 @@ 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
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
Expand Down
6 changes: 6 additions & 0 deletions app/serializers/alchemy/json_api/page_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/models/alchemy/json_api/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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([])
Expand Down
8 changes: 8 additions & 0 deletions spec/serializers/alchemy/json_api/page_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8018b56

Please sign in to comment.