Allow destroying of orders

This commit is contained in:
benji 2015-09-14 17:52:25 +02:00
parent 251d3b69b7
commit de1436f5c7
19 changed files with 37 additions and 59 deletions

View file

@ -3,7 +3,7 @@ class OrdersController < ApplicationController
include ApplicationHelper
load_resource :user
load_and_authorize_resource :order, through: :user, shallow: true, except: :destroy
load_and_authorize_resource :order, through: :user, shallow: true
def new
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
@ -20,22 +20,17 @@ class OrdersController < ApplicationController
end
def destroy
order = Order.unscoped.find(params[:id])
authorize! :destroy, order
if order.cancel
flash[:success] = "Order has been removed."
else
flash[:error] = "Something went wrong. Perhaps this order was already cancelled, or it has been place too long ago."
end
@order.destroy
flash[:success] = "Order has been removed."
redirect_to root_path
end
def overview
@users = User.members.publik.order(:uid)
@users = User.members.publik.order(:name)
end
def quickpay
user = User.find(params[:user_id])
user = User.find(params[:id])
order = user.orders.build
order.order_items << OrderItem.new(count: 1, product: user.dagschotel, order: order)
if order.save

View file

@ -2,7 +2,7 @@ require 'digest/md5'
module ApplicationHelper
def get_color(user)
Digest::MD5.hexdigest(user.nickname)[0..5]
Digest::MD5.hexdigest(user.name)[0..5]
end
def get_color_style(user)
@ -28,7 +28,7 @@ module ApplicationHelper
postData = Net::HTTP.post_form(URI.parse('https://slack.com/api/users.list'), {'token'=>Rails.application.secrets.access_token})
data = JSON.parse(postData.body)
if data["ok"]
slackmember = data["members"].select{ |m| m["profile"]["email"] == user.uid + "@zeus.ugent.be" }.first
slackmember = data["members"].select{ |m| m["profile"]["email"] == user.name + "@zeus.ugent.be" }.first
if slackmember
Webhook.new(channel: "@" + slackmember["name"], username: "Tab").ping(message)

View file

@ -7,7 +7,6 @@
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# transaction_id :integer
#
@ -23,24 +22,12 @@ class Order < ActiveRecord::Base
before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } }
after_create { Delayed::Job.enqueue TabApiJob.new(id) }
default_scope -> { where(cancelled: false) }
validates :user, presence: true
validates :order_items, presence: true, in_stock: true
validates :price_cents, presence: true
accepts_nested_attributes_for :order_items
def cancel
return false if cancelled || created_at < 5.minutes.ago
User.decrement_counter(:orders_count, user.id)
update_attribute(:cancelled, true)
self.order_items.each(&:cancel)
tab_api_cancelled
true
end
def to_sentence
self.order_items.map {
|oi| pluralize(oi.count, oi.product.name)

View file

@ -15,6 +15,7 @@ class OrderItem < ActiveRecord::Base
validates :product, presence: true
validates :count, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
before_destroy :put_back_in_stock
after_create :remove_from_stock
accepts_nested_attributes_for :product
@ -24,13 +25,13 @@ class OrderItem < ActiveRecord::Base
super
end
def cancel
self.product.increment!(:stock, self.count)
end
private
def remove_from_stock
product.decrement!(:stock, count)
end
def put_back_in_stock
product.increment!(:stock, self.count)
end
end

View file

@ -20,7 +20,7 @@
# orders_count :integer default("0")
# koelkast :boolean default("f")
# provider :string
# uid :string
# name :string
# encrypted_password :string default(""), not null
# private :boolean default("f")
#
@ -43,17 +43,13 @@ class User < ActiveRecord::Base
scope :publik, -> { where private: false }
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
where(provider: auth.provider, name: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.uid
user.avatar = Identicon.data_url_for auth.uid
end
end
def nickname
self.uid
end
def debt
42.15
end
@ -61,6 +57,6 @@ class User < ActiveRecord::Base
# Change URL params for User
def to_param
"#{id} #{nickname}".parameterize
"#{id} #{name}".parameterize
end
end

View file

@ -1,3 +0,0 @@
<%- headers = ['nickname', 'schulden'] -%>
<%= CSV.generate_line headers -%>
<% @users.each do |user| %><%= CSV.generate_line([user.nickname, user.debt]) %><% end %>

View file

@ -3,7 +3,7 @@
<div class="sign-in">
<%= f_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<%= f.text_field :uid %>
<%= f.text_field :name %>
<%= f.password_field :password %>
<% if devise_mapping.rememberable? %>

View file

@ -48,7 +48,7 @@
</li>
<% end %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Logged in as <%= current_user.nickname %> <b class="caret"></b></a>
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Logged in as <%= current_user.name %> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><%= link_to "Edit avatar", edit_user_path(current_user) %></li>
</ul>

View file

@ -1,4 +1,4 @@
<h3>Order for <%= @user.nickname %> (Huidige schuld: <%= euro(@user.debt) %>)</h3>
<h3>Order for <%= @user.name %> (Huidige schuld: <%= euro(@user.debt) %>)</h3>
<div class="row">
<%= f_form_for [@user, @order] do |f| %>

View file

@ -6,9 +6,9 @@
<%- if controller_name == 'users' %>
<div class="product_dagschotel">
<% if current_user.dagschotel != product %>
<%= link_to "Make dagschotel", user_dagschotel_path(current_user, product), class: "btn btn-default" %>
<%= link_to "Make dagschotel", dagschotel_user_path(current_user, product), class: "btn btn-default" %>
<% else %>
<%= link_to "Huidige dagschotel", user_dagschotel_path(current_user, product), class: "btn btn-success", disabled: true %>
<%= link_to "Huidige dagschotel", dagschotel_user_path(current_user, product), class: "btn btn-success", disabled: true %>
<% end %>
</div>
<% end -%>

View file

@ -1,10 +1,10 @@
<div class="col-md-2 overviewthumbnail">
<% unless user.dagschotel.nil? %>
<%= link_to user_quickpay_path(user) do %>
<%= link_to quickpay_user_path(user) do %>
<%= image_tag user.dagschotel.avatar(:dagschotel), class: "img-circle dagschotel" %>
<% end %>
<% end %>
<%= link_to image_tag(user.avatar(:large) , class: "img-circle avatar"), new_user_order_path(user) %>
<%= link_to user.nickname , new_user_order_path(user), class: "btn btn-info", style: get_color_style(user) %>
<%= link_to user.name , new_user_order_path(user), class: "btn btn-info", style: get_color_style(user) %>
</div>

View file

@ -1,7 +1,7 @@
<div class="col-sm-3">
<div>
<h2>
<%= @user.nickname %>
<%= @user.name %>
<% if can? :edit, @user %>
<%= link_to content_tag(:small, content_tag(:span, "", class: "glyphicon glyphicon-cog")), edit_user_path(@user) %>
<% end %>

View file

@ -1,7 +1,7 @@
<tr>
<td><%= user.id %></td>
<td><%= image_tag user.avatar(:small) %></td>
<td><%= user.nickname %></td>
<td><%= user.name %></td>
<td><%= euro(user.debt) %></td>
<% if current_user.admin? %>
<td>

View file

@ -0,0 +1,6 @@
class RemoveCancelledFromOrders < ActiveRecord::Migration
def change
remove_column :orders, :cancelled, :boolean
rename_column :users, :uid, :name
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150914081940) do
ActiveRecord::Schema.define(version: 20150914153143) do
create_table "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false
@ -38,13 +38,11 @@ ActiveRecord::Schema.define(version: 20150914081940) do
create_table "orders", force: :cascade do |t|
t.integer "user_id"
t.integer "price_cents"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "cancelled", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "transaction_id"
end
add_index "orders", ["cancelled"], name: "index_orders_on_cancelled"
add_index "orders", ["created_at"], name: "index_orders_on_created_at"
add_index "orders", ["user_id", "created_at"], name: "index_orders_on_user_id_and_created_at"
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
@ -82,7 +80,7 @@ ActiveRecord::Schema.define(version: 20150914081940) do
t.integer "orders_count", default: 0
t.boolean "koelkast", default: false
t.string "provider"
t.string "uid"
t.string "name"
t.string "encrypted_password", default: "", null: false
t.boolean "private", default: false
end

View file

@ -7,7 +7,6 @@
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# transaction_id :integer
#

View file

@ -20,7 +20,7 @@
# orders_count :integer default("0")
# koelkast :boolean default("f")
# provider :string
# uid :string
# name :string
# encrypted_password :string default(""), not null
# private :boolean default("f")
#

View file

@ -7,7 +7,6 @@
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
# transaction_id :integer
#

View file

@ -20,7 +20,7 @@
# orders_count :integer default("0")
# koelkast :boolean default("f")
# provider :string
# uid :string
# name :string
# encrypted_password :string default(""), not null
# private :boolean default("f")
#