Add undo button for quickpay/dagschotel
This commit is contained in:
parent
9e4563fbd1
commit
ca3045fbbf
5 changed files with 22 additions and 4 deletions
|
@ -28,6 +28,17 @@ class OrdersController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
order = Order.find(params[:id])
|
||||
if order.created_at > 5.minutes.ago
|
||||
order.destroy
|
||||
flash[:success] = "Order has been removed."
|
||||
else
|
||||
flash[:error] = "This order has been placed too long ago, it can't be removed. Please contact a sysadmin."
|
||||
end
|
||||
redirect_to koelkast_root_path
|
||||
end
|
||||
|
||||
def overview
|
||||
@users_by_name = User.members.order(:name)
|
||||
@users_by_order = User.members.order(:orders_count).reverse_order
|
||||
|
@ -38,7 +49,7 @@ class OrdersController < ApplicationController
|
|||
order = user.orders.build
|
||||
order.products << user.dagschotel
|
||||
if order.save
|
||||
flash[:success] = "Quick pay succeeded"
|
||||
flash[:success] = "Quick pay succeeded. #{view_context.link_to("Undo", [user, order], method: :delete)}."
|
||||
else
|
||||
flash[:error] = order.errors.full_messages.first
|
||||
end
|
||||
|
|
|
@ -14,9 +14,10 @@ class Order < ActiveRecord::Base
|
|||
|
||||
after_initialize { self.total_price = 0 }
|
||||
after_create { self.user.pay(price) }
|
||||
before_destroy { self.user.pay(-price) }
|
||||
|
||||
belongs_to :user, counter_cache: true
|
||||
has_many :order_products
|
||||
has_many :order_products, dependent: :destroy
|
||||
has_many :products, through: :order_products
|
||||
|
||||
attr_accessor :total_price
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
class OrderProduct < ActiveRecord::Base
|
||||
after_create :remove_from_stock
|
||||
before_destroy :put_back_in_stock
|
||||
|
||||
belongs_to :order
|
||||
belongs_to :product
|
||||
|
@ -30,4 +31,9 @@ class OrderProduct < ActiveRecord::Base
|
|||
product.stock -= self.count
|
||||
product.save
|
||||
end
|
||||
|
||||
def put_back_in_stock
|
||||
product.stock += self.count
|
||||
product.save
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<% if flash[:success] %>
|
||||
<div class="alert alert-success alert-dismissable">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
|
||||
<strong>Success!</strong> <%= flash[:success] %>
|
||||
<strong>Success!</strong> <%= raw flash[:success] %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% if flash[:notice] %>
|
||||
|
|
|
@ -12,7 +12,7 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
resources :users do
|
||||
resources :orders, only: [:new, :create, :index]
|
||||
resources :orders
|
||||
get 'quickpay' => 'orders#quickpay'
|
||||
get 'dagschotel/:product_id' => 'users#dagschotel', as: "dagschotel"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue