From 0885359d7112f220ef112428344049e5ff40306e Mon Sep 17 00:00:00 2001 From: benji Date: Tue, 22 Sep 2015 20:06:50 +0200 Subject: [PATCH] users controller test --- app/controllers/users_controller.rb | 10 +----- app/models/user.rb | 2 ++ app/views/products/_links.html.haml | 6 ++-- app/views/products/_product.html.haml | 2 +- config/routes.rb | 3 +- spec/controllers/users_controller_spec.rb | 39 +++++++++++++++++------ 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f0c276a..a81a85b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,14 +25,6 @@ class UsersController < ApplicationController @categories = Product.categories end - def update_dagschotel - @user.dagschotel = Product.find(params[:product_id]) - @user.save - - flash[:success] = "Succesfully updated dagschotel" - redirect_to @user - end - def quickpay order = @user.orders.build order.order_items.build(count: 1, product: @user.dagschotel) @@ -47,7 +39,7 @@ class UsersController < ApplicationController private def user_params - params.require(:user).permit(:avatar, :private) + params.require(:user).permit(:avatar, :private, :dagschotel_id) end def init diff --git a/app/models/user.rb b/app/models/user.rb index a4c89c2..9c8f7bd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,6 +29,8 @@ class User < ActiveRecord::Base has_many :products, through: :orders belongs_to :dagschotel, class_name: 'Product' + validates :dagschotel, presence: true, if: -> { dagschotel_id } + scope :members, -> { where koelkast: false } scope :publik, -> { where private: false } diff --git a/app/views/products/_links.html.haml b/app/views/products/_links.html.haml index e07ba1f..b2f3eba 100644 --- a/app/views/products/_links.html.haml +++ b/app/views/products/_links.html.haml @@ -1,9 +1,9 @@ -- if controller_name == 'products' && current_user && current_user.admin? +- if controller_name == 'products' && can?(:manage, Product) = link_to "Edit", edit_product_path(product), class: "btn btn-default" = link_to "Delete", product_path(product), method: :delete, class: "btn btn-danger", data: {confirm: 'Are you sure?'} - if controller_name == 'users' .product_dagschotel - if current_user.dagschotel != product - = link_to "Make dagschotel", dagschotel_user_path(current_user, product), class: "btn btn-default" + = button_to "Make dagschotel", { controller: 'users', action: 'update', "user[dagschotel_id]" => product.id }, method: :put, class: "btn btn-default" - else - = link_to "Huidige dagschotel", dagschotel_user_path(current_user, product), class: "btn btn-success", disabled: true + %span.btn.btn-success= "Current dagschotel" diff --git a/app/views/products/_product.html.haml b/app/views/products/_product.html.haml index 4527712..db6c08b 100644 --- a/app/views/products/_product.html.haml +++ b/app/views/products/_product.html.haml @@ -1,6 +1,6 @@ .col-md-3 .thumbnail.pic - .form_row_image + .center = image_tag product.avatar .caption = kcal_tag product.calories diff --git a/config/routes.rb b/config/routes.rb index c0d8efe..a8a3aff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,9 +20,8 @@ Rails.application.routes.draw do resources :users, only: [:show, :edit, :update] do resources :orders, only: [:new, :create, :destroy] member do - get 'quickpay' => 'users#quickpay' + get 'quickpay' => 'users#quickpay' get 'dagschotel/edit' => 'users#edit_dagschotel', as: 'edit_dagschotel' - get 'dagschotel/:product_id' => 'users#update_dagschotel', as: 'dagschotel' end end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 7a982f4..d394019 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -1,5 +1,10 @@ -require 'identicon' -require 'faker' +# quickpay_user GET /users/:id/quickpay(.:format) users#quickpay +# edit_dagschotel_user GET /users/:id/dagschotel/edit(.:format) users#edit_dagschotel +# edit_user GET /users/:id/edit(.:format) users#edit +# user GET /users/:id(.:format) users#show +# PATCH /users/:id(.:format) users#update +# PUT /users/:id(.:format) users#update +# describe UsersController, type: :controller do before :each do @@ -7,6 +12,10 @@ describe UsersController, type: :controller do sign_in @user end + ########## + # SHOW # + ########## + describe 'GET show' do before :each do get :show, id: @user @@ -22,6 +31,10 @@ describe UsersController, type: :controller do end end + ########## + # EDIT # + ########## + describe 'GET edit' do before :each do get :edit, id: @user @@ -36,6 +49,10 @@ describe UsersController, type: :controller do end end + ############ + # UPDATE # + ############ + describe 'PUT update' do it 'should load the correct user' do put :update, id: @user, user: attributes_for(:user) @@ -48,9 +65,19 @@ describe UsersController, type: :controller do put :update, id: @user, user: { private: new_private } expect(@user.reload.private).to be new_private end + + it 'should update dagschotel' do + product = create :product + put :update, id: @user, user: { dagschotel_id: product.id } + expect(@user.reload.dagschotel).to eq(product) + end end end + ##################### + # EDIT_DAGSCHOTEL # + ##################### + describe 'GET edit_dagschotel' do it 'should render the page' do get :edit_dagschotel, id: @user @@ -58,12 +85,4 @@ describe UsersController, type: :controller do expect(response).to have_http_status(200) end end - - describe 'GET update_dagschotel' do - it 'should update the dagschotel' do - product = create :product - get :update_dagschotel, id: @user, product_id: product - expect(@user.reload.dagschotel).to eq(product) - end - end end