diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 55f3148a8..3b4a9f522 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -58,7 +58,7 @@ class Dossier < ActiveRecord::Base scope :nouveaux, -> { not_archived.state_nouveaux } scope :en_instruction, -> { not_archived.state_en_instruction } scope :termine, -> { not_archived.state_termine } - scope :downloadable, -> { state_not_brouillon.includes(:entreprise, :etablissement, :champs, :champs_private) } + scope :downloadable_sorted, -> { state_not_brouillon.includes(:entreprise, :etablissement, :champs, :champs_private).order(initiated_at: 'asc') } scope :en_cours, -> { not_archived.state_en_construction_ou_instruction } scope :without_followers, -> { left_outer_joins(:follows).where(follows: { id: nil }) } scope :with_unread_notifications, -> { where(notifications: { already_read: false }) } diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 62502233e..38b696087 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -151,7 +151,7 @@ class Procedure < ActiveRecord::Base end def generate_export - exportable_dossiers = dossiers.downloadable + exportable_dossiers = dossiers.downloadable_sorted headers = exportable_dossiers.any? ? exportable_dossiers.first.export_headers : [] data = exportable_dossiers.any? ? exportable_dossiers.map { |d| d.full_data_strings_array } : [[]] diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index e59766092..047ff9671 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -758,19 +758,16 @@ describe Dossier do end end - describe '.downloadable' do + describe '.downloadable_sorted' do let(:procedure) { create(:procedure) } let!(:dossier) { create(:dossier, :with_entreprise, procedure: procedure, state: :draft) } - let!(:dossier2) { create(:dossier, :with_entreprise, procedure: procedure, state: :initiated) } - let!(:dossier3) { create(:dossier, :with_entreprise, procedure: procedure, state: :received) } - let!(:dossier4) { create(:dossier, :with_entreprise, procedure: procedure, state: :received, archived: true) } + let!(:dossier2) { create(:dossier, :with_entreprise, procedure: procedure, state: :initiated, initiated_at: DateTime.parse('03/01/2010')) } + let!(:dossier3) { create(:dossier, :with_entreprise, procedure: procedure, state: :received, initiated_at: DateTime.parse('01/01/2010')) } + let!(:dossier4) { create(:dossier, :with_entreprise, procedure: procedure, state: :received, archived: true, initiated_at: DateTime.parse('02/01/2010')) } - subject { procedure.dossiers.downloadable } + subject { procedure.dossiers.downloadable_sorted } - it { is_expected.not_to include(dossier)} - it { is_expected.to include(dossier2)} - it { is_expected.to include(dossier3)} - it { is_expected.to include(dossier4)} + it { is_expected.to match([dossier3, dossier4, dossier2])} end describe "#send_dossier_received" do