Skip to content

Commit

Permalink
Merge pull request #2754 from AlchemyCMS/backport/7.1-stable/pr-2752
Browse files Browse the repository at this point in the history
[7.1-stable] Merge pull request #2752 from tvdeyen/fix-copy-elements-order
  • Loading branch information
tvdeyen authored Feb 27, 2024
2 parents 0b0f932 + 5742f97 commit 65fbd58
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/models/alchemy/page/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ module ClassMethods
#
def copy_elements(source, target)
repository = source.draft_version.element_repository
elements = repository.not_nested
page_version = target.draft_version
duplicate_elements(elements.unfixed, repository, page_version) +
duplicate_elements(elements.fixed, repository, page_version)
end

private

def duplicate_elements(elements, repository, page_version)
transaction do
Element.acts_as_list_no_update do
repository.not_nested.each.with_index(1) do |element, position|
elements.each.with_index(1) do |element, position|
Alchemy::DuplicateElement.new(element, repository: repository).call(
page_version_id: target.draft_version.id,
page_version_id: page_version.id,
position: position
)
end
Expand Down
23 changes: 23 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,29 @@ module Alchemy
end
end

describe ".copy_elements" do
let(:page) { create(:alchemy_page) }
let(:page_2) { create(:alchemy_page) }
let!(:element_1) { create(:alchemy_element, page_version: page.draft_version) }
let!(:element_2) { create(:alchemy_element, page_version: page.draft_version) }
let!(:element_3) { create(:alchemy_element, page_version: page.draft_version) }
let!(:fixed_element_1) { create(:alchemy_element, :fixed, page_version: page.draft_version) }
let!(:fixed_element_2) { create(:alchemy_element, :fixed, page_version: page.draft_version) }

subject(:copy_elements) { Page.copy_elements(page, page_2) }

it "should keep original order of fixed and non-fixed elements" do
elements = copy_elements
expect(elements.map(&:position)).to match([
element_1.position,
element_2.position,
element_3.position,
fixed_element_1.position,
fixed_element_2.position
])
end
end

describe ".create" do
context "before/after filter" do
it "should automatically set the title from its name" do
Expand Down

0 comments on commit 65fbd58

Please sign in to comment.