commit
28ec934e0a
4 changed files with 64 additions and 56 deletions
|
@ -1,32 +1,22 @@
|
|||
class UsersController < ApplicationController
|
||||
load_and_authorize_resource
|
||||
before_action :init, only: [:show, :edit, :update]
|
||||
|
||||
def show
|
||||
@user = User.find_by_id(params[:id]) || current_user
|
||||
@orders = @user.orders
|
||||
.order(:created_at)
|
||||
.reverse_order
|
||||
.paginate(page: params[:page])
|
||||
@products = @user.products
|
||||
.select("products.*", "sum(order_items.count) as count")
|
||||
.group(:product_id)
|
||||
.order("count")
|
||||
.reverse_order
|
||||
@categories = @user.products
|
||||
.select("products.category", "sum(order_items.count) as count")
|
||||
.group(:category)
|
||||
end
|
||||
|
||||
def edit
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
def update
|
||||
@user = User.find(params[:id])
|
||||
if @user.update_attributes(user_params)
|
||||
flash[:success] = "Successfully updated!"
|
||||
redirect_to @user
|
||||
else
|
||||
@user.reload
|
||||
render 'edit'
|
||||
end
|
||||
end
|
||||
|
@ -67,4 +57,17 @@ class UsersController < ApplicationController
|
|||
def user_params
|
||||
params.require(:user).permit(:avatar, :private)
|
||||
end
|
||||
|
||||
def init
|
||||
@user = User.find_by_id(params[:id]) || current_user
|
||||
@orders = @user.orders
|
||||
.order(:created_at)
|
||||
.reverse_order
|
||||
.paginate(page: params[:page])
|
||||
@products = @user.products
|
||||
.select("products.*", "sum(order_items.count) as count")
|
||||
.group(:product_id)
|
||||
.order("count")
|
||||
.reverse_order
|
||||
end
|
||||
end
|
||||
|
|
32
app/views/users/_sidebar.html.erb
Normal file
32
app/views/users/_sidebar.html.erb
Normal file
|
@ -0,0 +1,32 @@
|
|||
<div class="col-sm-3">
|
||||
<div>
|
||||
<h2>
|
||||
<%= @user.nickname %>
|
||||
<% if can? :edit, @user %>
|
||||
<%= link_to content_tag(:small, content_tag(:span, "", class: "glyphicon glyphicon-cog")), edit_user_path(@user) %>
|
||||
<% end %>
|
||||
</h2>
|
||||
</div>
|
||||
<%= content_tag :div, image_tag(@user.avatar, class: "img-circle img-thumbnail center"), class: "user_avatar center" %>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item text-muted">Orders</li>
|
||||
<li class="list-group-item"><strong>Orders placed</strong><span class="badge"><%= @user.orders_count %></span></li>
|
||||
<li class="list-group-item"><strong>Products ordered</strong><span class="badge"><%= @products.map(&:count).sum %></span></li>
|
||||
<% if can? :create, @user.orders.build %>
|
||||
<li class="list-group-item"><%= link_to "Place new order", new_user_order_path(@user), class: "btn btn-default btn-block" %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% if can? :edit, @user %>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item text-muted">
|
||||
<% if @user.dagschotel %>
|
||||
Huidige dagschotel
|
||||
<%= image_tag @user.dagschotel.avatar %>
|
||||
<%= link_to "Change dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
||||
<% else %>
|
||||
<%= link_to "Set dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
||||
<% end %>
|
||||
</li>
|
||||
</ul>
|
||||
<% end %>
|
||||
</div>
|
|
@ -1,16 +1,20 @@
|
|||
<h2>Edit <%= @user.nickname %></h2>
|
||||
<%= render 'flash' %>
|
||||
|
||||
<%= f_form_for @user do |f| %>
|
||||
<%= f.error_messages %>
|
||||
<div class="row">
|
||||
<%= render 'sidebar' %>
|
||||
<div class="col-sm-9">
|
||||
<h2>Edit your settings</h2>
|
||||
<%= f_form_for @user do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<%= image_tag @user.avatar(:large), class: "img-thumbnail img-responsive" %>
|
||||
<%= f.file_field :avatar %>
|
||||
<p>
|
||||
If you check this option, nobody will be able to order stuff for you through koelkast.
|
||||
Only on your account things can be ordered.
|
||||
<p>
|
||||
<%= f.check_box :private %>
|
||||
<%= f.file_field :avatar %>
|
||||
<p>
|
||||
If you check this option, nobody will be able to order stuff for you through koelkast.
|
||||
Only on your account things can be ordered.
|
||||
<p>
|
||||
<%= f.check_box :private %>
|
||||
|
||||
<%= f.submit "Update" %>
|
||||
<% end %>
|
||||
<%= f.submit "Update" %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,37 +1,6 @@
|
|||
<%= render partial: 'flash' %>
|
||||
<div class="row">
|
||||
<div class="col-sm-3">
|
||||
<div>
|
||||
<h2>
|
||||
<%= @user.nickname %>
|
||||
<% if can? :edit, @user %>
|
||||
<%= link_to content_tag(:small, content_tag(:span, "", class: "glyphicon glyphicon-cog")), edit_user_path(@user) %>
|
||||
<% end %>
|
||||
</h2>
|
||||
</div>
|
||||
<%= content_tag :div, image_tag(@user.avatar, class: "img-circle img-thumbnail center"), class: "user_avatar center" %>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item text-muted">Orders</li>
|
||||
<li class="list-group-item"><strong>Orders placed</strong><span class="badge"><%= @user.orders_count %></span></li>
|
||||
<li class="list-group-item"><strong>Products ordered</strong><span class="badge"><%= @products.map(&:count).sum %></span></li>
|
||||
<% if can? :create, @user.orders.build %>
|
||||
<li class="list-group-item"><%= link_to "Place new order", new_user_order_path(@user), class: "btn btn-default btn-block" %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% if can? :edit, @user %>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item text-muted">
|
||||
<% if @user.dagschotel %>
|
||||
Huidige dagschotel
|
||||
<%= image_tag @user.dagschotel.avatar %>
|
||||
<%= link_to "Change dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
||||
<% else %>
|
||||
<%= link_to "Set dagschotel", user_edit_dagschotel_path(@user), class: "btn btn-default btn-block" %>
|
||||
<% end %>
|
||||
</li>
|
||||
</ul>
|
||||
<% end %>
|
||||
</div>
|
||||
<%= render 'sidebar' %>
|
||||
<div id="user_info" class="col-sm-9">
|
||||
<% if @orders.any? %>
|
||||
<h4>Previously ordered</h4>
|
||||
|
|
Loading…
Reference in a new issue