Add db/seeds and fix two minor bugs
|
@ -1,8 +1,7 @@
|
||||||
class UsersController < ApplicationController
|
class UsersController < ApplicationController
|
||||||
load_and_authorize_resource
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@user = User.find_by_id(params[:id]) || current_user
|
@user = User.find_by_id(params[:id]) || current_user
|
||||||
|
authorize! :read, @user
|
||||||
@orders = @user.orders.includes(:products).paginate(page: params[:page])
|
@orders = @user.orders.includes(:products).paginate(page: params[:page])
|
||||||
@products = @user.products.select("products.*", "sum(order_items.count) as count").group(:product_id)
|
@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)
|
@categories = @user.products.select("products.category", "sum(order_items.count) as count").group(:category)
|
||||||
|
@ -10,10 +9,13 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@users = User.members
|
@users = User.members
|
||||||
|
authorize! :read, @users
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
User.find(params[:id]).destroy
|
@user = User.find(params[:id])
|
||||||
|
authorize! :destroy, @users
|
||||||
|
@user.destroy
|
||||||
flash[:success] = "Succesfully removed user"
|
flash[:success] = "Succesfully removed user"
|
||||||
redirect_to action: :index
|
redirect_to action: :index
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,8 +8,9 @@ class Ability
|
||||||
can :schulden, :admins
|
can :schulden, :admins
|
||||||
elsif user.koelkast?
|
elsif user.koelkast?
|
||||||
can :manage, Order
|
can :manage, Order
|
||||||
else
|
elsif user[:id]
|
||||||
can :read, :all
|
can :read, :all
|
||||||
|
can :update, User
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,8 +38,6 @@ class User < ActiveRecord::Base
|
||||||
belongs_to :dagschotel, class_name: 'Product'
|
belongs_to :dagschotel, class_name: 'Product'
|
||||||
|
|
||||||
validates :nickname, presence: true, uniqueness: true
|
validates :nickname, presence: true, uniqueness: true
|
||||||
validates :name, presence: true
|
|
||||||
validates :last_name, presence: true
|
|
||||||
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
||||||
|
|
||||||
scope :members, -> { where koelkast: false }
|
scope :members, -> { where koelkast: false }
|
||||||
|
|
|
@ -27,6 +27,9 @@
|
||||||
<li><%= mail_to "tab@zeus.ugent.be", "Send feedback" %></li>
|
<li><%= mail_to "tab@zeus.ugent.be", "Send feedback" %></li>
|
||||||
<% if user_signed_in? %>
|
<% if user_signed_in? %>
|
||||||
<% if can? :manage, :all %>
|
<% if can? :manage, :all %>
|
||||||
|
<li>
|
||||||
|
<%= link_to "Place order", orders_path %>
|
||||||
|
</li>
|
||||||
<li class="dropdown">
|
<li class="dropdown">
|
||||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Products <span class="caret"></span></a>
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Products <span class="caret"></span></a>
|
||||||
<ul class="dropdown-menu" role="menu">
|
<ul class="dropdown-menu" role="menu">
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<%= image_tag product.avatar %>
|
<%= image_tag product.avatar %>
|
||||||
<div class="caption">
|
<div class="caption">
|
||||||
<h3><%= product.name %> - <%= euro(product.price) %> (<%= product.stock %>)</h3>
|
<h3><%= product.name %> - <%= euro(product.price) %> (<%= product.stock %>)</h3>
|
||||||
<% if current_user.admin? %>
|
<% if current_user && current_user.admin? %>
|
||||||
<p>
|
<p>
|
||||||
<%= link_to "Edit", edit_product_path(product), class: "btn btn-default" %>
|
<%= 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?'} %>
|
<%= link_to "Delete", product_path(product), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} %>
|
||||||
|
|
|
@ -227,7 +227,7 @@ Devise.setup do |config|
|
||||||
# config.navigational_formats = ['*/*', :html]
|
# config.navigational_formats = ['*/*', :html]
|
||||||
|
|
||||||
# The default HTTP method used to sign out a resource. Default is :delete.
|
# The default HTTP method used to sign out a resource. Default is :delete.
|
||||||
config.sign_out_via = :get
|
config.sign_out_via = :delete
|
||||||
|
|
||||||
# ==> OmniAuth
|
# ==> OmniAuth
|
||||||
# Add a new OmniAuth provider. Check the wiki for more information on setting
|
# Add a new OmniAuth provider. Check the wiki for more information on setting
|
||||||
|
|
67
db/seeds.rb
|
@ -6,3 +6,70 @@
|
||||||
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
|
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
|
||||||
# Mayor.create(name: 'Emanuel', city: cities.first)
|
# 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',
|
||||||
|
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
|
||||||
|
|
BIN
public/seeds/products/bueno.jpg
Normal file
After Width: | Height: | Size: 244 KiB |
BIN
public/seeds/products/club_mate.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
public/seeds/products/fanta.jpg
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
public/seeds/users/admin.jpg
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
public/seeds/users/benji.jpg
Normal file
After Width: | Height: | Size: 58 KiB |
BIN
public/seeds/users/don.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
public/seeds/users/silox.jpg
Normal file
After Width: | Height: | Size: 11 KiB |