DossierController: add tab for dossiers invites

This commit is contained in:
simon lehericey 2018-04-03 10:49:16 +02:00
parent 0bff7abb0c
commit 05ab4c5ca6
3 changed files with 86 additions and 3 deletions

View file

@ -85,11 +85,31 @@ module NewUser
end
def index
@dossiers = current_user.dossiers.includes(:procedure).page([params[:page].to_i, 1].max)
@user_dossiers = current_user.dossiers.includes(:procedure).page([params[:page].to_i, 1].max)
@dossiers_invites = current_user.dossiers_invites.includes(:procedure).page([params[:page].to_i, 1].max)
@current_tab = current_tab(@user_dossiers.count, @dossiers_invites.count)
@dossiers = case @current_tab
when 'mes-dossiers'
@user_dossiers
when 'dossiers-invites'
@dossiers_invites
end
end
private
def current_tab(mes_dossiers_count, dossiers_invites_count)
if dossiers_invites_count == 0
'mes-dossiers'
elsif mes_dossiers_count == 0
'dossiers-invites'
else
params[:current_tab].presence || 'mes-dossiers'
end
end
# FIXME: require(:dossier) when all the champs are united
def champs_params
params.permit(dossier: { champs_attributes: [:id, :value, :piece_justificative_file, value: []] })

View file

@ -1,6 +1,18 @@
.container
%h1.page-title Mes dossiers
.dossiers-headers.accompagnateur-header
.container
%h1.page-title Les dossiers
%ul.tabs
- if @user_dossiers.count > 0
%li{ class: (@current_tab == 'mes-dossiers') ? 'active' : nil }>
= link_to(dossiers_path(current_tab: 'mes-dossiers')) do
mes dossiers
- if @dossiers_invites.count > 0
%li{ class: (@current_tab == 'dossiers-invites') ? 'active' : nil }>
= link_to(dossiers_path(current_tab: 'dossiers-invites')) do
dossiers invités
.container
%table.table.dossiers-table.hoverable
%thead
%tr

View file

@ -333,4 +333,55 @@ describe NewUser::DossiersController, type: :controller do
end
end
end
describe '#index' do
before { sign_in(user) }
context 'when the user does not have any dossiers' do
before { get(:index) }
it { expect(assigns(:current_tab)).to eq('mes-dossiers') }
end
context 'when the user only have its own dossiers' do
let!(:own_dossier) { create(:dossier, user: user) }
before { get(:index) }
it { expect(assigns(:current_tab)).to eq('mes-dossiers') }
it { expect(assigns(:dossiers)).to match([own_dossier]) }
end
context 'when the user only have some dossiers invites' do
let!(:invite) { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') }
before { get(:index) }
it { expect(assigns(:current_tab)).to eq('dossiers-invites') }
it { expect(assigns(:dossiers)).to match([invite.dossier]) }
end
context 'when the user has both' do
let!(:own_dossier) { create(:dossier, user: user) }
let!(:invite) { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') }
context 'and there is no current_tab param' do
before { get(:index) }
it { expect(assigns(:current_tab)).to eq('mes-dossiers') }
end
context 'and there is "dossiers-invites" param' do
before { get(:index, params: { current_tab: 'dossiers-invites' }) }
it { expect(assigns(:current_tab)).to eq('dossiers-invites') }
end
context 'and there is "mes-dossiers" param' do
before { get(:index, params: { current_tab: 'mes-dossiers' }) }
it { expect(assigns(:current_tab)).to eq('mes-dossiers') }
end
end
end
end