Skip to content

Commit

Permalink
Do not change page_id on reorder elements
Browse files Browse the repository at this point in the history
Reordering elements does not need to change the elements page id anymore
since we removed the trash.
  • Loading branch information
tvdeyen committed Feb 11, 2021
1 parent c8d4b70 commit c9b1c68
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
11 changes: 4 additions & 7 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ def publish
def order
@parent_element = Element.find_by(id: params[:parent_element_id])
Element.transaction do
params.fetch(:element_ids, []).each_with_index do |element_id, idx|
# Ensure to set page_id and parent_element_id to the current
# because of trashed elements could still have old values
Element.where(id: element_id).update_all(
page_id: params[:page_id],
Element.where(id: params.fetch(:element_ids, [])).each.with_index(1) do |element, position|
element.update_columns(
parent_element_id: params[:parent_element_id],
position: idx + 1,
position: position
)
end
@parent_element.try!(:touch)
@parent_element&.touch
end
end

Expand Down
8 changes: 2 additions & 6 deletions spec/controllers/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ module Alchemy
let(:page) { element_1.page }

it "sets new position for given element ids" do
post :order, params: {page_id: page.id, element_ids: element_ids}, xhr: true
post :order, params: { element_ids: element_ids }, xhr: true
expect(Element.all.pluck(:id)).to eq(element_ids)
end

context "with missing [:element_ids] param" do
it "does not raise any error and silently rejects to order" do
expect {
post :order, params: {page_id: page.id}, xhr: true
}.to_not raise_error
expect { post :order, xhr: true }.to_not raise_error
end
end

Expand All @@ -70,15 +68,13 @@ module Alchemy
expect(Element).to receive(:find_by) { parent }
expect(parent).to receive(:touch) { true }
post :order, params: {
page_id: page.id,
element_ids: element_ids,
parent_element_id: parent.id,
}, xhr: true
end

it "assigns parent element id to each element" do
post :order, params: {
page_id: page.id,
element_ids: element_ids,
parent_element_id: parent.id,
}, xhr: true
Expand Down

0 comments on commit c9b1c68

Please sign in to comment.