Some changes to user profile page and other fixes

This commit is contained in:
benji 2015-01-13 13:06:42 +01:00
parent aa97b51ca6
commit 49dc9201b6
6 changed files with 24 additions and 12 deletions

View file

@ -4,6 +4,8 @@ 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 = Order.joins(:products).select(:count, "products.*", "orders.id").where(user: @user).group_by &:id @orders = Order.joins(:products).select(:count, "products.*", "orders.id").where(user: @user).group_by &:id
@products = @user.products.select("products.*", "count(products.id) as count").group(:product_id)
@categories = @user.products.select("products.category", "count(products.category) as count").group(:category)
end end
def index def index
@ -20,7 +22,7 @@ class UsersController < ApplicationController
user = User.find(params[:user_id]) user = User.find(params[:user_id])
user.dagschotel = Product.find(params[:product_id]) user.dagschotel = Product.find(params[:product_id])
if user.save if user.save
flash[:success] = "Succesfully removed user" flash[:success] = "Succesfully updated dagschotel"
else else
flash[:error] = "Error updating dagschotel" flash[:error] = "Error updating dagschotel"
end end

View file

@ -31,6 +31,7 @@ class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium, default_url: "http://babeholder.pixoil.com/img/70/70" has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium, default_url: "http://babeholder.pixoil.com/img/70/70"
has_many :orders, -> { includes :products } has_many :orders, -> { includes :products }
has_many :products, through: :orders
belongs_to :dagschotel, class_name: 'Product' belongs_to :dagschotel, class_name: 'Product'
validates :nickname, presence: true, uniqueness: true validates :nickname, presence: true, uniqueness: true

View file

@ -1,5 +1,3 @@
<%- headers = ['nickname', 'schulden'] -%> <%- headers = ['nickname', 'schulden'] -%>
<%= CSV.generate_line headers %> <%= CSV.generate_line headers -%>
<%- @users.each do |user| -%> <% @users.each do |user| %><%= CSV.generate_line([user.nickname, user.balance]) %><% end %>
<%= CSV.generate_line([user.nickname, user.balance]) %>
<%- end -%>

View file

@ -0,0 +1,3 @@
<p>
<%= pluralize(category.count, category.category) %>
</p>

View file

@ -0,0 +1,3 @@
<p>
<%= pluralize(product.count, product.name) %>
</p>

View file

@ -9,13 +9,18 @@
</div> </div>
<% if @user.orders.any? %> <% if @user.orders.any? %>
<div> <div class="col-md-6">
<h3>Orders (<%= @user.orders.count %>)</h3> <h3>Orders (<%= @user.orders_count %>)</h3>
<ol class="orders"> <% @orders.each do |k,v| %>
<% @orders.each do |k,v| %> <%= render 'orders/order', order: v.first, products: v %>
<%= render 'orders/order', order: v.first, products: v %> <% end %>
<% end %>
</ol>
</div> </div>
<% end %> <% end %>
<div class="col-md-6">
<h3>Products</h3>
<%= render partial: "products/product_row", collection: @products, as: :product %>
<h4>Categories</h4>
<%= render partial: "products/category", collection: @categories %>
</div>
</div> </div>