Skip to content

Commit adac074

Browse files
committed
fix: Filter nested elements by visibility when publishing pages
When publishing a page, only visible top-level elements were being filtered. Nested elements were copied regardless of their public status, which meant hidden nested elements would appear on the published version. This adds a publishable_only option to DuplicateElement that filters nested elements through the repository's visible method, ensuring only public nested elements are copied to the published version. Signed-off-by: Thomas von Deyen <vondeyen@blish.cloud>
1 parent 8988cbe commit adac074

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

app/models/alchemy/page/publisher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def publish!(public_on:)
3030
ActiveRecord::Base.no_touching do
3131
Element.acts_as_list_no_update do
3232
repository.visible.not_nested.each.with_index(1) do |element, position|
33-
Alchemy::DuplicateElement.new(element, repository: repository).call(
33+
Alchemy::DuplicateElement.new(element, repository: repository, publishable_only: true).call(
3434
page_version_id: version.id,
3535
position: position
3636
)

app/services/alchemy/duplicate_element.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ class DuplicateElement
1313
"updater_id"
1414
].freeze
1515

16-
attr_reader :source_element, :repository
16+
attr_reader :source_element, :repository, :publishable_only
1717

18-
def initialize(source_element, repository: source_element.page_version.element_repository)
18+
def initialize(source_element, repository: source_element.page_version.element_repository, publishable_only: false)
1919
@source_element = source_element
2020
@repository = repository
21+
@publishable_only = publishable_only
2122
end
2223

2324
def call(differences = {})
@@ -35,9 +36,10 @@ def call(differences = {})
3536
new_element.save!
3637

3738
nested_elements = repository.children_of(source_element)
39+
nested_elements = nested_elements.visible if publishable_only
3840
Element.acts_as_list_no_update do
3941
nested_elements.each.with_index(1) do |nested_element, position|
40-
self.class.new(nested_element, repository: repository).call(
42+
self.class.new(nested_element, repository: repository, publishable_only: publishable_only).call(
4143
parent_element: new_element,
4244
page_version: new_element.page_version,
4345
position: position

spec/models/alchemy/page/publisher_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@
124124
end
125125
end
126126

127+
context "with nested elements" do
128+
let(:page) { create(:alchemy_page) }
129+
130+
let!(:parent_element) do
131+
create(:alchemy_element, page_version: page.draft_version, public: true)
132+
end
133+
134+
let!(:visible_nested) do
135+
create(:alchemy_element, parent_element: parent_element, page_version: page.draft_version, public: true)
136+
end
137+
138+
let!(:hidden_nested) do
139+
create(:alchemy_element, parent_element: parent_element, page_version: page.draft_version, public: false)
140+
end
141+
142+
it "copies only visible nested elements to public version" do
143+
publish
144+
published_parent = page.reload.public_version.elements.first
145+
expect(published_parent.all_nested_elements).to all(be_public)
146+
end
147+
end
148+
127149
context "in parallel" do
128150
before do
129151
# another publisher - instance created a mutex entry and locked the page

spec/services/alchemy/duplicate_element_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,25 @@
7070
end
7171
end
7272
end
73+
74+
context "with publishable_only option" do
75+
let(:element) { create(:alchemy_element) }
76+
77+
let!(:visible_nested) do
78+
create(:alchemy_element, parent_element: element, page_version: element.page_version, public: true)
79+
end
80+
81+
let!(:hidden_nested) do
82+
create(:alchemy_element, parent_element: element, page_version: element.page_version, public: false)
83+
end
84+
85+
subject do
86+
described_class.new(element, publishable_only: true).call(differences)
87+
end
88+
89+
it "only copies visible nested elements" do
90+
expect(subject.all_nested_elements).to all(be_public)
91+
end
92+
end
7393
end
7494
end

0 commit comments

Comments
 (0)