diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 31dfde0..717d8ca 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/views/users/_sidebar.html.erb b/app/views/users/_sidebar.html.erb new file mode 100644 index 0000000..4121c70 --- /dev/null +++ b/app/views/users/_sidebar.html.erb @@ -0,0 +1,32 @@ +
+
+

+ <%= @user.nickname %> + <% if can? :edit, @user %> + <%= link_to content_tag(:small, content_tag(:span, "", class: "glyphicon glyphicon-cog")), edit_user_path(@user) %> + <% end %> +

+
+ <%= content_tag :div, image_tag(@user.avatar, class: "img-circle img-thumbnail center"), class: "user_avatar center" %> + + <% if can? :edit, @user %> + + <% end %> +
diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 935de30..f9c87b4 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -1,16 +1,20 @@ -

Edit <%= @user.nickname %>

<%= render 'flash' %> -<%= f_form_for @user do |f| %> - <%= f.error_messages %> +
+ <%= render 'sidebar' %> +
+

Edit your settings

+ <%= f_form_for @user do |f| %> + <%= f.error_messages %> - <%= image_tag @user.avatar(:large), class: "img-thumbnail img-responsive" %> - <%= f.file_field :avatar %> -

- If you check this option, nobody will be able to order stuff for you through koelkast. - Only on your account things can be ordered. -

- <%= f.check_box :private %> + <%= f.file_field :avatar %> +

+ If you check this option, nobody will be able to order stuff for you through koelkast. + Only on your account things can be ordered. +

+ <%= f.check_box :private %> - <%= f.submit "Update" %> -<% end %> + <%= f.submit "Update" %> + <% end %> +

+
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 08c1976..0a81052 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,37 +1,6 @@ <%= render partial: 'flash' %>
-
-
-

- <%= @user.nickname %> - <% if can? :edit, @user %> - <%= link_to content_tag(:small, content_tag(:span, "", class: "glyphicon glyphicon-cog")), edit_user_path(@user) %> - <% end %> -

-
- <%= content_tag :div, image_tag(@user.avatar, class: "img-circle img-thumbnail center"), class: "user_avatar center" %> - - <% if can? :edit, @user %> - - <% end %> -
+ <%= render 'sidebar' %>
<% if @orders.any? %>

Previously ordered