Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix element re-ordering #2028

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ def publish
def order
@parent_element = Element.find_by(id: params[:parent_element_id])
Element.transaction do
Element.where(id: params.fetch(:element_ids, [])).each.with_index(1) do |element, position|
element.update_columns(
params.fetch(:element_ids, []).each.with_index(1) do |element_id, position|
# We need to set the parent_element_id, because we might have dragged the
# element over from another nestable element
Element.find_by(id: element_id).update_columns(
parent_element_id: params[:parent_element_id],
position: position
position: position,
)
end
# Need to manually touch the parent because Rails does not do it
# with the update_columns above
@parent_element&.touch
end
end
Expand Down
25 changes: 15 additions & 10 deletions spec/controllers/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ module Alchemy
end

describe "#order" do
let(:element_1) { create(:alchemy_element) }
let(:element_2) { create(:alchemy_element, page: page) }
let(:element_3) { create(:alchemy_element, page: page) }
let!(:element_1) { create(:alchemy_element) }
let!(:element_2) { create(:alchemy_element, page: page) }
let!(:element_3) { create(:alchemy_element, page: page) }
let(:element_ids) { [element_1.id, element_3.id, element_2.id] }
let(:page) { element_1.page }

it "sets new position for given element ids" do
post :order, params: { element_ids: element_ids }, xhr: true
expect(Element.all.pluck(:id)).to eq(element_ids)
expect(Element.all.pluck(:id, :position)).to eq([
[element_1.id, 1],
[element_3.id, 2],
[element_2.id, 3],
])
end

context "with missing [:element_ids] param" do
Expand All @@ -65,12 +69,13 @@ module Alchemy
let(:parent) { create(:alchemy_element) }

it "touches the cache key of parent element" do
expect(Element).to receive(:find_by) { parent }
expect(parent).to receive(:touch) { true }
post :order, params: {
element_ids: element_ids,
parent_element_id: parent.id,
}, xhr: true
parent.update_column(:updated_at, 3.days.ago)
expect {
post :order, params: {
element_ids: element_ids,
parent_element_id: parent.id,
}, xhr: true
}.to change { parent.reload.updated_at }
end

it "assigns parent element id to each element" do
Expand Down