Do some extra shit

This commit is contained in:
benji 2015-03-10 11:37:48 +01:00
parent a5dbe99d27
commit 01c777baa1
14 changed files with 126 additions and 91 deletions

View file

@ -22,7 +22,7 @@ disIfNec = (row) ->
recalculate = () ->
value = ($(row).find('.row_counter').val() * $(row).find('.price').val() for row in $('.form_row')).reduce (a, b) -> a+b
$('#order_total_price').val((value / 100.0).toFixed(2))
$('#order_price').val((value / 100.0).toFixed(2))
increment = (button, n) ->
row = $(button).closest('.form_row')

View file

@ -34,7 +34,7 @@ class OrdersController < ApplicationController
def destroy
order = Order.find(params[:id])
if order.created_at > 5.minutes.ago
order.destroy
order.cancel
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."
@ -61,6 +61,6 @@ class OrdersController < ApplicationController
private
def order_params
params.require(:order).permit(order_items_attributes: [:count, product_attributes: [:id]])
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
end
end

View file

@ -3,9 +3,9 @@ class UsersController < ApplicationController
def show
@user = User.find_by_id(params[:id]) || current_user
@orders = @user.orders.includes(:products).order(:created_at).reverse_order.paginate(page: params[:page])
@products = @user.products.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc")
@categories = @user.products.select("products.category", "sum(order_items.count) as count").group(:category)
@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)
end
def index

View file

@ -28,7 +28,12 @@ class FormattedFormBuilder < ActionView::Helpers::FormBuilder
def price_field(name, options = {})
options[:min] ||= 0
options[:value] ||= number_with_precision(object[name], precision: 2)
# if object.is_a?(ActiveRecord::Base)
# options[:value] ||= object[name]
if object.respond_to?(name)
options[:value] ||= object.send name
end
options[:value] = number_with_precision(options[:value], precision: 2)
form_group_builder(name, options) do
number_field_without_format(name, options)

View file

@ -11,6 +11,7 @@ class Ability
elsif user[:id]
can :read, :all
can :update, User
can :dagschotel, User
can :create, Order
end
end

View file

@ -12,14 +12,15 @@
class Order < ActiveRecord::Base
include ActionView::Helpers::TextHelper
after_initialize { self.total_price = 0 }
after_create { self.user.increment!(:balance_cents, -price) }
before_destroy { self.user.increment!(:balance_cents, price) }
after_initialize { self.price = 0 }
after_create { self.user.decrement!(:balance_cents, price) }
belongs_to :user, counter_cache: true
has_many :order_items, dependent: :destroy
has_many :products, through: :order_items
scope :active, -> { where(cancelled: false) }
attr_accessor :total_price
validates :user, presence: true
@ -31,6 +32,17 @@ class Order < ActiveRecord::Base
self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
end
def price=(_)
write_attribute(:price, price)
end
def cancel
return if self.cancelled
user.increment!(:balance_cents, price)
User.decrement_counter(:orders_count, user.id)
update_attribute(:cancelled, true)
end
def to_sentence
self.order_items.map {
|oi| pluralize(oi.count, oi.product.name)

View file

@ -2,7 +2,7 @@
<%= f.label :total_price %>
<div class="input-group">
<span class="input-group-addon">&euro;</span>
<%= f.number_field :total_price, skip_label: true, step: :any, class: 'form-control big-form-field' %>
<%= f.number_field :price, skip_label: true, step: :any, class: 'form-control big-form-field' %>
<span class="input-group-btn">
<%= f.submit "Order!", class: "btn btn-primary big-form-button" %>
</span>

View file

@ -10,6 +10,6 @@
<% end %>
</div>
<%= render 'order_items/total_price', f: f %>
<%= render 'orders/price', f: f %>
<% end %>
</div>

View file

@ -7,10 +7,10 @@
<div class="balance">BALANCE: <%= euro(@user.balance) %></div>
</div>
<% if @user.orders.any? %>
<% if @orders.any? %>
<div class="stats">
<h4>Total products</h4>
Total: <br/><ul><li><%=@categories.map{|c| pluralize(c.count, c.category)}.join(", ")%></li></ul>
Total: <br/><ul><li><%= @categories.map{|c| pluralize(c.count, c.category)}.join(", ")%></li></ul>
<br/>
Specifics:<br/>
<%= render partial: "products/product_row", collection: @products, as: :product %>

View file

@ -0,0 +1,5 @@
class AddCancelledBooleanToOrders < ActiveRecord::Migration
def change
add_column :orders, :cancelled, :boolean, default: false
end
end

View file

@ -0,0 +1,5 @@
class ChangeCostToPriceInOrders < ActiveRecord::Migration
def change
rename_column :orders, :cost, :price
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: 20150210062809) do
ActiveRecord::Schema.define(version: 20150310080932) do
create_table "order_items", force: :cascade do |t|
t.integer "order_id"
@ -21,9 +21,10 @@ ActiveRecord::Schema.define(version: 20150210062809) do
create_table "orders", force: :cascade do |t|
t.integer "user_id"
t.integer "cost"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "price"
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"

View file

@ -6,76 +6,3 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
DEFAULT_PASSWORD = "password"
products = [
{
name: 'Fanta',
price: 0.6,
category: 'beverages',
stock: 25,
avatar: File.new('public/seeds/products/fanta.jpg', 'r')
},
{
name: 'Club Mate',
price: 1.2,
category: 'beverages',
stock: 25,
avatar: File.new('public/seeds/products/club_mate.jpg', 'r')
},
{
name: 'Kinder Bueno',
price: 0.6,
category: 'food',
stock: 15,
avatar: File.new('public/seeds/products/bueno.jpg', 'r')
}
]
products.each do |attr|
Product.create name: attr[:name], price: attr[:price], category: attr[:category], stock: attr[:stock], avatar: attr[:avatar]
end
users = [
{
nickname: 'admin',
name: 'A.',
last_name: 'Admin',
avatar: File.new('public/seeds/users/admin.jpg', 'r'),
admin: true
},
{
nickname: 'koelkast',
name: 'K.',
last_name: 'Koelkast',
avatar: File.new('public/seeds/users/admin.jpg', 'r'),
koelkast: true
},
{
nickname: 'benji',
name: 'Benjamin',
last_name: 'Cousaert',
avatar: File.new('public/seeds/users/benji.jpg', 'r'),
dagschotel: Product.first
},
{
nickname: 'don',
name: 'Lorin',
last_name: 'Werthen',
avatar: File.new('public/seeds/users/don.jpg', 'r')
},
{
nickname: 'silox',
name: 'Tom',
last_name: 'Naessens',
avatar: File.new('public/seeds/users/silox.jpg', 'r')
}
]
users.each do |attr|
User.create nickname: attr[:nickname], name: attr[:name], last_name: attr[:last_name], avatar: attr[:avatar], dagschotel: attr[:dagschotel], password: DEFAULT_PASSWORD, password_confirmation: DEFAULT_PASSWORD, admin: attr[:admin] || false, koelkast: attr[:koelkast] || false
end
50.times do |i|
User.create nickname: "TestUser#{i}", name: "Test", last_name: "User", avatar: users[0][:avatar], password: DEFAULT_PASSWORD, password_confirmation: DEFAULT_PASSWORD
end

79
lib/tasks/db.rake Normal file
View file

@ -0,0 +1,79 @@
namespace :db do
desc "Fill the database with some sample data."
task generatedata: :environment do
DEFAULT_PASSWORD = "password"
products = [
{
name: 'Fanta',
price: 0.6,
category: 'beverages',
stock: 25,
avatar: File.new('public/seeds/products/fanta.jpg', 'r')
},
{
name: 'Club Mate',
price: 1.2,
category: 'beverages',
stock: 25,
avatar: File.new('public/seeds/products/club_mate.jpg', 'r')
},
{
name: 'Kinder Bueno',
price: 0.6,
category: 'food',
stock: 15,
avatar: File.new('public/seeds/products/bueno.jpg', 'r')
}
]
products.each do |attr|
Product.create name: attr[:name], price: attr[:price], category: attr[:category], stock: attr[:stock], avatar: attr[:avatar]
end
users = [
{
nickname: 'admin',
name: 'A.',
last_name: 'Admin',
avatar: File.new('public/seeds/users/admin.jpg', 'r'),
admin: true
},
{
nickname: 'koelkast',
name: 'K.',
last_name: 'Koelkast',
avatar: File.new('public/seeds/users/admin.jpg', 'r'),
koelkast: true
},
{
nickname: 'benji',
name: 'Benjamin',
last_name: 'Cousaert',
avatar: File.new('public/seeds/users/benji.jpg', 'r'),
dagschotel: Product.first
},
{
nickname: 'don',
name: 'Lorin',
last_name: 'Werthen',
avatar: File.new('public/seeds/users/don.jpg', 'r')
},
{
nickname: 'silox',
name: 'Tom',
last_name: 'Naessens',
avatar: File.new('public/seeds/users/silox.jpg', 'r')
}
]
users.each do |attr|
User.create nickname: attr[:nickname], name: attr[:name], last_name: attr[:last_name], avatar: attr[:avatar], dagschotel: attr[:dagschotel], password: DEFAULT_PASSWORD, password_confirmation: DEFAULT_PASSWORD, admin: attr[:admin] || false, koelkast: attr[:koelkast] || false
end
50.times do |i|
User.create nickname: "TestUser#{i}", name: "Test", last_name: "User", avatar: users[0][:avatar], password: DEFAULT_PASSWORD, password_confirmation: DEFAULT_PASSWORD
end
end
end