Add search method on dossier model

This commit is contained in:
Xavier J 2015-11-16 15:58:35 +01:00
parent dd8bafab49
commit de356ae0ab
2 changed files with 107 additions and 29 deletions

View file

@ -128,6 +128,29 @@ class Dossier < ActiveRecord::Base
Dossier.joins(:procedure).where("state='closed' AND dossiers.procedure_id = procedures.id AND procedures.administrateur_id = #{current_gestionnaire.administrateur_id}").order('updated_at ASC')
end
def self.search terms
return if terms.blank?
dossiers = Dossier.arel_table
users = User.arel_table
etablissements = Etablissement.arel_table
entreprises = Entreprise.arel_table
composed_scope = self.joins('LEFT OUTER JOIN users ON users.id = dossiers.user_id')
.joins('LEFT OUTER JOIN entreprises ON entreprises.dossier_id = dossiers.id')
.joins('LEFT OUTER JOIN etablissements ON etablissements.dossier_id = dossiers.id')
terms.split.each do |word|
query_string = "%#{word}%"
query_string_start_with = "#{word}%"
composed_scope = composed_scope.where(
dossiers[:nom_projet].matches(query_string).or\
users[:email].matches(query_string).or\
dossiers[:id].eq(word).or\
etablissements[:siret].matches(query_string_start_with).or\
entreprises[:raison_sociale].matches(query_string)
)
end
composed_scope
end
private
def build_default_cerfa

View file

@ -72,7 +72,7 @@ describe Dossier do
end
describe '#retrieve_piece_justificative_by_type' do
let(:all_dossier_pj_id){dossier.procedure.types_de_piece_justificative}
let(:all_dossier_pj_id) { dossier.procedure.types_de_piece_justificative }
subject { dossier.retrieve_piece_justificative_by_type all_dossier_pj_id.first }
before do
dossier.build_default_pieces_justificatives
@ -137,12 +137,12 @@ describe Dossier do
context 'when action is not valid' do
let(:action) { 'test' }
it { expect{ subject }.to raise_error('action is not valid') }
it { expect { subject }.to raise_error('action is not valid') }
end
context 'when role is not valid' do
let(:role) { 'test' }
it { expect{ subject }.to raise_error('role is not valid') }
it { expect { subject }.to raise_error('role is not valid') }
end
context 'when dossier is at state draft' do
@ -154,13 +154,13 @@ describe Dossier do
let(:role) { 'user' }
context 'when he updates dossier informations' do
let(:action) {'update'}
let(:action) { 'update' }
it { is_expected.to eq('draft') }
end
context 'when he posts a comment' do
let(:action) {'comment'}
let(:action) { 'comment' }
it { is_expected.to eq('draft') }
end
@ -184,13 +184,13 @@ describe Dossier do
context 'when is update dossier informations' do
let(:action) { 'update' }
it {is_expected.to eq('initiated')}
it { is_expected.to eq('initiated') }
end
context 'when is post a comment' do
let(:action) { 'comment' }
it {is_expected.to eq('initiated')}
it { is_expected.to eq('initiated') }
end
end
@ -200,13 +200,13 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('replied')}
it { is_expected.to eq('replied') }
end
context 'when is validated the dossier' do
let(:action) { 'valid' }
it {is_expected.to eq('validated')}
it { is_expected.to eq('validated') }
end
end
end
@ -241,13 +241,13 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('replied')}
it { is_expected.to eq('replied') }
end
context 'when is validated the dossier' do
let(:action) { 'valid' }
it {is_expected.to eq('validated')}
it { is_expected.to eq('validated') }
end
end
end
@ -263,13 +263,13 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('updated')}
it { is_expected.to eq('updated') }
end
context 'when is updated dossier informations' do
let(:action) { 'update' }
it { is_expected.to eq('updated')}
it { is_expected.to eq('updated') }
end
end
@ -279,13 +279,13 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('replied')}
it { is_expected.to eq('replied') }
end
context 'when is validated the dossier' do
let(:action) { 'valid' }
it {is_expected.to eq('validated')}
it { is_expected.to eq('validated') }
end
end
end
@ -316,7 +316,7 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('validated')}
it { is_expected.to eq('validated') }
end
end
end
@ -342,13 +342,13 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it {is_expected.to eq('submitted')}
it { is_expected.to eq('submitted') }
end
context 'when is closed the dossier' do
let(:action) { 'close' }
it {is_expected.to eq('closed')}
it { is_expected.to eq('closed') }
end
end
end
@ -364,7 +364,7 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('closed')}
it { is_expected.to eq('closed') }
end
end
@ -374,7 +374,7 @@ describe Dossier do
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('closed')}
it { is_expected.to eq('closed') }
end
end
end
@ -388,15 +388,15 @@ describe Dossier do
let(:procedure_admin) { create(:procedure, administrateur: admin) }
let(:procedure_admin_2) { create(:procedure, administrateur: admin_2) }
let!(:dossier1) { create(:dossier, :with_user, procedure: procedure_admin, state: 'draft')}
let!(:dossier2) { create(:dossier, :with_user, procedure: procedure_admin, state: 'initiated')} #a_traiter
let!(:dossier3) { create(:dossier, :with_user, procedure: procedure_admin, state: 'initiated')} #a_traiter
let!(:dossier4) { create(:dossier, :with_user, procedure: procedure_admin, state: 'replied')} #en_attente
let!(:dossier5) { create(:dossier, :with_user, procedure: procedure_admin, state: 'updated')} #a_traiter
let!(:dossier6) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'validated')} #en_attente
let!(:dossier7) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'submitted')} #a_traiter
let!(:dossier8) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'closed')} #termine
let!(:dossier9) { create(:dossier, :with_user, procedure: procedure_admin, state: 'closed')} #termine
let!(:dossier1) { create(:dossier, :with_user, procedure: procedure_admin, state: 'draft') }
let!(:dossier2) { create(:dossier, :with_user, procedure: procedure_admin, state: 'initiated') } #a_traiter
let!(:dossier3) { create(:dossier, :with_user, procedure: procedure_admin, state: 'initiated') } #a_traiter
let!(:dossier4) { create(:dossier, :with_user, procedure: procedure_admin, state: 'replied') } #en_attente
let!(:dossier5) { create(:dossier, :with_user, procedure: procedure_admin, state: 'updated') } #a_traiter
let!(:dossier6) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'validated') } #en_attente
let!(:dossier7) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'submitted') } #a_traiter
let!(:dossier8) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'closed') } #termine
let!(:dossier9) { create(:dossier, :with_user, procedure: procedure_admin, state: 'closed') } #termine
describe '#a_traiter' do
@ -417,5 +417,60 @@ describe Dossier do
it { expect(subject.size).to eq(1) }
end
end
describe '.search' do
subject { described_class.search(terms) }
let!(:dossier_1) { create(:dossier, nom_projet: 'Projet de test', user: create(:user, email: 'contact@test.com')) }
let!(:dossier_2) { create(:dossier, nom_projet: 'Lili et Marcel', user: create(:user, email: 'plop@gmail.com')) }
let!(:dossier_3) { create(:dossier, nom_projet: 'Construction projet marcel', user: create(:user, email: 'peace@clap.fr')) }
let!(:etablissement_1) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Academy', dossier: dossier_1), dossier: dossier_1, siret: '41636169600051') }
let!(:etablissement_2) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'Plop octo', dossier: dossier_2), dossier: dossier_2, siret: '41816602300012') }
let!(:etablissement_3) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Technology', dossier: dossier_3), dossier: dossier_3, siret: '41816609600051') }
describe 'search is empty' do
let(:terms) { '' }
it { expect(subject).to eq(nil) }
end
describe 'search on file title' do
let(:terms) { 'Marcel' }
it { expect(subject.size).to eq(2) }
end
describe 'search on contact email' do
let(:terms) { 'clap' }
it { expect(subject.size).to eq(1) }
end
describe 'search on ID dossier' do
let(:terms) { "#{dossier_2.id}" }
it { expect(subject.size).to eq(1) }
end
describe 'search on SIRET' do
let(:terms) { '4181' }
it { expect(subject.size).to eq(2) }
end
describe 'search on raison social' do
let(:terms) { 'OCTO' }
it { expect(subject.size).to eq(3) }
end
describe 'search on multiple fields' do
let(:terms) { 'octo peace' }
it { expect(subject.size).to eq(1) }
end
end
end
end