diff --git a/.gitignore b/.gitignore index 11b75c0..d4e179b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Ignore bundler config. /.bundle +/vendor # Ignore the default SQLite database. /db/*.sqlite3 diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 034425e..8042d64 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -38,6 +38,10 @@ $gray-medium-light: #eaeaea; text-align: center; } +.float-right{ + float: right; +} + .form-field{ margin-bottom: 15px; text-align: bottom; diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d56ac92..9922687 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -5,16 +5,17 @@ class UsersController < ApplicationController def show end - def edit - end - def update - if @user.update_attributes(user_params) + if user_params.empty? + flash[:notice] = "Nothing happened." + redirect_to @user + elsif @user.update_attributes(user_params) flash[:success] = "Successfully updated!" redirect_to @user else + flash[:error] = "Update failed!" @user.reload - render 'edit' + render 'show' end end @@ -38,7 +39,7 @@ class UsersController < ApplicationController private def user_params - params.require(:user).permit(:avatar, :private, :dagschotel_id) + params.fetch(:user, {}).permit(:avatar, :private, :dagschotel_id) end def init diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index 0a3efe1..fc714df 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -23,12 +23,8 @@ %li= link_to "List", products_path %li= link_to "Add product" , barcode_products_path %li= link_to "Barcodes", barcodes_path - %li.dropdown - %a.dropdown-toggle{"data-toggle" => "dropdown", href: "#"} - Logged in as #{current_user.name} - %b.caret - %ul.dropdown-menu - %li= link_to "Edit profile", edit_user_path(current_user) + %li + %p.navbar-text Logged in as #{current_user.name} %li %p.navbar-text Balance: diff --git a/app/views/users/_sidebar.html.haml b/app/views/users/_sidebar.html.haml index 80c6d5b..6387df7 100644 --- a/app/views/users/_sidebar.html.haml +++ b/app/views/users/_sidebar.html.haml @@ -1,10 +1,9 @@ .col-sm-3 %div - %h2 + %h2.float-right + = content_tag :div, image_tag(@user.avatar, class: "img-circle img-thumbnail center"), class: "user_avatar center" + %h2.center = @user.name - - if can? :edit, @user - = link_to content_tag(:small, content_tag(:span, "", class: "glyphicon glyphicon-cog")), edit_user_path(@user) - = content_tag :div, image_tag(@user.avatar, class: "img-circle img-thumbnail center"), class: "user_avatar center" %ul.list-group %li.list-group-item.text-muted Orders %li.list-group-item @@ -15,12 +14,24 @@ %span.badge= @user.products_group_by_id.map(&:count).sum - if can? :create, @user.orders.build %li.list-group-item= link_to "Place new order", new_user_order_path(@user), class: "btn btn-default btn-block" + = render 'errors', object: @user - if can? :edit, @user %ul.list-group %li.list-group-item.text-muted - if @user.dagschotel - Huidige dagschotel - \#{image_tag @user.dagschotel.avatar} + .center + %p= image_tag @user.dagschotel.avatar, title: "Huidige dagschotel" \#{link_to "Change dagschotel", edit_dagschotel_user_path(@user), class: "btn btn-default btn-block"} - else = link_to "Set dagschotel", edit_dagschotel_user_path(@user), class: "btn btn-default btn-block" + %li.list-group-item.text-muted + %p Orders can be placed on koelkast for every public account. Private accounts can only order products by logging in here. + // = link_to "Go #{if @user.private then "public" else "private" end}", toggle_privacy_user_path(@user), class: "btn btn-default btn-block" + = f_form_for @user do |f| + .hidden + = f.check_box :private, checked: !@user.private + = f.submit "Go #{@user.private ? "public" : "private"}", class: "btn btn-default btn-block" + %li.list-group-item.text-muted + = f_form_for @user do |f| + = f.file_field :avatar + = f.submit "Change avatar", class: "btn btn-default btn-block" diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml deleted file mode 100644 index e0884e3..0000000 --- a/app/views/users/edit.html.haml +++ /dev/null @@ -1,13 +0,0 @@ -.row - = render 'sidebar' - .col-sm-9 - %h2 Edit your settings - = f_form_for @user do |f| - = f.error_messages - = 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" diff --git a/config/routes.rb b/config/routes.rb index 1663591..e223888 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,7 +17,7 @@ Rails.application.routes.draw do end end - resources :users, only: [:show, :edit, :update] do + resources :users, only: [:show, :update] do resources :orders, only: [:new, :create, :destroy] member do get 'quickpay' => 'users#quickpay' diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 4c4af9c..f3d316c 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -34,28 +34,6 @@ describe UsersController, type: :controller do end end - ########## - # EDIT # - ########## - - describe 'GET edit' do - before :each do - get :edit, id: @user - end - - it 'should be successful' do - expect(response).to have_http_status(200) - end - - it 'should render the form' do - expect(response).to render_template(:edit) - end - - it 'should load the correct user' do - expect(assigns(:user)).to eq(@user) - end - end - ############ # UPDATE # ############ @@ -67,10 +45,11 @@ describe UsersController, type: :controller do end context 'successful' do - it 'should update attributes' do + it 'should update privacy' do new_private = !(@user.private) put :update, id: @user, user: { private: new_private } expect(@user.reload.private).to be new_private + expect(flash[:success]).to be_present end it 'should update dagschotel' do @@ -78,6 +57,25 @@ describe UsersController, type: :controller do put :update, id: @user, user: { dagschotel_id: product.id } expect(@user.reload.dagschotel).to eq(product) end + + it 'should accept real images' do + file = fixture_file_upload('files/real-image.png', 'image/png') + put :update, id: @user, user: { avatar: file } + expect(flash[:success]).to be_present + end + end + + context 'danger zone' do + it 'should warn for NOPs' do + put :update, id: @user, user: {} + expect(flash[:notice]).to be_present + end + + it 'should not accept unreal images' do + file = fixture_file_upload('files/unreal-image.svg', 'image/svg+xml') + put :update, id: @user, user: { avatar: file } + expect(flash[:error]).to be_present + end end end diff --git a/spec/fixtures/files/real-image.png b/spec/fixtures/files/real-image.png new file mode 100644 index 0000000..ae84994 Binary files /dev/null and b/spec/fixtures/files/real-image.png differ diff --git a/spec/fixtures/files/unreal-image.svg b/spec/fixtures/files/unreal-image.svg new file mode 100644 index 0000000..a920dc7 --- /dev/null +++ b/spec/fixtures/files/unreal-image.svg @@ -0,0 +1,182 @@ + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + +