add stock to products and some clean up

This commit is contained in:
benji 2015-01-15 00:39:34 +01:00
parent 49dc9201b6
commit 9e72274629
34 changed files with 123 additions and 114 deletions

View file

@ -9,14 +9,18 @@ ready = ->
increment($(this), -1)
$('.btn-dec').prop("disabled", true)
$('.btn-inc').each((index, button) ->
$(button).prop("disabled", $(button).closest('.form_row').find('.stock').val() == '0')
)
increment = (button, n) ->
# Fix the counter
counter = $(button).closest('.form_row').find('.row_counter')
counter.val(parseInt(counter.val()) + n)
# Enable or disable the dec button
counter.parent().find('.btn-dec').prop("disabled", counter.val() == '0');
# Enable or disable the buttons
counter.parent().find('.btn-dec').prop('disabled', counter.val() == '0');
counter.parent().find('.btn-inc').prop('disabled', counter.val() == counter.parent().find('.stock').val());
# Update the price
oldVal = parseFloat($('#order_total_price').val())

View file

@ -5,8 +5,7 @@ class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
redirect_to root_path
redirect_to root_path, flash: { error: exception.message }
end
def after_sign_in_path_for(resource)
@ -14,7 +13,7 @@ class ApplicationController < ActionController::Base
end
def after_sign_up_path_for(resource)
new_user_session_path
root_path
end
protected
@ -24,9 +23,9 @@ class ApplicationController < ActionController::Base
:nickname, :name, :last_name, :password, :password_confirmation,
:current_password, :avatar
) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(
:password, :password_confirmation, :current_password, :avatar
) }
end
end

View file

@ -19,8 +19,9 @@ class OrdersController < ApplicationController
@order = @user.orders.build(order_params)
@products = Product.all
@order_products = @order.order_products
if @order.save
flash[:success] = order_to_sentence(@order) + " ordered. Enjoy it!"
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
redirect_to root_path
else
render 'new'
@ -33,13 +34,13 @@ class OrdersController < ApplicationController
end
def quickpay
@user = User.find(params[:user_id])
order = @user.orders.build
order.products << @user.dagschotel
user = User.find(params[:user_id])
order = user.orders.build
order.products << user.dagschotel
if order.save
flash[:success] = "Quick pay succeeded"
else
flash[:error] = "Quick pay went wrong ... Sorry!"
flash[:error] = order.errors.full_messages.first
end
redirect_to root_path
end

View file

@ -36,13 +36,13 @@ class ProductsController < ApplicationController
def destroy
Product.find(params[:id]).destroy
flash[:success] = "Succesfully removed product"
redirect_to products_path
redirect_to action: :index
end
private
def product_params
params.require(:product).permit(:name, :price, :avatar, :category)
params.require(:product).permit(:name, :price, :avatar, :category, :stock)
end
end

View file

@ -1,5 +1,5 @@
class UsersController < ApplicationController
load_and_authorize_resource only: [:destroy]
load_and_authorize_resource
def show
@user = User.find_by_id(params[:id]) || current_user
@ -15,7 +15,7 @@ class UsersController < ApplicationController
def destroy
User.find(params[:id]).destroy
flash[:success] = "Succesfully removed user"
redirect_to users_path
redirect_to action: :index
end
def dagschotel

View file

@ -13,11 +13,6 @@ module ApplicationHelper
"#{number_with_precision f, precision: 2}"
end
#tijdelijk voor layout
def koelkast(status)
@koelkast ||= status
end
# Form helpers
def form_errors(object)
render partial: "form_errors", locals: {object: object}

View file

@ -1,9 +1,2 @@
module OrdersHelper
include ActionView::Helpers::TextHelper
def order_to_sentence(order)
order.order_products.map {
|op| pluralize(op.count, op.product.name)
}.to_sentence
end
end

View file

@ -10,8 +10,10 @@
#
class Order < ActiveRecord::Base
include ActionView::Helpers::TextHelper
after_initialize { self.total_price = 0 }
after_create :pay_price
after_create { self.user.pay(price) }
belongs_to :user, counter_cache: true
has_many :order_products
@ -20,21 +22,17 @@ class Order < ActiveRecord::Base
attr_accessor :total_price
validates :user, presence: true
validates :order_products, presence: true
validates :order_products, presence: true, in_stock: true
accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 }
def price
price = 0
order_products.each do |op|
price += op.count * op.product.read_attribute(:price)
end
price
self.order_products.map{ |op| op.count * op.product.price }.sum
end
private
def pay_price
user.pay(price)
end
def to_sentence
self.order_products.map {
|op| pluralize(op.count, op.product.name)
}.to_sentence
end
end

View file

@ -9,6 +9,8 @@
#
class OrderProduct < ActiveRecord::Base
after_create :remove_from_stock
belongs_to :order
belongs_to :product
@ -16,4 +18,11 @@ class OrderProduct < ActiveRecord::Base
validates :count, numericality: { greater_than_or_equal_to: 0 }
accepts_nested_attributes_for :product
private
def remove_from_stock
product.stock -= self.count
product.save
end
end

View file

@ -12,6 +12,7 @@
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0)
#
class Product < ActiveRecord::Base
@ -22,6 +23,7 @@ class Product < ActiveRecord::Base
validates :name, presence: true
validates :price, 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, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
def price

View file

@ -23,12 +23,13 @@
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# koelkast :boolean default(FALSE)
#
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:rememberable, :trackable
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium, default_url: "http://babeholder.pixoil.com/img/70/70"
devise :database_authenticatable, :registerable, :rememberable, :trackable
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium,
default_url: "http://babeholder.pixoil.com/img/70/70"
has_many :orders, -> { includes :products }
has_many :products, through: :orders

View file

@ -0,0 +1,7 @@
class InStockValidator < ActiveModel::Validator
def validate(record)
record.order_products.each do |op|
record.errors[op.product.name] = "is not in stock anymore" if op.count > op.product.stock
end
end
end

View file

@ -1,4 +1,5 @@
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= render 'flash' %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>

View file

@ -15,11 +15,6 @@
</div>
<%= render "layouts/footer" %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>

View file

@ -1,19 +0,0 @@
<div class="col-md-3 form_products">
<div class="thumbnail ">
<div class="form_row center">
<%= image_tag f.object.product.avatar %>
<div class="caption">
<h3><%= f.object.product.name %> - <%= content_tag :span, euro(f.object.product.price), class: "price" %></h3>
<p>
<div class="input-group">
<%= render 'btn_dec', id: f.object.product_id %>
<%= f.text_field :count, class: 'form-control row_counter', value: 0 %>
<%= f.hidden_field :price, value: f.object.product.price, class: :price %>
<%= render 'btn_inc', id: f.object.product_id %>
<%= f.hidden_field :product_id %>
</div>
</p>
</div>
</div>
</div>
</div>

View file

@ -0,0 +1,20 @@
<div class="col-md-3 form_products">
<div class="thumbnail ">
<div class="form_row center">
<%= image_tag product.avatar %>
<div class="caption">
<h3><%= product.name %> - <%= content_tag :span, euro(product.price), class: "price" %></h3>
<p>
<div class="input-group">
<%= render 'btn_dec', id: product.id %>
<%= f.text_field :count, class: 'form-control row_counter', value: 0 %>
<%= f.hidden_field :price, value: product.price, class: :price %>
<%= f.hidden_field :stock, value: product.stock, class: :stock %>
<%= render 'btn_inc', id: product.id %>
<%= f.hidden_field :product_id %>
</div>
</p>
</div>
</div>
</div>
</div>

View file

@ -1,8 +1,10 @@
<%= f.label :total_price %>
<div class="input-group">
<span class="input-group-addon">&euro;</span>
<%= f.number_field :total_price, step: :any, class: 'form-control input-lg' %>
<span class="input-group-btn">
<%= f.submit "Order!", class: "btn btn-primary input-lg" %>
</span>
<div class="col-md-3 form_total">
<%= f.label :total_price %>
<div class="input-group">
<span class="input-group-addon">&euro;</span>
<%= f.number_field :total_price, step: :any, class: 'form-control input-lg' %>
<span class="input-group-btn">
<%= f.submit "Order!", class: "btn btn-primary input-lg" %>
</span>
</div>
</div>

View file

@ -1,6 +1,2 @@
<%= order.created_at %>
<p>
<% products.each do |product| %>
<%= pluralize(product.count, product.name) %>
<% end %>
</p>
<%= simple_format(products.map { |p| pluralize(p.count, p.name) }.to_sentence) %>

View file

@ -0,0 +1,6 @@
<div class="col-md-6">
<h3 class="center" >sort by name</h3>
<% users.each do |user| %>
<%= render 'users/new_order', user: user %>
<% end %>
</div>

View file

@ -1,9 +0,0 @@
<% users.each do |user| %>
<div class="thumbnail overview">
<%= link_to image_tag(user.dagschotel.avatar, class: "img-circle dagschotel"), user_quickpay_path(user) unless user.dagschotel.nil? %>
<%= link_to image_tag(user.avatar , class: "img-circle avatar"), new_user_order_path(user) %>
<%= link_to user.name , new_user_order_path(user), class: "btn btn-info",
style: get_color_style(user) %>
</div>
<% end %>

View file

@ -6,13 +6,10 @@
<div class="col-md-12">
<%= f.fields_for :order_products do |op_field| %>
<%= render 'order_products/form_row', f: op_field %>
<%= render op_field.object, f: op_field, product: op_field.object.product %>
<% end %>
</div>
<div class="col-md-3 form_total">
<%= render 'order_products/total_price', f: f %>
</div>
<% end %>
<%= render 'order_products/total_price', f: f %>
<% end %>
</div>

View file

@ -1,11 +1,4 @@
<%= render partial: 'flash' %>
<div class="col-md-6">
<h3 class="center" >sort by name</h3>
<%= render 'orders/user_list', users: @users_by_name %>
</div>
<div class="col-md-6">
<h3 class="center" >sort by order</h3>
<%= render 'orders/user_list', users: @users_by_order %>
</div>
<%= render 'overview', users: @users_by_name %>
<%= render 'overview', users: @users_by_order %>

View file

@ -1,3 +1 @@
<p>
<%= pluralize(category.count, category.category) %>
</p>
<%= simple_format(pluralize(category.count, category.category)) %>

View file

@ -10,6 +10,9 @@
<%= form_collection_select f, :category, Product.categories.keys, :to_s, :titlecase %>
<%= f.label :stock %>
<%= f.number_field :stock, class: 'form-control form-field' %>
<%= f.label :avatar %>
<%= f.file_field :avatar , class: "form-field" %>

View file

@ -3,10 +3,12 @@
<%= image_tag product.avatar %>
<div class="caption">
<h3><%= product.name %> - <%= euro(product.price) %></h3>
<p>
<%= link_to "Edit", edit_product_path(product), class: "btn btn-default" %>
<%= link_to "Delete", product_path(product), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} %>
</p>
<% if current_user.admin? %>
<p>
<%= link_to "Edit", edit_product_path(product), class: "btn btn-default" %>
<%= link_to "Delete", product_path(product), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} %>
</p>
<% end %>
</div>
</div>
</div>

View file

@ -1,3 +1 @@
<p>
<%= pluralize(product.count, product.name) %>
</p>
<%= simple_format(pluralize(product.count, product.name)) %>

View file

@ -0,0 +1,7 @@
<div class="thumbnail overview">
<%= link_to image_tag(user.dagschotel.avatar, class: "img-circle dagschotel"), user_quickpay_path(user) unless user.dagschotel.nil? %>
<%= link_to image_tag(user.avatar , class: "img-circle avatar"), new_user_order_path(user) %>
<%= link_to user.name , new_user_order_path(user), class: "btn btn-info",
style: get_color_style(user) %>
</div>

View file

@ -1,6 +1,6 @@
<div class="col-md-3">
<div class="thumbnail monopoly">
<h3 class="header" style="background-color: #<%=get_color(user)%>;" ><%= user.full_name %></h3>
<h3 class="header" style="background-color: #<%= get_color(user)%>;" ><%= user.full_name %></h3>
<div class="caption">
<%= image_tag(user.avatar , class: "img-circle avatar") %>
<p><strong>Name:</strong> <%= user.name %></p>

View file

@ -0,0 +1,5 @@
class AddStockToProducts < ActiveRecord::Migration
def change
add_column :products, :stock, :integer, default: 0
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: 20141217063222) do
ActiveRecord::Schema.define(version: 20150113155744) do
create_table "order_products", force: true do |t|
t.integer "order_id"
@ -39,6 +39,7 @@ ActiveRecord::Schema.define(version: 20141217063222) do
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "category", default: 0
t.integer "stock", default: 0
end
create_table "users", force: true do |t|

View file

@ -12,6 +12,7 @@
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0)
#
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -23,6 +23,7 @@
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# koelkast :boolean default(FALSE)
#
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -12,6 +12,7 @@
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0)
#
require 'test_helper'

View file

@ -23,6 +23,7 @@
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# koelkast :boolean default(FALSE)
#
require 'test_helper'