diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 80fb528..65bde11 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,6 +4,8 @@ class UsersController < ApplicationController def show @user = User.find_by_id(params[:id]) || current_user @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 def index @@ -20,7 +22,7 @@ class UsersController < ApplicationController user = User.find(params[:user_id]) user.dagschotel = Product.find(params[:product_id]) if user.save - flash[:success] = "Succesfully removed user" + flash[:success] = "Succesfully updated dagschotel" else flash[:error] = "Error updating dagschotel" end diff --git a/app/models/user.rb b/app/models/user.rb index b645505..729dd2f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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_many :orders, -> { includes :products } + has_many :products, through: :orders belongs_to :dagschotel, class_name: 'Product' validates :nickname, presence: true, uniqueness: true diff --git a/app/views/admins/schulden.csv.erb b/app/views/admins/schulden.csv.erb index d707805..1c6f801 100644 --- a/app/views/admins/schulden.csv.erb +++ b/app/views/admins/schulden.csv.erb @@ -1,5 +1,3 @@ <%- headers = ['nickname', 'schulden'] -%> -<%= CSV.generate_line headers %> -<%- @users.each do |user| -%> - <%= CSV.generate_line([user.nickname, user.balance]) %> -<%- end -%> +<%= CSV.generate_line headers -%> +<% @users.each do |user| %><%= CSV.generate_line([user.nickname, user.balance]) %><% end %> diff --git a/app/views/products/_category.html.erb b/app/views/products/_category.html.erb new file mode 100644 index 0000000..e86381c --- /dev/null +++ b/app/views/products/_category.html.erb @@ -0,0 +1,3 @@ +
+ <%= pluralize(category.count, category.category) %> +
diff --git a/app/views/products/_product_row.html.erb b/app/views/products/_product_row.html.erb new file mode 100644 index 0000000..dcc241d --- /dev/null +++ b/app/views/products/_product_row.html.erb @@ -0,0 +1,3 @@ ++ <%= pluralize(product.count, product.name) %> +
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 203b910..6760b5b 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -9,13 +9,18 @@ <% if @user.orders.any? %> -