Merge conflict

This commit is contained in:
Caroline De Brouwer 2015-02-12 16:19:41 +01:00
commit 5b5c454e0e
12 changed files with 110 additions and 79 deletions

View file

@ -10,13 +10,20 @@ ready = ->
$('.form_row').each((index, row) ->
disIfNec(row)
$(row).on('input', recalculate)
)
recalculate()
disIfNec = (row) ->
counter = parseInt($(row).find('.row_counter').val())
$(row).find('.btn-dec').prop('disabled', counter == 0);
$(row).find('.btn-inc').prop('disabled', counter == parseInt($(row).find('.stock').val()))
recalculate = () ->
value = ($(row).find('.row_counter').val() * $(row).find('.price').val() for row in $('.form_row')).reduce (a, b) -> a+b
$('#order_total_price').val((value / 100.0).toFixed(2))
increment = (button, n) ->
row = $(button).closest('.form_row')
@ -27,10 +34,7 @@ increment = (button, n) ->
# Disable buttons if necessary
disIfNec(row)
# Update the price
oldVal = parseFloat($('#order_total_price').val()) * 100
newVal = (oldVal + parseInt($(row).find('.price').val()) * n)/100
$('#order_total_price').val(newVal.toFixed(2))
recalculate()
$(document).ready(ready)
$(document).on('page:load', ready)

View file

@ -1,27 +1,28 @@
class OrdersController < ApplicationController
include ActionView::Helpers::NumberHelper
load_and_authorize_resource
def new
@user = User.find(params[:user_id])
@order = @user.orders.build
@products = Product.all
@order_items = @order.order_items
redirect_to root_path, flash: { error: "Koelkast can't order things." } if @user.koelkast?
@products.each do |p|
@order_items.build(product: p)
end
@order = @user.orders.build
@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?
@order = @user.orders.build(order_params)
@products = Product.all
@order_items = @order.order_items
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)
render 'new'
end
end
@ -34,7 +35,7 @@ class OrdersController < ApplicationController
else
flash[:error] = "This order has been placed too long ago, it can't be removed. Please contact a sysadmin."
end
redirect_to koelkast_root_path
redirect_to root_path
end
def overview
@ -57,6 +58,6 @@ class OrdersController < ApplicationController
private
def order_params
params.require(:order).permit(order_items_attributes: [:count, product_attributes: [:id, :price_cents, :stock]])
params.require(:order).permit(order_items_attributes: [:count, product_attributes: [:id]])
end
end

View file

@ -1,20 +1,19 @@
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])
@orders = @user.orders.includes(:products).order(:created_at).reverse_order.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)
end
def index
@users = User.members
authorize! :read, @users
end
def destroy
@user = User.find(params[:id])
authorize! :destroy, @users
@user.destroy
flash[:success] = "Succesfully removed user"
redirect_to action: :index

View file

@ -10,7 +10,7 @@ module ApplicationHelper
end
def euro(f)
"#{number_with_precision f, precision: 2}"
number_to_currency(f, unit: '€')
end
def f_form_for(record, options = {}, &block)

View file

@ -36,4 +36,15 @@ class Order < ActiveRecord::Base
|oi| pluralize(oi.count, oi.product.name)
}.to_sentence
end
def g_order_items(products, params = {})
products.each do |p|
if (oi = self.order_items.select { |t| t.product == p }).size > 0
puts oi.inspect
oi.first.count = [oi.first.product.stock, oi.first.count].min
else
self.order_items.build(product: p)
end
end
end
end

View file

@ -38,6 +38,8 @@ 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, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
# validates_attachment :avatar, presence: true

View file

@ -1,7 +1,9 @@
class InStockValidator < ActiveModel::Validator
def validate(record)
p_short = []
record.order_items.each do |oi|
record.errors[oi.product.name] = "is not in stock anymore" if oi.count > oi.product.stock
p_short.append oi.product.name if oi.count > oi.product.stock
end
record.errors.add(:base, "There is not enough stock for your order of the following products: #{p_short.join(', ')}") if p_short.size > 0
end
end

View file

@ -23,4 +23,10 @@
<strong>Warning!</strong> <%= flash[:warning] %>
</div>
<% end %>
<% if flash[:alert] %>
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<strong>Error!</strong> <%= flash[:alert] %>
</div>
<% end %>
</div>

View file

@ -11,60 +11,62 @@
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse">
<div class="hidden-xs navbar-form navbar-right">
<div class="form-group">
<% if user_signed_in? %>
<%= button_to "Logout", destroy_user_session_path, class: "btn btn-default form-control", method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
<%= link_to "Register", new_user_registration_path, class: "btn btn-default form-control" %>
<% end %>
</div>
</div>
<ul class="nav navbar-nav navbar-right">
<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">
<li><%= link_to "List", products_path %></li>
<li><%= link_to "Add product" , new_product_path %></li>
<li><%= link_to "Add stock", stock_products_path %></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Users <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><%= link_to "List" , users_path %></li>
</ul>
</li>
<li><%= link_to 'Download schulden', admins_schulden_path(format: :csv) %></li>
<% unless current_user && current_user.koelkast? %>
<div class="collapse navbar-collapse">
<div class="hidden-xs navbar-form navbar-right">
<div class="form-group">
<% if user_signed_in? %>
<%= button_to "Logout", destroy_user_session_path, class: "btn btn-default form-control", method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
<%= link_to "Register", new_user_registration_path, class: "btn btn-default form-control" %>
<% end %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Logged in as <%= current_user.nickname %> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><%= link_to "Edit password", edit_user_registration_path %></li>
</ul>
</li>
<% end %>
</ul>
<div class="visible-xs navbar-form">
<div class="form-group">
<% if user_signed_in? %>
<%= button_to "Logout", destroy_user_session_path, class: "btn btn-default form-control", method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
<%= link_to "Register", new_user_registration_path, class: "btn btn-default form-control" %>
<% end %>
</div>
</div>
</div>
<ul class="nav navbar-nav navbar-right">
<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">
<li><%= link_to "List", products_path %></li>
<li><%= link_to "Add product" , new_product_path %></li>
<li><%= link_to "Add stock", stock_products_path %></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Users <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><%= link_to "List" , users_path %></li>
</ul>
</li>
<li><%= link_to 'Download schulden', admins_schulden_path(format: :csv) %></li>
<% end %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Logged in as <%= current_user.nickname %> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><%= link_to "Edit password", edit_user_registration_path %></li>
</ul>
</li>
<% end %>
</ul>
<div class="visible-xs navbar-form">
<div class="form-group">
<% if user_signed_in? %>
<%= button_to "Logout", destroy_user_session_path, class: "btn btn-default form-control", method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
<%= link_to "Register", new_user_registration_path, class: "btn btn-default form-control" %>
<% end %>
</div>
</div>
<% end %>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->

View file

@ -1,6 +1,6 @@
<div class="col-md-3">
<div class="thumbnail monopoly">
<h3 class="header" style="background-color: #<%= get_color(user)%>;" ><%= user.full_name %></h3>
<h3 class="header" style="background-color: #<%= get_color(user)%>;" ><%= link_to user.full_name, user %></h3>
<div class="caption">
<%= image_tag(user.avatar , class: "img-circle avatar") %>
<p><strong>Name:</strong> <%= user.name %></p>
@ -8,11 +8,13 @@
<p><strong>Nickname:</strong> <%= user.nickname %></p>
<p><strong>Balance:</strong> <%= euro(user.balance) %></p>
</div>
<div class="footer">
<p>
<%= link_to "Delete", user_path(user), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} %>
</p>
<% if current_user.admin? %>
<div class="footer">
<p>
<%= link_to "Delete", user_path(user), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} %>
</p>
</div>
</div>
<% end %>
</div>
</div>

View file

@ -2,7 +2,7 @@
<div class="row">
<div class="user_info">
<h2><%= @user.nickname %> (<%= @user.full_name %>)</h2>
<h5><%= link_to "[Edit profile]" , edit_user_registration_path %></h5>
<h5><%= link_to "[Edit profile]" , edit_user_registration_path if current_user == @user%></h5>
<h4>Balance: <%= euro(@user.balance) %></h4>
</div>

View file

@ -46,6 +46,8 @@ users = [
},
{
nickname: 'koelkast',
name: 'K.',
last_name: 'Koelkast',
# avatar: File.new('public/seeds/users/admin.jpg', 'r'),
koelkast: true
},