Skip to content

Commit

Permalink
Merge pull request #130 from BrennanAyers/user_orders_cancel
Browse files Browse the repository at this point in the history
User orders cancel
  • Loading branch information
WHomer authored May 25, 2019
2 parents d575564 + dc2086c commit 20ef9b4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ def show
@order = Order.find(params[:id])
@order_items = @order.order_items
end

def destroy
@order = Order.find(params[:id])
@order.update(status: :cancelled)
@order.order_items.each do |order_item|
order_item.update(fulfilled: false)
end
flash[:notice] = "#{@order.id} has been cancelled."

redirect_to profile_orders_path
end
end
17 changes: 17 additions & 0 deletions app/models/order_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ class OrderItem < ApplicationRecord
validates_presence_of :quantity, :price
validates_inclusion_of :fulfilled, in: [true, false]

after_save :update_inventory

def sub_total
quantity * price
end

private

def update_inventory
if saved_change_to_fulfilled?

if fulfilled
new_inventory = item.inventory - quantity
item.update(inventory: new_inventory)
else
new_inventory = item.inventory + quantity
item.update(inventory: new_inventory)
end
end
end
end
6 changes: 6 additions & 0 deletions app/views/orders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@

<p>Number of Items: <%= @order.item_count %></p>
<p>Grand Total: <%= number_to_currency(@order.grand_total, unit: "$") %></p>


<%= button_to "Cancel Order", profile_order_path(@order), method: "delete", disabled: !@order.pending? %>
<% if !@order.pending? %>
<p>You can only cancel orders that are pending!</p>
<% end %>
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
get '/profile', to: 'users#show'
get '/profile/edit', to: 'users#edit'
patch '/profile/edit', to: 'users#update'
get '/profile/orders', to: 'orders#index'
get '/profile/orders/:id', to: 'orders#show', as: :profile_order
scope :profile do
# resources :users, only: [:show, :edit, :update], as: :profile
resources :orders, only: [:show, :index, :destroy], as: :profile_orders
# get '/profile/orders', to: 'orders#index'
# get '/profile/orders/:id', to: 'orders#show'
end


get '/dashboard', to: 'merchants#show'
Expand Down
42 changes: 42 additions & 0 deletions spec/features/users/default/orders_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,47 @@
expect(page).to have_content("Number of Items: #{@order_1.item_count}")
expect(page).to have_content("Grand Total: $#{@order_1.grand_total}")
end

it 'I can cancel the order if it is still pending' do
@order_1.update!(status: :pending)
@item_2.update!(inventory: 3)
@order_item_2.update!(fulfilled: true)
@item_2.reload
@order_item_2.reload
expect(@item_2.inventory).to eq(2)

visit profile_order_path(@order_1)

expect(page).to have_content("Current Status: Pending")
expect(page).to have_button("Cancel Order")

click_button "Cancel Order"

@order_item_1.reload
@order_item_2.reload
@order_item_3.reload
expect(@order_item_1.fulfilled).to be false
expect(@order_item_2.fulfilled).to be false
expect(@order_item_3.fulfilled).to be false

@item_2.reload
expect(@item_2.inventory).to eq(3)

expect(current_path).to eq(profile_orders_path)

expect(page).to have_content("#{@order_1.id} has been cancelled.")

within("#order-#{@order_1.id}") do
expect(page).to have_content("Current Status: Cancelled")
end
end

it 'Should have a disabled cancel button if the order is not pending' do
visit profile_order_path(@order_1)

expect(page).to have_content("Current Status: Shipped")
expect(page).to have_button("Cancel Order", disabled: true)
expect(page).to have_content("You can only cancel orders that are pending!")
end
end
end

0 comments on commit 20ef9b4

Please sign in to comment.