Files
openfoodnetwork/app/controllers/line_items_controller.rb
Matt-Yorkley 9abf6cdcdf Rename expensive method Order#update_distribution_charge!
This method is named "update distribution charge". What this method actually does is delete all of the fee adjustments on an order and all it's line items, then recreate them all from scratch. We call this from lots of different places all the time, and it's incredibly expensive. It even gets called from inside of transactions being run inside callbacks. Renaming it hopefully will add a bit of clarity.

This needs to be a lot more granular!
2021-01-29 21:52:28 +00:00

50 lines
1.1 KiB
Ruby

class LineItemsController < BaseController
respond_to :json
before_action :load_line_item, only: :destroy
def bought
respond_with bought_items, each_serializer: Api::LineItemSerializer
end
def destroy
authorize! :destroy, @line_item
destroy_with_lock @line_item
respond_with(@line_item)
end
private
def load_line_item
@line_item = Spree::LineItem.find_by(id: params[:id])
not_found unless @line_item
end
# List all items the user already ordered in the current order cycle
def bought_items
return [] unless current_order_cycle && spree_current_user && current_distributor
current_order_cycle.items_bought_by_user(spree_current_user, current_distributor)
end
def unauthorized
status = spree_current_user ? 403 : 401
render(nothing: true, status: status) && return
end
def not_found
render(nothing: true, status: :not_found) && return
end
def destroy_with_lock(item)
order = item.order
order.with_lock do
item.destroy
order.update_shipping_fees!
order.update_payment_fees!
order.recreate_all_fees!
order.create_tax_charge!
end
end
end