Module: Spree::Api::V2::Platform::NestedSetRepositionConcern

Included in:
MenuItemsController, TaxonsController
Defined in:
app/controllers/concerns/spree/api/v2/platform/nested_set_reposition_concern.rb

Instance Method Summary collapse

Instance Method Details

#repositionObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/concerns/spree/api/v2/platform/nested_set_reposition_concern.rb', line 6

def reposition
  spree_authorize! :update, resource if spree_current_user.present?

  @new_parent = scope.find(permitted_resource_params[:new_parent_id])
  new_index = permitted_resource_params[:new_position_idx].to_i

  if resource.move_to_child_with_index(@new_parent, new_index)
    # If successful reposition call the custom method for handling success.
    successful_reposition_actions
  elsif resource.errors.any?
    # If there are errors output them to the response
    render_error_payload(resource.errors.full_messages.to_sentence)
  else
    # If the user drops the re-positioned item in the same location it came from
    # we just render the serialized payload, nothing has changed, we don't need to
    # render any errors, or fire any custom success methods.
    render_serialized_payload { serialize_resource(resource) }
  end
end