InviteUser → Invite

This commit is contained in:
gregoirenovel 2018-10-10 09:23:08 +02:00
parent ed11852300
commit c8f872c566
12 changed files with 25 additions and 52 deletions

View file

@ -5,7 +5,7 @@ class InvitesController < ApplicationController
email = params[:invite_email].downcase email = params[:invite_email].downcase
dossier = current_user.dossiers.find(params[:dossier_id]) dossier = current_user.dossiers.find(params[:dossier_id])
invite = InviteUser.create( invite = Invite.create(
dossier: dossier, dossier: dossier,
user: User.find_by(email: email), user: User.find_by(email: email),
email: email, email: email,

View file

@ -35,7 +35,7 @@ class Commentaire < ApplicationRecord
def notify def notify
dossier_user_email = dossier.user.email dossier_user_email = dossier.user.email
invited_users_emails = dossier.invites_user.pluck(:email).to_a invited_users_emails = dossier.invites.pluck(:email).to_a
# - If the email is the contact email, the commentaire is a copy # - If the email is the contact email, the commentaire is a copy
# of an automated notification email we sent to a user, so do nothing. # of an automated notification email we sent to a user, so do nothing.

View file

@ -24,7 +24,6 @@ class Dossier < ApplicationRecord
has_many :cadastres, dependent: :destroy has_many :cadastres, dependent: :destroy
has_many :commentaires, dependent: :destroy has_many :commentaires, dependent: :destroy
has_many :invites, dependent: :destroy has_many :invites, dependent: :destroy
has_many :invites_user, class_name: 'InviteUser', dependent: :destroy
has_many :follows has_many :follows
has_many :followers_gestionnaires, through: :follows, source: :gestionnaire has_many :followers_gestionnaires, through: :follows, source: :gestionnaire
has_many :avis, dependent: :destroy has_many :avis, dependent: :destroy
@ -159,7 +158,7 @@ class Dossier < ApplicationRecord
end end
def invite_for_user(user) def invite_for_user(user)
invites_user.find_by(user_id: user.id) invites.find_by(user_id: user.id)
end end
def can_be_en_construction? def can_be_en_construction?

View file

@ -1,2 +0,0 @@
class InviteUser < Invite
end

View file

@ -44,7 +44,7 @@ describe InvitesController, type: :controller do
sign_in(user) sign_in(user)
end end
it { expect { subject }.to change(InviteUser, :count).by(1) } it { expect { subject }.to change(Invite, :count).by(1) }
end end
end end
end end
@ -54,7 +54,7 @@ describe InvitesController, type: :controller do
shared_examples_for "he can not create a invite" do shared_examples_for "he can not create a invite" do
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) } it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect { subject rescue nil }.to change(InviteUser, :count).by(0) } it { expect { subject rescue nil }.to change(Invite, :count).by(0) }
end end
context 'when user has no access to dossier' do context 'when user has no access to dossier' do
@ -73,7 +73,7 @@ describe InvitesController, type: :controller do
dossier.update(user: signed_in_profile) dossier.update(user: signed_in_profile)
end end
it { expect { subject }.to change(InviteUser, :count).by(1) } it { expect { subject }.to change(Invite, :count).by(1) }
it "redirects to the previous URL" do it "redirects to the previous URL" do
expect(subject).to redirect_to("/dossiers/#{dossier.id}/brouillon") expect(subject).to redirect_to("/dossiers/#{dossier.id}/brouillon")

View file

@ -50,13 +50,13 @@ describe NewUser::DossiersController, type: :controller do
end end
context 'when an invite asks for a dossier where they were invited' do context 'when an invite asks for a dossier where they were invited' do
before { create(:invite, dossier: asked_dossier, user: user, type: 'InviteUser') } before { create(:invite, dossier: asked_dossier, user: user) }
it_behaves_like 'redirects and flashes' it_behaves_like 'redirects and flashes'
end end
context 'when an invite asks for another dossier' do context 'when an invite asks for another dossier' do
before { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') } before { create(:invite, dossier: create(:dossier), user: user) }
it_behaves_like 'redirects and flashes' it_behaves_like 'redirects and flashes'
end end
@ -84,13 +84,13 @@ describe NewUser::DossiersController, type: :controller do
end end
context 'when an invite asks for a dossier where they were invited' do context 'when an invite asks for a dossier where they were invited' do
before { create(:invite, dossier: asked_dossier, user: user, type: 'InviteUser') } before { create(:invite, dossier: asked_dossier, user: user) }
it_behaves_like 'does not redirect nor flash' it_behaves_like 'does not redirect nor flash'
end end
context 'when an invite asks for another dossier' do context 'when an invite asks for another dossier' do
before { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') } before { create(:invite, dossier: create(:dossier), user: user) }
it_behaves_like 'redirects and flashes' it_behaves_like 'redirects and flashes'
end end
@ -123,14 +123,14 @@ describe NewUser::DossiersController, type: :controller do
end end
context 'when an invite save the draft for a dossier where they where invited' do context 'when an invite save the draft for a dossier where they where invited' do
before { create(:invite, dossier: asked_dossier, user: user, type: 'InviteUser') } before { create(:invite, dossier: asked_dossier, user: user) }
let(:draft) { true } let(:draft) { true }
it_behaves_like 'does not redirect nor flash' it_behaves_like 'does not redirect nor flash'
end end
context 'when an invite submit a dossier where they where invited' do context 'when an invite submit a dossier where they where invited' do
before { create(:invite, dossier: asked_dossier, user: user, type: 'InviteUser') } before { create(:invite, dossier: asked_dossier, user: user) }
let(:draft) { false } let(:draft) { false }
it_behaves_like 'redirects and flashes' it_behaves_like 'redirects and flashes'
@ -370,7 +370,7 @@ describe NewUser::DossiersController, type: :controller do
context 'when the user has an invitation but is not the owner' do context 'when the user has an invitation but is not the owner' do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier) }
let!(:invite) { create(:invite, dossier: dossier, user: user, type: 'InviteUser') } let!(:invite) { create(:invite, dossier: dossier, user: user) }
context 'and the invite saves a draft' do context 'and the invite saves a draft' do
let(:payload) { submit_payload.merge(save_draft: true) } let(:payload) { submit_payload.merge(save_draft: true) }
@ -493,7 +493,7 @@ describe NewUser::DossiersController, type: :controller do
context 'when the user has an invitation but is not the owner' do context 'when the user has an invitation but is not the owner' do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier) }
let!(:invite) { create(:invite, dossier: dossier, user: user, type: 'InviteUser') } let!(:invite) { create(:invite, dossier: dossier, user: user) }
before do before do
dossier.en_construction! dossier.en_construction!
@ -525,7 +525,7 @@ describe NewUser::DossiersController, type: :controller do
end end
context 'when the user only have some dossiers invites' do context 'when the user only have some dossiers invites' do
let!(:invite) { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') } let!(:invite) { create(:invite, dossier: create(:dossier), user: user) }
before { get(:index) } before { get(:index) }
@ -535,7 +535,7 @@ describe NewUser::DossiersController, type: :controller do
context 'when the user has both' do context 'when the user has both' do
let!(:own_dossier) { create(:dossier, user: user) } let!(:own_dossier) { create(:dossier, user: user) }
let!(:invite) { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') } let!(:invite) { create(:invite, dossier: create(:dossier), user: user) }
context 'and there is no current_tab param' do context 'and there is no current_tab param' do
before { get(:index) } before { get(:index) }
@ -560,8 +560,8 @@ describe NewUser::DossiersController, type: :controller do
before do before do
Timecop.freeze(4.days.ago) { create(:dossier, user: user) } Timecop.freeze(4.days.ago) { create(:dossier, user: user) }
Timecop.freeze(2.days.ago) { create(:dossier, user: user) } Timecop.freeze(2.days.ago) { create(:dossier, user: user) }
Timecop.freeze(4.days.ago) { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') } Timecop.freeze(4.days.ago) { create(:invite, dossier: create(:dossier), user: user) }
Timecop.freeze(2.days.ago) { create(:invite, dossier: create(:dossier), user: user, type: 'InviteUser') } Timecop.freeze(2.days.ago) { create(:invite, dossier: create(:dossier), user: user) }
get(:index) get(:index)
end end

View file

@ -17,7 +17,7 @@ RSpec.describe Users::CarteController, type: :controller do
let(:adresse) { etablissement.geo_adresse } let(:adresse) { etablissement.geo_adresse }
before do before do
create :invite, dossier: dossier, user: invite_by_user, email: invite_by_user.email, type: 'InviteUser' create :invite, dossier: dossier, user: invite_by_user, email: invite_by_user.email
sign_in user sign_in user
end end

View file

@ -20,7 +20,7 @@ describe UsersController, type: :controller do
context 'when user is invite by the owner' do context 'when user is invite by the owner' do
before do before do
create :invite, email: invite_user.email, dossier: dossier, user: invite_user, type: 'InviteUser' create :invite, email: invite_user.email, dossier: dossier, user: invite_user
sign_in invite_user sign_in invite_user
end end

View file

@ -1,24 +0,0 @@
FactoryBot.define do
factory :invite_user do
email { 'plop@octo.com' }
after(:build) do |invite, _evaluator|
if invite.dossier.nil?
invite.dossier = create(:dossier)
end
if invite.user.present?
invite.email = invite.user.email
end
end
trait :with_user do
after(:build) do |invite, _evaluator|
if invite.user.nil?
invite.user = create(:user)
invite.email = invite.user.email
end
end
end
end
end

View file

@ -5,7 +5,7 @@ feature 'Invitations' do
let(:owner) { create(:user) } let(:owner) { create(:user) }
let(:invited_user) { create(:user, email: 'user_invite@exemple.fr') } let(:invited_user) { create(:user, email: 'user_invite@exemple.fr') }
let(:procedure) { create(:simple_procedure) } let(:procedure) { create(:simple_procedure) }
let(:invite) { create(:invite_user, user: invited_user, dossier: dossier) } let(:invite) { create(:invite, user: invited_user, dossier: dossier) }
context 'when the dossier is a brouillon' do context 'when the dossier is a brouillon' do
let!(:dossier) { create(:dossier, :for_individual, state: Dossier.states.fetch(:brouillon), user: owner, procedure: procedure) } let!(:dossier) { create(:dossier, :for_individual, state: Dossier.states.fetch(:brouillon), user: owner, procedure: procedure) }
@ -26,7 +26,7 @@ feature 'Invitations' do
end end
context 'when inviting someone without an existing account' do context 'when inviting someone without an existing account' do
let(:invite) { create(:invite_user, dossier: dossier, user: nil) } let(:invite) { create(:invite, dossier: dossier, user: nil) }
let(:user_password) { 'l33tus3r' } let(:user_password) { 'l33tus3r' }
scenario 'an invited user can register using the registration link sent in the invitation email' do scenario 'an invited user can register using the registration link sent in the invitation email' do

View file

@ -33,7 +33,7 @@ RSpec.describe CommentaireHelper, type: :helper do
describe '.commentaire_is_from_guest' do describe '.commentaire_is_from_guest' do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier) }
let!(:guest) { create(:invite_user, dossier: dossier) } let!(:guest) { create(:invite, dossier: dossier) }
subject { commentaire_is_from_guest(commentaire) } subject { commentaire_is_from_guest(commentaire) }

View file

@ -66,7 +66,7 @@ describe User, type: :model do
context 'when user was invited by user' do context 'when user was invited by user' do
before do before do
create(:invite, dossier: dossier, user: invite_user, type: 'InviteUser') create(:invite, dossier: dossier, user: invite_user)
end end
let(:user) { invite_user } let(:user) { invite_user }
@ -116,7 +116,7 @@ describe User, type: :model do
context 'when user was invited by user' do context 'when user was invited by user' do
before do before do
create(:invite, dossier: dossier, user: invite_user, type: 'InviteUser') create(:invite, dossier: dossier, user: invite_user)
end end
let(:user) { invite_user } let(:user) { invite_user }