diff --git a/app/assets/stylesheets/users.css.scss b/app/assets/stylesheets/users.css.scss index 45b8735..17ae72c 100644 --- a/app/assets/stylesheets/users.css.scss +++ b/app/assets/stylesheets/users.css.scss @@ -73,4 +73,29 @@ aside { margin-top: 0px; } } -} \ No newline at end of file +} + +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; + } +} diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 1df7505..9bb5f19 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -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 diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 798956d..9c30afe 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 84336bf..7ef41af 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/form_builders/formatted_form_builder.rb b/app/form_builders/formatted_form_builder.rb index 73c37d4..0c415e0 100644 --- a/app/form_builders/formatted_form_builder.rb +++ b/app/form_builders/formatted_form_builder.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index 619c09b..4cec0a3 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/order_item.rb b/app/models/order_item.rb index 130b736..3820fed 100644 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -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 diff --git a/app/models/product.rb b/app/models/product.rb index 2e4d031..0fd4b29 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index d48a642..595ae6d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 } diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5a0c969..fbd0101 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -15,7 +15,7 @@ <%= render "layouts/header" %>
- <%= yield %> + <%= yield %>
<%= render "layouts/footer" %> <%= debug(params) if Rails.env.development? %> diff --git a/app/views/orders/_order.html.erb b/app/views/orders/_order.html.erb index 532c2db..aef0f4a 100644 --- a/app/views/orders/_order.html.erb +++ b/app/views/orders/_order.html.erb @@ -1,2 +1,13 @@ -<%= order.created_at.strftime("%d %b %Y at %H:%M") %> -<%= simple_format(order.to_sentence) %> + + + <%= order.created_at.strftime("%d %b %Y at %H:%M") %> + + + + + <%= order.to_sentence %> + + + <%= euro(order.price) %> + + diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index d85f831..333f6ff 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -16,7 +16,7 @@ <%= render partial: "products/product_row", collection: @products, as: :product %>

All orders (<%= @user.orders_count %>)

- <%= render @orders %> + <%= render @orders %>
<%= will_paginate @orders %>
<% end %> diff --git a/db/migrate/20150310150231_change_price_to_price_cents_orders.rb b/db/migrate/20150310150231_change_price_to_price_cents_orders.rb new file mode 100644 index 0000000..99a3576 --- /dev/null +++ b/db/migrate/20150310150231_change_price_to_price_cents_orders.rb @@ -0,0 +1,5 @@ +class ChangePriceToPriceCentsOrders < ActiveRecord::Migration + def change + rename_column :orders, :price, :price_cents + end +end diff --git a/db/schema.rb b/db/schema.rb index 98dc99b..238794e 100644 --- a/db/schema.rb +++ b/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"