feat: add empty_brouillon scope
This commit is contained in:
parent
0e95381002
commit
0137763d42
3 changed files with 57 additions and 0 deletions
29
app/models/concerns/dossier_empty_concern.rb
Normal file
29
app/models/concerns/dossier_empty_concern.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module DossierEmptyConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
scope :empty_brouillon, -> (created_at) do
|
||||
dossiers_ids = Dossier.brouillon.where(created_at:).ids
|
||||
|
||||
dossiers_with_value = Dossier.select('id').includes(:champs)
|
||||
.where.not(champs: { value: nil })
|
||||
.where(id: dossiers_ids)
|
||||
|
||||
dossier_with_geo_areas = Dossier.select('id').includes(champs: :geo_areas)
|
||||
.where.not(geo_areas: { id: nil })
|
||||
.where(id: dossiers_ids)
|
||||
|
||||
dossier_with_pj = Dossier.select('id')
|
||||
.joins(champs: :piece_justificative_file_attachments)
|
||||
.where(id: dossiers_ids)
|
||||
|
||||
brouillon
|
||||
.where.not(id: dossiers_with_value)
|
||||
.where.not(id: dossier_with_geo_areas)
|
||||
.where.not(id: dossier_with_pj)
|
||||
.where(id: dossiers_ids)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -12,6 +12,7 @@ class Dossier < ApplicationRecord
|
|||
include DossierSectionsConcern
|
||||
include DossierStateConcern
|
||||
include DossierChampsConcern
|
||||
include DossierEmptyConcern
|
||||
|
||||
enum state: {
|
||||
brouillon: 'brouillon',
|
||||
|
|
27
spec/models/concerns/dossier_empty_concern_spec.rb
Normal file
27
spec/models/concerns/dossier_empty_concern_spec.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe DossierEmptyConcern do
|
||||
describe 'empty_brouillon' do
|
||||
let(:types) { [{ type: :text }, { type: :carte }, { type: :piece_justificative }] }
|
||||
let(:procedure) { create(:procedure, types_de_champ_public: types) }
|
||||
let!(:empty_brouillon) { create(:dossier, procedure:) }
|
||||
let!(:empty_en_construction) { create(:dossier, :en_construction, procedure:) }
|
||||
let!(:value_filled_dossier) { create(:dossier, procedure:) }
|
||||
let!(:carte_filled_dossier) { create(:dossier, procedure:) }
|
||||
let!(:pj_filled_dossier) { create(:dossier, procedure:) }
|
||||
let(:geo_area) { build(:geo_area, :selection_utilisateur, :polygon) }
|
||||
let(:attachment) { { io: StringIO.new("toto"), filename: "toto.png", content_type: "image/png" } }
|
||||
|
||||
subject { Dossier.empty_brouillon(2.days.ago..) }
|
||||
|
||||
before do
|
||||
value_filled_dossier.champs.first.update(value: 'filled')
|
||||
carte_filled_dossier.champs.second.update(geo_areas: [geo_area])
|
||||
pj_filled_dossier.champs.third.piece_justificative_file.attach(attachment)
|
||||
end
|
||||
|
||||
it do
|
||||
is_expected.to eq([empty_brouillon])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue