Add db/seeds and fix two minor bugs

This commit is contained in:
benji 2015-02-10 07:15:25 +01:00
parent d653c6ca84
commit 7642e35d5d
14 changed files with 79 additions and 8 deletions

View file

@ -1,8 +1,7 @@
class UsersController < ApplicationController
load_and_authorize_resource
def show
@user = User.find_by_id(params[:id]) || current_user
authorize! :read, @user
@orders = @user.orders.includes(:products).paginate(page: params[:page])
@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)
@ -10,10 +9,13 @@ class UsersController < ApplicationController
def index
@users = User.members
authorize! :read, @users
end
def destroy
User.find(params[:id]).destroy
@user = User.find(params[:id])
authorize! :destroy, @users
@user.destroy
flash[:success] = "Succesfully removed user"
redirect_to action: :index
end

View file

@ -8,8 +8,9 @@ class Ability
can :schulden, :admins
elsif user.koelkast?
can :manage, Order
else
elsif user[:id]
can :read, :all
can :update, User
end
end
end

View file

@ -38,8 +38,6 @@ class User < ActiveRecord::Base
belongs_to :dagschotel, class_name: 'Product'
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"] }
scope :members, -> { where koelkast: false }

View file

@ -27,6 +27,9 @@
<li><%= mail_to "tab@zeus.ugent.be", "Send feedback" %></li>
<% if user_signed_in? %>
<% if can? :manage, :all %>
<li>
<%= link_to "Place order", orders_path %>
</li>
<li class="dropdown">
<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">

View file

@ -3,7 +3,7 @@
<%= image_tag product.avatar %>
<div class="caption">
<h3><%= product.name %> - <%= euro(product.price) %> (<%= product.stock %>)</h3>
<% if current_user.admin? %>
<% if current_user && 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?'} %>

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 = :get
config.sign_out_via = :delete
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting

View file

@ -6,3 +6,70 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 244 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

BIN
public/seeds/users/don.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB