Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User orders cancel #130

Merged
merged 12 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: [email protected]? %>
<% if [email protected]? %>
<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