Change some css and refactor some controller actions
This commit is contained in:
parent
3a11861bb0
commit
0c06c4bedb
14 changed files with 115 additions and 55 deletions
|
@ -73,4 +73,29 @@ aside {
|
|||
margin-top: 0px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.orders {
|
||||
margin-top: -10px;
|
||||
line-height: 140%;
|
||||
& tr:nth-child(even){
|
||||
border-bottom: 1px solid #bbb;
|
||||
td {
|
||||
padding-bottom: 15px;
|
||||
}
|
||||
}
|
||||
& tr:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
& tr:nth-child(odd) td {
|
||||
padding-top: 10px;
|
||||
}
|
||||
& td.order_date {
|
||||
font-size: 12px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,29 +4,23 @@ class OrdersController < ApplicationController
|
|||
load_and_authorize_resource
|
||||
|
||||
def new
|
||||
@user = User.find(params[:user_id])
|
||||
redirect_to root_path, flash: { error: "Koelkast can't order things." } if @user.koelkast?
|
||||
redirect_to root_path, flash: { error: "Please don't order stuff for other people" } unless current_user.koelkast? || current_user == @user
|
||||
|
||||
init
|
||||
@order = @user.orders.build
|
||||
|
||||
# products = @user.products.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc")
|
||||
# @order.g_order_items(products)
|
||||
@order.g_order_items(Product.all)
|
||||
@order.g_order_items Product.all
|
||||
end
|
||||
|
||||
def create
|
||||
@user = User.find(params[:user_id])
|
||||
redirect_to root_path, flash: { error: "Koelkast can't order things." } if @user.koelkast?
|
||||
redirect_to root_path, flash: { error: "Please don't order stuff for other people" } unless current_user.koelkast? || current_user == @user
|
||||
|
||||
@order = @user.orders.build(order_params)
|
||||
init
|
||||
@order = @user.orders.build order_params
|
||||
|
||||
if @order.save
|
||||
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
||||
redirect_to root_path
|
||||
else
|
||||
@order.g_order_items(Product.all, order_params)
|
||||
@order.total_price = number_with_precision((@order.price / 100.0), precision: 2)
|
||||
@order.g_order_items Product.all
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
@ -60,6 +54,20 @@ class OrdersController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
def init
|
||||
@user = User.find(params[:user_id])
|
||||
|
||||
if @user.koelkast?
|
||||
flash[:error] = "Koelkast can't order things."
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
unless current_user.koelkast? || current_user == @user
|
||||
flash[:error] = "Please don't order stuff for other people"
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
|
||||
def order_params
|
||||
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
|
||||
end
|
||||
|
|
|
@ -8,9 +8,9 @@ class ProductsController < ApplicationController
|
|||
def create
|
||||
@product = Product.new(product_params)
|
||||
if @product.save
|
||||
redirect_to action: :index
|
||||
redirect_to products_path
|
||||
else
|
||||
render :new
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -27,7 +27,7 @@ class ProductsController < ApplicationController
|
|||
@product = Product.find(params[:id])
|
||||
if @product.update_attributes(product_params)
|
||||
flash[:success] = "Succesfully updated product"
|
||||
redirect_to action: :index
|
||||
redirect_to products_path
|
||||
else
|
||||
render 'edit'
|
||||
end
|
||||
|
@ -36,7 +36,7 @@ class ProductsController < ApplicationController
|
|||
def destroy
|
||||
Product.find(params[:id]).destroy
|
||||
flash[:success] = "Succesfully removed product"
|
||||
redirect_to action: :index
|
||||
redirect_to products_path
|
||||
end
|
||||
|
||||
def stock
|
||||
|
|
|
@ -3,9 +3,21 @@ class UsersController < ApplicationController
|
|||
|
||||
def show
|
||||
@user = User.find_by_id(params[:id]) || current_user
|
||||
@orders = @user.orders.active.includes(:products).order(:created_at).reverse_order.paginate(page: params[:page])
|
||||
@products = @user.products.select("products.*", "sum(order_items.count) as count").where("orders.cancelled = ?", false).group(:product_id).order("count desc")
|
||||
@categories = @user.products.select("products.category", "sum(order_items.count) as count").where("orders.cancelled = ?", false).group(:category)
|
||||
@orders = @user.orders
|
||||
.active
|
||||
.order(:created_at)
|
||||
.reverse_order
|
||||
.paginate(page: params[:page])
|
||||
@products = @user.products
|
||||
.select("products.*", "sum(order_items.count) as count")
|
||||
.where("orders.cancelled = ?", false)
|
||||
.group(:product_id)
|
||||
.order("count")
|
||||
.reverse_order
|
||||
@categories = @user.products
|
||||
.select("products.category", "sum(order_items.count) as count")
|
||||
.where("orders.cancelled = ?", false)
|
||||
.group(:category)
|
||||
end
|
||||
|
||||
def index
|
||||
|
@ -16,17 +28,18 @@ class UsersController < ApplicationController
|
|||
@user = User.find(params[:id])
|
||||
@user.destroy
|
||||
flash[:success] = "Succesfully removed user"
|
||||
redirect_to action: :index
|
||||
redirect_to users_path
|
||||
end
|
||||
|
||||
def dagschotel
|
||||
user = User.find(params[:user_id])
|
||||
user.dagschotel = Product.find(params[:product_id])
|
||||
if user.save
|
||||
|
||||
if user.update_attributes(dagschotel: Product.find(params[:product_id]))
|
||||
flash[:success] = "Succesfully updated dagschotel"
|
||||
else
|
||||
flash[:error] = "Error updating dagschotel"
|
||||
end
|
||||
|
||||
redirect_to edit_user_registration_path(user)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -83,7 +83,7 @@ class FormattedFormBuilder < ActionView::Helpers::FormBuilder
|
|||
if object.errors.any?
|
||||
content_tag :div, class: "panel panel-danger form-errors" do
|
||||
content_tag(:div, class: "panel-body") do
|
||||
error_header + error_messages
|
||||
error_header + error_body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -95,7 +95,7 @@ class FormattedFormBuilder < ActionView::Helpers::FormBuilder
|
|||
end
|
||||
end
|
||||
|
||||
def error_messages
|
||||
def error_body
|
||||
content_tag :ul do
|
||||
object.errors.full_messages.map do |msg|
|
||||
content_tag :li, msg
|
||||
|
|
|
@ -12,8 +12,7 @@
|
|||
class Order < ActiveRecord::Base
|
||||
include ActionView::Helpers::TextHelper
|
||||
|
||||
after_initialize { self.price = 0 }
|
||||
after_create { self.user.decrement!(:balance_cents, price) }
|
||||
after_create { self.user.decrement!(:balance_cents, price_cents) }
|
||||
|
||||
belongs_to :user, counter_cache: true
|
||||
has_many :order_items, dependent: :destroy
|
||||
|
@ -21,24 +20,26 @@ class Order < ActiveRecord::Base
|
|||
|
||||
scope :active, -> { where(cancelled: false) }
|
||||
|
||||
attr_accessor :total_price
|
||||
|
||||
validates :user, presence: true
|
||||
validates :order_items, presence: true, in_stock: true
|
||||
|
||||
accepts_nested_attributes_for :order_items, reject_if: proc { |oi| oi[:count].to_i <= 0 }
|
||||
|
||||
def price
|
||||
def price_cents
|
||||
self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
|
||||
end
|
||||
|
||||
def price
|
||||
self.price_cents / 100.0
|
||||
end
|
||||
|
||||
def price=(_)
|
||||
write_attribute(:price, price)
|
||||
write_attribute(:price_cents, price_cents)
|
||||
end
|
||||
|
||||
def cancel
|
||||
return if self.cancelled
|
||||
user.increment!(:balance_cents, price)
|
||||
user.increment!(:balance_cents, price_cents)
|
||||
User.decrement_counter(:orders_count, user.id)
|
||||
update_attribute(:cancelled, true)
|
||||
end
|
||||
|
@ -49,7 +50,7 @@ class Order < ActiveRecord::Base
|
|||
}.to_sentence
|
||||
end
|
||||
|
||||
def g_order_items(products, params = {})
|
||||
def g_order_items(products)
|
||||
products.each do |p|
|
||||
if (oi = self.order_items.select { |t| t.product == p }).size > 0
|
||||
oi.first.count = [oi.first.product.stock, oi.first.count].min
|
||||
|
|
|
@ -15,8 +15,7 @@ class OrderItem < ActiveRecord::Base
|
|||
validates :product, presence: true
|
||||
validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
|
||||
after_create :remove_from_stock
|
||||
before_destroy :put_back_in_stock
|
||||
after_create :remove_from_stock
|
||||
|
||||
accepts_nested_attributes_for :product
|
||||
|
||||
|
@ -28,10 +27,6 @@ class OrderItem < ActiveRecord::Base
|
|||
private
|
||||
|
||||
def remove_from_stock
|
||||
product.increment!(:stock, - self.count)
|
||||
end
|
||||
|
||||
def put_back_in_stock
|
||||
product.increment!(:stock, self.count)
|
||||
product.decrement!(:stock, count)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,15 +17,16 @@
|
|||
|
||||
class Product < ActiveRecord::Base
|
||||
has_many :order_items
|
||||
has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>" }, default_style: :medium, default_url: "http://lorempixel.com/70/70/"
|
||||
has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>" }, default_style: :medium
|
||||
|
||||
enum category: %w(food beverages other)
|
||||
|
||||
validates :name, presence: true
|
||||
validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||
validates_attachment :avatar, presence: true
|
||||
validates_attachment :avatar,
|
||||
presence: true,
|
||||
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||
|
||||
def price
|
||||
self.price_cents / 100.0
|
||||
|
|
|
@ -31,7 +31,7 @@ class User < ActiveRecord::Base
|
|||
|
||||
has_paper_trail only: [:balance, :admin, :orders_count, :koelkast]
|
||||
|
||||
has_attached_file :avatar, styles: { large: "150x150>", medium: "100x100>" }, default_style: :medium, default_url: "http://lorempixel.com/70/70/"
|
||||
has_attached_file :avatar, styles: { large: "150x150>", medium: "100x100>" }, default_style: :medium
|
||||
|
||||
has_many :orders, -> { includes :products }
|
||||
has_many :products, through: :orders
|
||||
|
@ -40,8 +40,9 @@ class User < ActiveRecord::Base
|
|||
validates :nickname, presence: true, uniqueness: true
|
||||
validates :name, presence: true
|
||||
validates :last_name, presence: true
|
||||
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||
validates_attachment :avatar, presence: true
|
||||
validates_attachment :avatar,
|
||||
presence: true,
|
||||
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||
|
||||
scope :members, -> { where koelkast: false }
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
<%= render "layouts/header" %>
|
||||
<div class="container">
|
||||
<div class="container" >
|
||||
<%= yield %>
|
||||
<%= yield %>
|
||||
</div>
|
||||
<%= render "layouts/footer" %>
|
||||
<%= debug(params) if Rails.env.development? %>
|
||||
|
|
|
@ -1,2 +1,13 @@
|
|||
<%= order.created_at.strftime("%d %b %Y at %H:%M") %>
|
||||
<%= simple_format(order.to_sentence) %>
|
||||
<tr>
|
||||
<td class="order_date">
|
||||
<%= order.created_at.strftime("%d %b %Y at %H:%M") %>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<%= order.to_sentence %>
|
||||
</td>
|
||||
<td>
|
||||
<%= euro(order.price) %>
|
||||
</td>
|
||||
</tr>
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
<%= render partial: "products/product_row", collection: @products, as: :product %>
|
||||
|
||||
<h4>All orders (<%= @user.orders_count %>)</h4>
|
||||
<%= render @orders %>
|
||||
<table class="orders"><%= render @orders %></table>
|
||||
<%= will_paginate @orders %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class ChangePriceToPriceCentsOrders < ActiveRecord::Migration
|
||||
def change
|
||||
rename_column :orders, :price, :price_cents
|
||||
end
|
||||
end
|
10
db/schema.rb
10
db/schema.rb
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20150310080932) do
|
||||
ActiveRecord::Schema.define(version: 20150310150231) do
|
||||
|
||||
create_table "order_items", force: :cascade do |t|
|
||||
t.integer "order_id"
|
||||
|
@ -21,10 +21,10 @@ ActiveRecord::Schema.define(version: 20150310080932) do
|
|||
|
||||
create_table "orders", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "price"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.boolean "cancelled", default: false
|
||||
t.integer "price_cents"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.boolean "cancelled", default: false
|
||||
end
|
||||
|
||||
add_index "orders", ["user_id", "created_at"], name: "index_orders_on_user_id_and_created_at"
|
||||
|
|
Loading…
Reference in a new issue