Skip to content

Commit

Permalink
Merge pull request solidusio#457 from bonobos/line_item_remove
Browse files Browse the repository at this point in the history
Add OrderContents#remove_line_item
  • Loading branch information
magnusvk committed Oct 26, 2015
2 parents 71fb1df + ffc15d5 commit 2d5bb31
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 1 addition & 2 deletions api/app/controllers/spree/api/line_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def update

def destroy
@line_item = find_line_item
variant = Spree::Variant.unscoped.find(@line_item.variant_id)
@order.contents.remove(variant, @line_item.quantity)
@order.contents.remove_line_item(@line_item)
respond_with(@line_item, status: 204)
end

Expand Down
5 changes: 5 additions & 0 deletions core/app/models/spree/order_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def remove(variant, quantity = 1, options = {})
after_add_or_remove(line_item, options)
end

def remove_line_item(line_item, options = {})
line_item.destroy!
after_add_or_remove(line_item, options)
end

def update_cart(params)
if order.update_attributes(params)
unless order.completed?
Expand Down
42 changes: 42 additions & 0 deletions core/spec/models/spree/order_contents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,48 @@
end
end

context "#remove_line_item" do
context 'given a shipment' do
it "ensure shipment calls update_amounts instead of order calling ensure_updated_shipments" do
line_item = subject.add(variant, 1)
shipment = create(:shipment)
expect(subject.order).to_not receive(:ensure_updated_shipments)
expect(shipment).to receive(:update_amounts)
subject.remove_line_item(line_item, shipment: shipment)
end
end

context 'not given a shipment' do
it "ensures updated shipments" do
line_item = subject.add(variant, 1)
expect(subject.order).to receive(:ensure_updated_shipments)
subject.remove_line_item(line_item)
end
end

it 'should remove line_item' do
line_item = subject.add(variant, 1)
subject.remove_line_item(line_item)

expect(order.reload.line_items).to_not include(line_item)
end

it "should update order totals" do
expect(order.item_total.to_f).to eq(0.00)
expect(order.total.to_f).to eq(0.00)

line_item = subject.add(variant,2)

expect(order.item_total.to_f).to eq(39.98)
expect(order.total.to_f).to eq(39.98)

subject.remove_line_item(line_item)
expect(order.item_total.to_f).to eq(0.00)
expect(order.total.to_f).to eq(0.00)
end
end


context "update cart" do
let!(:shirt) { subject.add variant, 1 }

Expand Down

0 comments on commit 2d5bb31

Please sign in to comment.