Skip to content

Commit

Permalink
Merge pull request #47 from umts/access-control
Browse files Browse the repository at this point in the history
Allow adding existing users to rotations
  • Loading branch information
dfaulken authored Apr 17, 2017
2 parents e5baae8 + 849e1e2 commit 7d49a35
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
14 changes: 13 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UsersController < ApplicationController
before_action :find_user, only: [:destroy, :edit, :update]
before_action :find_user, except: %i(create index new)

def create
user_params = params.require(:user).permit!
Expand All @@ -22,9 +22,21 @@ def destroy

def index
@users = @roster.users
@other_users = User.all - @users
@fallback = @roster.fallback_user
end

def transfer
@user.rosters += [@roster]
if @user.save
flash[:message] = "Added #{@user.full_name} to roster."
redirect_to roster_users_path(@roster)
else
flash[:errors] = @user.errors.full_messages
redirect_to :back
end
end

def update
user_params = params.require(:user).permit!
if @user.update parse_roster_ids(user_params)
Expand Down
10 changes: 8 additions & 2 deletions app/views/users/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
%td= user.phone
%td= link_to 'Edit', edit_roster_user_path(@roster, user)
%td= button_to 'Destroy', roster_user_path(@roster, user), method: :delete
%tr
%td{ colspan: 6 }= link_to 'Add user', new_roster_user_path
= link_to 'Add new user', new_roster_user_path
- if @other_users.present?
.form
Add existing user:
= form_tag transfer_roster_users_path do
= select_tag :id,
options_from_collection_for_select(@other_users, :id, :last_name)
= submit_tag 'Add'
- if @fallback.nil?
%h3.slightly-red No fallback user!
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
end
end

resources :users, except: :show
resources :users, except: :show do
collection do
post :transfer
end
end

get 'twilio/call', to: 'twilio#call', as: :twilio_call
get 'twilio/text', to: 'twilio#text', as: :twilio_text
Expand Down
29 changes: 29 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@
end
end

describe 'POST #transfer' do
let(:user) { create :user }
let :submit do
when_current_user_is :whoever
post :transfer, id: user.id, roster_id: @roster.id
end
context 'user added succesfullly' do
it 'redirects to the index' do
submit
expect(response).to redirect_to roster_users_path(@roster)
end
it 'shows a nice message' do
submit
expect(flash[:message]).to be_present
end
end
context 'user somehow not added succesfully' do
before :each do
expect_any_instance_of(User)
.to receive(:save)
.and_return false
end
it 'redirects back and shows errors' do
expect { submit }.to redirect_back
expect(flash[:errors]).not_to be_nil
end
end
end

describe 'POST #update' do
before :each do
@new_roster = create :roster
Expand Down

0 comments on commit 7d49a35

Please sign in to comment.