Some clean up

This commit is contained in:
benji 2015-02-09 17:06:24 +01:00
parent 3676068fd9
commit cf8ec8da6e
31 changed files with 184 additions and 139 deletions

View file

@ -21,7 +21,7 @@ class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(
:nickname, :name, :last_name, :password, :password_confirmation,
:current_password, :avatar
:avatar
) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(

View file

@ -5,10 +5,10 @@ class OrdersController < ApplicationController
@user = User.find(params[:user_id])
@order = @user.orders.build
@products = Product.all
@order_products = @order.order_products
@order_items = @order.order_items
@products.each do |p|
@order_products.build(product: p)
@order_items.build(product: p)
end
end
@ -16,7 +16,7 @@ class OrdersController < ApplicationController
@user = User.find(params[:user_id])
@order = @user.orders.build(order_params)
@products = Product.all
@order_products = @order.order_products
@order_items = @order.order_items
if @order.save
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
@ -57,6 +57,6 @@ class OrdersController < ApplicationController
private
def order_params
params.require(:order).permit(order_products_attributes: [:count, product_attributes: [:id, :price, :stock]])
params.require(:order).permit(order_items_attributes: [:count, product_attributes: [:id, :price, :stock]])
end
end

View file

@ -4,8 +4,8 @@ class UsersController < ApplicationController
def show
@user = User.find_by_id(params[:id]) || current_user
@orders = @user.orders.includes(:products).paginate(page: params[:page])
@products = @user.products.select("products.*", "sum(order_products.count) as count").group(:product_id)
@categories = @user.products.select("products.category", "sum(order_products.count) as count").group(:category)
@products = @user.products.select("products.*", "sum(order_items.count) as count").group(:product_id)
@categories = @user.products.select("products.category", "sum(order_items.count) as count").group(:category)
end
def index

View file

@ -13,27 +13,27 @@ class Order < ActiveRecord::Base
include ActionView::Helpers::TextHelper
after_initialize { self.total_price = 0 }
after_create { self.user.pay(price) }
before_destroy { self.user.pay(-price) }
after_create { self.user.increment!(:balance_cents, -price) }
before_destroy { self.user.increment!(:balance_cents, price) }
belongs_to :user, counter_cache: true
has_many :order_products, dependent: :destroy
has_many :products, through: :order_products
has_many :order_items, dependent: :destroy
has_many :products, through: :order_items
attr_accessor :total_price
validates :user, presence: true
validates :order_products, presence: true, in_stock: true
validates :order_items, presence: true, in_stock: true
accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 }
accepts_nested_attributes_for :order_items, reject_if: proc { |oi| oi[:count].to_i <= 0 }
def price
self.order_products.map{ |op| op.count * op.product.price_cents }.sum
self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
end
def to_sentence
self.order_products.map {
|op| pluralize(op.count, op.product.name)
self.order_items.map {
|oi| pluralize(oi.count, oi.product.name)
}.to_sentence
end
end

View file

@ -1,22 +1,22 @@
# == Schema Information
#
# Table name: order_products
# Table name: order_items
#
# id :integer not null, primary key
# order_id :integer
# product_id :integer
# count :integer default(1)
# order_id :integer not null
# product_id :integer not null
# count :integer default(0)
#
class OrderProduct < ActiveRecord::Base
after_create :remove_from_stock
before_destroy :put_back_in_stock
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
validates :product, presence: true
validates :count, numericality: { greater_than_or_equal_to: 0 }
validates :count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
after_create :remove_from_stock
before_destroy :put_back_in_stock
accepts_nested_attributes_for :product
@ -28,12 +28,10 @@ class OrderProduct < ActiveRecord::Base
private
def remove_from_stock
product.stock -= self.count
product.save
product.increment!(:stock, - self.count)
end
def put_back_in_stock
product.stock += self.count
product.save
product.increment!(:stock, self.count)
end
end

View file

@ -3,20 +3,20 @@
# Table name: products
#
# id :integer not null, primary key
# name :string(255)
# price_cents :integer
# created_at :datetime
# updated_at :datetime
# name :string(255) not null
# price_cents :integer default(0), not null
# category :integer default(0)
# stock :integer default(0), not null
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0)
# created_at :datetime
# updated_at :datetime
#
class Product < ActiveRecord::Base
has_many :order_products
has_many :order_items
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
enum category: %w(food beverages other)
@ -27,12 +27,11 @@ class Product < ActiveRecord::Base
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
def price
(price_cents || 0) / 100.0
self.price_cents / 100.0
end
def price=(value)
if value.is_a? String then value.sub!(',', '.') end
self.price_cents = (value.to_f * 100).to_int
end
end

View file

@ -5,8 +5,16 @@
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# balance_cents :integer default(0), not null
# nickname :string(255)
# admin :boolean
# koelkast :boolean default(FALSE)
# dagschotel_id :integer
# orders_count :integer default(0)
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
@ -16,21 +24,14 @@
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# admin :boolean
# dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# 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
devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable
has_paper_trail only: [:balance, :admin, :orders_count, :koelkast]
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium,
default_url: "http://babeholder.pixoil.com/img/70/70"
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
has_many :orders, -> { includes :products }
has_many :products, through: :orders
@ -39,7 +40,6 @@ class User < ActiveRecord::Base
validates :nickname, presence: true, uniqueness: true
validates :name, presence: true
validates :last_name, presence: true
validates :password, length: { in: 8..128 }, confirmation: true, on: :create
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
scope :members, -> { where koelkast: false }
@ -48,17 +48,28 @@ class User < ActiveRecord::Base
"#{name} #{last_name}"
end
def pay(amount)
write_attribute(:balance, read_attribute(:balance) - amount)
self.save
end
def balance
(read_attribute(:balance) || 0) / 100.0
self.balance_cents / 100.0
end
def balance=(value)
if value.is_a? String then value.sub!(',', '.') end
write_attribute(:balance, (value.to_f * 100).to_int)
self.balance_cents = (value.to_f * 100).to_int
end
# Change URL params for User
def to_param
"#{id} #{nickname}".parameterize
end
# This is needed so Devise doesn't try to validate :email
def email_required?
false
end
def email_changed?
false
end
end

View file

@ -1,7 +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
record.order_items.each do |oi|
record.errors[oi.product.name] = "is not in stock anymore" if oi.count > oi.product.stock
end
end
end

View file

@ -3,7 +3,6 @@
<div class="sign-in">
<%= f_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<%= f.text_field :nickname %>
<%= f.password_field :password %>

View file

@ -0,0 +1,15 @@
<div class="col-md-3 form_products">
<div class="thumbnail ">
<div class="form_row center">
<%= image_tag product.avatar %>
<div class="caption">
<%= content_tag :h3, "#{product.name} - #{euro(product.price)}" %>
<%= f.counter :count, skip_label: true, wrapper_class: "input-group", class: "row_counter" %>
<%= f.fields_for :product do |product| %>
<%= product.hidden_field :price_cents, class: :price %>
<%= product.hidden_field :stock, class: :stock %>
<% end %>
</div>
</div>
</div>
</div>

View file

@ -1,17 +0,0 @@
<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) %></h3>
<p>
<%= f.counter :count, skip_label: true, wrapper_class: "input-group", class: "row_counter" %>
<%= f.fields_for :product do |product| %>
<%= product.hidden_field :price_cents, class: :price %>
<%= product.hidden_field :stock, class: :stock %>
<% end %>
</p>
</div>
</div>
</div>
</div>

View file

@ -1,2 +1,2 @@
<%= order.created_at %>
<%= simple_format(order.order_products.map { |p| pluralize(p.count, p.product.name) }.to_sentence) %>
<%= simple_format(order.to_sentence) %>

View file

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

View file

@ -1,15 +1,15 @@
<h3>Order for <%= @user.nickname %> (<%= euro(@user.balance) %>)</h3>
<div class="row">
<%= form_for @order, builder: FormattedFormBuilder, url: user_orders_path(@user) do |f| %>
<%= f_form_for [@user, @order] do |f| %>
<%= f.error_messages %>
<div class="col-md-12">
<%= f.fields_for :order_products do |op_field| %>
<%= f.fields_for :order_items do |op_field| %>
<%= render op_field.object, f: op_field, product: op_field.object.product %>
<% end %>
</div>
<%= render 'order_products/total_price', f: f %>
<%= render 'order_items/total_price', f: f %>
<% end %>
</div>

View file

@ -1,6 +1,6 @@
<div class="row">
<div class="col-md-6 col-md-offset-3 sign-in">
<%= form_for @product, builder: FormattedFormBuilder, html: { multipart: true } do |f| %>
<%= f_form_for @product, html: { multipart: true } do |f| %>
<%= f.error_messages %>
<%= f.text_field :name %>

View file

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

View file

@ -1,7 +1,10 @@
<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) %>
<% unless user.dagschotel.nil? %>
<%= link_to user_quickpay_path(user) do %>
<%= image_tag user.dagschotel.avatar, class: "img-circle dagschotel" %>
<% end %>
<% end %>
<%= link_to user.name , new_user_order_path(user), class: "btn btn-info",
style: get_color_style(user) %>
<%= 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

@ -227,7 +227,7 @@ Devise.setup do |config|
# config.navigational_formats = ['*/*', :html]
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete
config.sign_out_via = :get
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting

View file

@ -23,6 +23,7 @@ Rails.application.routes.draw do
post 'stock' => 'products#update_stock', as: 'update_stock'
end
end
get 'admins' => 'admins#schulden', as: "admins_schulden"
get 'overview' => 'orders#overview', as: "orders"
end

View file

@ -0,0 +1,5 @@
class ChangeOrderProductToOrderItem < ActiveRecord::Migration
def change
rename_table :order_products, :order_items
end
end

View file

@ -0,0 +1,5 @@
class ChangeBalanceToBalanceCents < ActiveRecord::Migration
def change
rename_column :users, :balance, :balance_cents
end
end

View file

@ -0,0 +1,5 @@
class AddNotNullBalanceUsers < ActiveRecord::Migration
def change
change_column :users, :balance_cents, :integer, default: 0, null: false
end
end

View file

@ -0,0 +1,8 @@
class AddIndexOnKoelkast < ActiveRecord::Migration
def change
add_index :users, :koelkast
add_index :users, :orders_count
change_column :products, :price_cents, :integer, default: 0, null: false
change_column :products, :stock, :integer, default: 0, null: false
end
end

View file

@ -0,0 +1,6 @@
class NotNullFields < ActiveRecord::Migration
def change
change_column :order_items, :product_id, :integer, null: false
change_column :order_items, :order_id, :integer, null: false
end
end

View file

@ -0,0 +1,5 @@
class ProductsNamePresenceTrue < ActiveRecord::Migration
def change
change_column :products, :name, :string, null: false
end
end

View file

@ -11,11 +11,11 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150209113630) do
ActiveRecord::Schema.define(version: 20150209145303) do
create_table "order_products", force: true do |t|
t.integer "order_id"
t.integer "product_id"
create_table "order_items", force: true do |t|
t.integer "order_id", null: false
t.integer "product_id", null: false
t.integer "count", default: 0
end
@ -30,23 +30,31 @@ ActiveRecord::Schema.define(version: 20150209113630) do
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
create_table "products", force: true do |t|
t.string "name"
t.integer "price_cents"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", null: false
t.integer "price_cents", default: 0, null: false
t.integer "category", default: 0
t.integer "stock", default: 0, null: false
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "category", default: 0
t.integer "stock", default: 0
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.string "last_name"
t.integer "balance", default: 0
t.integer "balance_cents", default: 0, null: false
t.string "nickname"
t.boolean "admin"
t.boolean "koelkast", default: false
t.integer "dagschotel_id"
t.integer "orders_count", default: 0
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password", default: "", null: false
@ -56,16 +64,11 @@ ActiveRecord::Schema.define(version: 20150209113630) do
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.boolean "admin"
t.integer "dagschotel_id"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "orders_count", default: 0
t.boolean "koelkast", default: false
end
add_index "users", ["koelkast"], name: "index_users_on_koelkast"
add_index "users", ["orders_count"], name: "index_users_on_orders_count"
create_table "versions", force: true do |t|
t.string "item_type", null: false
t.integer "item_id", null: false

View file

@ -3,16 +3,16 @@
# Table name: products
#
# id :integer not null, primary key
# name :string(255)
# price_cents :integer
# created_at :datetime
# updated_at :datetime
# name :string(255) not null
# price_cents :integer default(0), not null
# category :integer default(0)
# stock :integer default(0), not null
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0)
# created_at :datetime
# updated_at :datetime
#
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -5,8 +5,16 @@
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# balance_cents :integer default(0), not null
# nickname :string(255)
# admin :boolean
# koelkast :boolean default(FALSE)
# dagschotel_id :integer
# orders_count :integer default(0)
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
@ -16,14 +24,6 @@
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# admin :boolean
# dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# 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

@ -3,16 +3,16 @@
# Table name: products
#
# id :integer not null, primary key
# name :string(255)
# price_cents :integer
# created_at :datetime
# updated_at :datetime
# name :string(255) not null
# price_cents :integer default(0), not null
# category :integer default(0)
# stock :integer default(0), not null
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default(0)
# stock :integer default(0)
# created_at :datetime
# updated_at :datetime
#
require 'test_helper'

View file

@ -5,8 +5,16 @@
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# balance_cents :integer default(0), not null
# nickname :string(255)
# admin :boolean
# koelkast :boolean default(FALSE)
# dagschotel_id :integer
# orders_count :integer default(0)
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
@ -16,14 +24,6 @@
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# admin :boolean
# dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# koelkast :boolean default(FALSE)
#
require 'test_helper'