Remove debt from tab and refactor some code
This commit is contained in:
parent
df91936ff8
commit
b5017bff9a
3
app/assets/javascripts/sessions.js.coffee
Normal file
3
app/assets/javascripts/sessions.js.coffee
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Place all the behaviors and hooks related to the matching controller here.
|
||||||
|
# All this logic will automatically be available in application.js.
|
||||||
|
# You can use CoffeeScript in this file: http://coffeescript.org/
|
|
@ -37,16 +37,25 @@
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.debt {
|
.user_info .actions {
|
||||||
padding: 12px 0;
|
a, span {
|
||||||
text-align: center;
|
display: inline-block;
|
||||||
font-family: monospace;
|
padding: 12px 0;
|
||||||
font-size: 16px;
|
text-align: center;
|
||||||
width: 60%;
|
font-family: monospace;
|
||||||
margin: auto;
|
font-size: 16px;
|
||||||
margin-top: 10px;
|
width: 60%;
|
||||||
background-color: #FF7F00;
|
margin: auto;
|
||||||
color: white;
|
margin-top: 10px;
|
||||||
|
background-color: #FF7F00;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
background: #64a724;
|
||||||
|
background: -moz-linear-gradient(top, #64a724 0%, #579727 50%, #58982a 51%, #498c25 100%);
|
||||||
|
background: -webkit-gradient(linear, left top, left bottom, from(#64a724), to(#498c25), color-stop(0.4, #579727), color-stop(0.5, #58982a), color-stop(.9, #498c25), color-stop(0.9, #498c25));
|
||||||
|
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#64a724', endColorstr='#498c25', GradientType=0 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.stats{
|
.stats{
|
||||||
|
|
3
app/assets/stylesheets/sessions.css.scss
Normal file
3
app/assets/stylesheets/sessions.css.scss
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// Place all the styles related to the sessions controller here.
|
||||||
|
// They will automatically be included in application.css.
|
||||||
|
// You can use Sass (SCSS) here: http://sass-lang.com/
|
|
@ -1,7 +1,6 @@
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
# Prevent CSRF attacks by raising an exception.
|
|
||||||
# For APIs, you may want to use :null_session instead.
|
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
|
check_authorization
|
||||||
|
|
||||||
rescue_from CanCan::AccessDenied do |exception|
|
rescue_from CanCan::AccessDenied do |exception|
|
||||||
redirect_to root_path, flash: { error: exception.message }
|
redirect_to root_path, flash: { error: exception.message }
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
class CallbacksController < Devise::OmniauthCallbacksController
|
class CallbacksController < Devise::OmniauthCallbacksController
|
||||||
|
skip_authorization_check
|
||||||
|
|
||||||
def zeuswpi
|
def zeuswpi
|
||||||
@user = User.from_omniauth(request.env["omniauth.auth"])
|
@user = User.from_omniauth(request.env["omniauth.auth"])
|
||||||
sign_in_and_redirect @user
|
sign_in_and_redirect @user
|
||||||
|
|
|
@ -2,10 +2,11 @@ class OrdersController < ApplicationController
|
||||||
include ActionView::Helpers::NumberHelper
|
include ActionView::Helpers::NumberHelper
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
|
|
||||||
load_and_authorize_resource
|
load_and_authorize_resource :user
|
||||||
|
load_and_authorize_resource :order, through: :user
|
||||||
|
|
||||||
def new
|
def new
|
||||||
init
|
@user = User.find(params[:user_id])
|
||||||
@order = @user.orders.build
|
@order = @user.orders.build
|
||||||
|
|
||||||
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
|
products = (@user.products.for_sale.select("products.*", "sum(order_items.count) as count").group(:product_id).order("count desc") | Product.for_sale)
|
||||||
|
@ -13,13 +14,11 @@ class OrdersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
init
|
@user = User.find(params[:user_id])
|
||||||
@order = @user.orders.build order_params
|
@order = @user.orders.build order_params
|
||||||
|
|
||||||
if @order.save
|
if @order.save
|
||||||
message = "#{@order.to_sentence} ordered. Enjoy it!"
|
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
||||||
flash[:success] = message
|
|
||||||
slack_notification(@user, message)
|
|
||||||
redirect_to root_path
|
redirect_to root_path
|
||||||
else
|
else
|
||||||
@order.g_order_items Product.for_sale
|
@order.g_order_items Product.for_sale
|
||||||
|
@ -56,25 +55,6 @@ class OrdersController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def init
|
|
||||||
@user = User.find(params[:user_id])
|
|
||||||
|
|
||||||
if @user.koelkast?
|
|
||||||
flash[:error] = "Koelkast can't order things."
|
|
||||||
redirect_to root_path
|
|
||||||
end
|
|
||||||
|
|
||||||
if @user.private && current_user != @user
|
|
||||||
flash[:error] = "You can't order stuff for this person."
|
|
||||||
redirect_to root_path
|
|
||||||
end
|
|
||||||
|
|
||||||
unless current_user.koelkast? || current_user.admin? || current_user == @user
|
|
||||||
flash[:error] = "Please don't order stuff for other people"
|
|
||||||
redirect_to root_path
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def order_params
|
def order_params
|
||||||
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
|
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,7 @@ class ProductsController < ApplicationController
|
||||||
def create
|
def create
|
||||||
@product = Product.new(product_params)
|
@product = Product.new(product_params)
|
||||||
if @product.save
|
if @product.save
|
||||||
|
flash[:success] = "Product created!"
|
||||||
redirect_to products_path
|
redirect_to products_path
|
||||||
else
|
else
|
||||||
render 'new'
|
render 'new'
|
||||||
|
@ -19,9 +20,8 @@ class ProductsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@products = Product.all
|
@products = Product.all
|
||||||
@categories = Product.categories
|
@categories = Product.categories
|
||||||
if current_user.admin?
|
|
||||||
render 'products_list/listview'
|
render 'products_list/listview' if current_user.admin?
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
@ -35,12 +35,6 @@ class ProductsController < ApplicationController
|
||||||
respond_with @product
|
respond_with @product
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
|
||||||
Product.find(params[:id]).destroy
|
|
||||||
flash[:success] = "Succesfully removed product"
|
|
||||||
redirect_to products_path
|
|
||||||
end
|
|
||||||
|
|
||||||
def stock
|
def stock
|
||||||
@products = Product.all
|
@products = Product.all
|
||||||
end
|
end
|
||||||
|
|
3
app/controllers/sessions_controller.rb
Normal file
3
app/controllers/sessions_controller.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class SessionsController < Devise::SessionsController
|
||||||
|
skip_authorization_check
|
||||||
|
end
|
|
@ -4,19 +4,16 @@ class UsersController < ApplicationController
|
||||||
def show
|
def show
|
||||||
@user = User.find_by_id(params[:id]) || current_user
|
@user = User.find_by_id(params[:id]) || current_user
|
||||||
@orders = @user.orders
|
@orders = @user.orders
|
||||||
.active
|
|
||||||
.order(:created_at)
|
.order(:created_at)
|
||||||
.reverse_order
|
.reverse_order
|
||||||
.paginate(page: params[:page])
|
.paginate(page: params[:page])
|
||||||
@products = @user.products
|
@products = @user.products
|
||||||
.select("products.*", "sum(order_items.count) as count")
|
.select("products.*", "sum(order_items.count) as count")
|
||||||
.where("orders.cancelled = ?", false)
|
|
||||||
.group(:product_id)
|
.group(:product_id)
|
||||||
.order("count")
|
.order("count")
|
||||||
.reverse_order
|
.reverse_order
|
||||||
@categories = @user.products
|
@categories = @user.products
|
||||||
.select("products.category", "sum(order_items.count) as count")
|
.select("products.category", "sum(order_items.count) as count")
|
||||||
.where("orders.cancelled = ?", false)
|
|
||||||
.group(:category)
|
.group(:category)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,14 +36,15 @@ class UsersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@user = User.find(params[:id])
|
user = User.find(params[:id])
|
||||||
@user.destroy
|
user.destroy
|
||||||
flash[:success] = "Succesfully removed user"
|
flash[:success] = "Succesfully removed user"
|
||||||
redirect_to users_path
|
redirect_to users_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit_dagschotel
|
def edit_dagschotel
|
||||||
@user = User.find(params[:user_id])
|
@user = User.find(params[:user_id])
|
||||||
|
authorize! :update_dagschotel, @user
|
||||||
@dagschotel = @user.dagschotel
|
@dagschotel = @user.dagschotel
|
||||||
|
|
||||||
@products = Product.for_sale
|
@products = Product.for_sale
|
||||||
|
@ -54,31 +52,19 @@ class UsersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_dagschotel
|
def update_dagschotel
|
||||||
@user = User.find(params[:user_id])
|
user = User.find(params[:user_id])
|
||||||
@user.dagschotel = Product.find(params[:product_id])
|
authorize! :update_dagschotel, user
|
||||||
|
|
||||||
@products = Product.for_sale
|
user.dagschotel = Product.find(params[:product_id])
|
||||||
@categories = Product.categories
|
user.save
|
||||||
|
|
||||||
if @user.save
|
|
||||||
flash[:success] = "Succesfully updated dagschotel"
|
|
||||||
redirect_to @user
|
|
||||||
else
|
|
||||||
flash[:error] = "Error updating dagschotel"
|
|
||||||
@dagschotel = @user.reload.dagschotel
|
|
||||||
render 'edit_dagschotel'
|
|
||||||
end
|
|
||||||
|
|
||||||
|
flash[:success] = "Succesfully updated dagschotel"
|
||||||
|
redirect_to user
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def init
|
|
||||||
@user = User.find(params[:user_id])
|
|
||||||
redirect_to root_path, error: "You are not authorized to access this page." unless @user == current_user || current_user.admin?
|
|
||||||
end
|
|
||||||
|
|
||||||
def user_params
|
def user_params
|
||||||
params.fetch(:user, {}).permit(:avatar, :private)
|
params.require(:user).permit(:avatar, :private)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
class WelcomeController < ApplicationController
|
class WelcomeController < ApplicationController
|
||||||
|
skip_authorization_check
|
||||||
|
|
||||||
def index
|
def index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
2
app/helpers/sessions_helper.rb
Normal file
2
app/helpers/sessions_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
module SessionsHelper
|
||||||
|
end
|
|
@ -3,17 +3,15 @@ class Ability
|
||||||
|
|
||||||
def initialize(user)
|
def initialize(user)
|
||||||
user ||= User.new # guest user (not logged in)
|
user ||= User.new # guest user (not logged in)
|
||||||
|
|
||||||
if user.admin?
|
if user.admin?
|
||||||
can :manage, :all
|
can :manage, :all
|
||||||
can :schulden, :admins
|
|
||||||
elsif user.koelkast?
|
elsif user.koelkast?
|
||||||
can :manage, Order
|
can :manage, Order
|
||||||
elsif user[:id]
|
elsif user[:id]
|
||||||
can :read, :all
|
can :read, :all
|
||||||
can :update, User
|
can :manage, User, id: user.id
|
||||||
can :edit_dagschotel, User
|
can :manage, Order, user: user
|
||||||
can :update_dagschotel, User
|
|
||||||
can :create, Order
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,13 +13,11 @@
|
||||||
class Order < ActiveRecord::Base
|
class Order < ActiveRecord::Base
|
||||||
include ActionView::Helpers::TextHelper
|
include ActionView::Helpers::TextHelper
|
||||||
|
|
||||||
after_create { self.user.increment!(:debt_cents, price_cents) }
|
|
||||||
|
|
||||||
belongs_to :user, counter_cache: true
|
belongs_to :user, counter_cache: true
|
||||||
has_many :order_items, dependent: :destroy
|
has_many :order_items, dependent: :destroy
|
||||||
has_many :products, through: :order_items
|
has_many :products, through: :order_items
|
||||||
|
|
||||||
scope :active, -> { where(cancelled: false) }
|
default_scope -> { where(cancelled: false) }
|
||||||
|
|
||||||
validates :user, presence: true
|
validates :user, presence: true
|
||||||
validates :order_items, presence: true, in_stock: true
|
validates :order_items, presence: true, in_stock: true
|
||||||
|
@ -40,7 +38,6 @@ class Order < ActiveRecord::Base
|
||||||
|
|
||||||
def cancel
|
def cancel
|
||||||
return if self.cancelled
|
return if self.cancelled
|
||||||
user.decrement!(:debt_cents, price_cents)
|
|
||||||
User.decrement_counter(:orders_count, user.id)
|
User.decrement_counter(:orders_count, user.id)
|
||||||
update_attribute(:cancelled, true)
|
update_attribute(:cancelled, true)
|
||||||
self.order_items.each(&:cancel)
|
self.order_items.each(&:cancel)
|
||||||
|
|
|
@ -31,7 +31,6 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
has_paper_trail
|
has_paper_trail
|
||||||
has_attached_file :avatar, styles: { large: "150x150>", medium: "100x100>", small: "40x40>" }, default_style: :medium
|
has_attached_file :avatar, styles: { large: "150x150>", medium: "100x100>", small: "40x40>" }, default_style: :medium
|
||||||
|
|
||||||
has_many :orders, -> { includes :products }
|
has_many :orders, -> { includes :products }
|
||||||
has_many :products, through: :orders
|
has_many :products, through: :orders
|
||||||
belongs_to :dagschotel, class_name: 'Product'
|
belongs_to :dagschotel, class_name: 'Product'
|
||||||
|
@ -56,12 +55,7 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def debt
|
def debt
|
||||||
self.debt_cents / 100.0
|
42.15
|
||||||
end
|
|
||||||
|
|
||||||
def debt=(value)
|
|
||||||
if value.is_a? String then value.sub!(',', '.') end
|
|
||||||
self.debt_cents = (value.to_f * 100).to_int
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Change URL params for User
|
# Change URL params for User
|
||||||
|
|
|
@ -1,27 +1,36 @@
|
||||||
<%= render partial: 'flash' %>
|
<%= render partial: 'flash' %>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="user_info">
|
<div class="user_info">
|
||||||
<% if current_user == @user %>
|
<% if can? :edit, @user %>
|
||||||
<h5>
|
<h5>
|
||||||
<%= link_to "[Edit dagschotel]" , user_edit_dagschotel_path(@user) %>
|
<%= link_to "[Edit dagschotel]" , user_edit_dagschotel_path(@user) %>
|
||||||
<%= link_to "[Edit profile]" , edit_user_path(@user) %>
|
<%= link_to "[Edit profile]" , edit_user_path(@user) %>
|
||||||
</h5>
|
</h5>
|
||||||
<% end %>
|
<% end %>
|
||||||
<h2><%= @user.nickname %></h2>
|
<h2><%= @user.nickname %></h2>
|
||||||
<%= button_to "PLACE ORDER!", new_user_order_path(@user), method: :get if current_user == @user %>
|
<div class="actions">
|
||||||
<div class="debt">DEBT: <%= euro(@user.debt) %></div>
|
<%= link_to "PLACE ORDER!", new_user_order_path(@user) if current_user == @user %>
|
||||||
|
<span>DEBT: <%= euro(@user.debt) %></span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% if @orders.any? %>
|
<% if @orders.any? %>
|
||||||
<div class="stats">
|
<div class="stats">
|
||||||
<h4>Total products</h4>
|
<h4>Total products</h4>
|
||||||
Total: <br/><ul><li><%= @categories.map{|c| pluralize(c.count, c.category)}.join(", ")%></li></ul>
|
Total:
|
||||||
<br/>
|
|
||||||
Specifics:<br/>
|
|
||||||
<ul>
|
<ul>
|
||||||
<%= @products.map{ |p| content_tag(:li, pluralize(p.count, p.name)) }.join("\n").html_safe %>
|
<li>
|
||||||
|
<%= @categories.map{|c| pluralize(c.count, c.category)}.to_sentence %>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
Specifics:
|
||||||
|
<%= content_tag :ul do %>
|
||||||
|
<% @products.each do |p| %>
|
||||||
|
<%= content_tag :li, pluralize(p.count, p.name) %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<h4>All orders (<%= @user.orders_count %>)</h4>
|
<h4>All orders (<%= @user.orders_count %>)</h4>
|
||||||
<table class="orders"><%= render @orders %></table>
|
<table class="orders"><%= render @orders %></table>
|
||||||
<%= will_paginate @orders %>
|
<%= will_paginate @orders %>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
Rails.application.routes.draw do
|
Rails.application.routes.draw do
|
||||||
devise_for :users, controllers: {
|
devise_for :users, controllers: {
|
||||||
omniauth_callbacks: "callbacks"
|
omniauth_callbacks: "callbacks",
|
||||||
|
sessions: "sessions"
|
||||||
}
|
}
|
||||||
|
|
||||||
devise_scope :user do
|
devise_scope :user do
|
||||||
|
@ -24,8 +25,6 @@ Rails.application.routes.draw do
|
||||||
get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel'
|
get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel'
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :user_avatar
|
|
||||||
|
|
||||||
resources :products do
|
resources :products do
|
||||||
collection do
|
collection do
|
||||||
get 'stock' => 'products#stock', as: 'stock'
|
get 'stock' => 'products#stock', as: 'stock'
|
||||||
|
|
5
db/migrate/20150827152754_remove_debt_from_users.rb
Normal file
5
db/migrate/20150827152754_remove_debt_from_users.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class RemoveDebtFromUsers < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
remove_column :users, :debt, :int
|
||||||
|
end
|
||||||
|
end
|
6
db/migrate/20150827155036_add_some_indexes_to_tables.rb
Normal file
6
db/migrate/20150827155036_add_some_indexes_to_tables.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
class AddSomeIndexesToTables < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_index :orders, :created_at
|
||||||
|
add_index :orders, :cancelled
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20150824142843) do
|
ActiveRecord::Schema.define(version: 20150827155036) do
|
||||||
|
|
||||||
create_table "order_items", force: :cascade do |t|
|
create_table "order_items", force: :cascade do |t|
|
||||||
t.integer "order_id"
|
t.integer "order_id"
|
||||||
|
@ -27,6 +27,8 @@ ActiveRecord::Schema.define(version: 20150824142843) do
|
||||||
t.boolean "cancelled", default: false
|
t.boolean "cancelled", default: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_index "orders", ["cancelled"], name: "index_orders_on_cancelled"
|
||||||
|
add_index "orders", ["created_at"], name: "index_orders_on_created_at"
|
||||||
add_index "orders", ["user_id", "created_at"], name: "index_orders_on_user_id_and_created_at"
|
add_index "orders", ["user_id", "created_at"], name: "index_orders_on_user_id_and_created_at"
|
||||||
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
|
add_index "orders", ["user_id"], name: "index_orders_on_user_id"
|
||||||
|
|
||||||
|
|
7
test/controllers/sessions_controller_test.rb
Normal file
7
test/controllers/sessions_controller_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class SessionsControllerTest < ActionController::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
Loading…
Reference in a new issue