Skip to content

Commit

Permalink
Merge pull request #3581 from hefan/master
Browse files Browse the repository at this point in the history
Backend: more robust update_positions for resource controller
  • Loading branch information
aldesantis authored Apr 18, 2020
2 parents 874d54f + 6700480 commit ff2b06a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create
def update_positions
ActiveRecord::Base.transaction do
params[:positions].each do |id, index|
model_class.find(id).set_list_position(index)
model_class.find_by(id: id)&.set_list_position(index)
end
end

Expand Down
12 changes: 12 additions & 0 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,17 @@ def check_destroy_constraints
it 'updates the position of widget 2' do
expect { subject }.to change { widget_2.reload.position }.from(2).to(1)
end

context 'passing a not persisted item' do
subject do
post :update_positions, params: { id: widget_1.to_param,
positions: { widget_1.id => '2', widget_2.id => '1', 'widget' => '3' }, format: 'js' }
end

it 'only updates the position of persisted attributes' do
subject
expect(Widget.all.order('position')).to eq [widget_2, widget_1]
end
end
end
end

0 comments on commit ff2b06a

Please sign in to comment.