users controller test
This commit is contained in:
parent
e8f4fe6849
commit
0885359d71
6 changed files with 37 additions and 25 deletions
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
.col-md-3
|
||||
.thumbnail.pic
|
||||
.form_row_image
|
||||
.center
|
||||
= image_tag product.avatar
|
||||
.caption
|
||||
= kcal_tag product.calories
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue