From b0c047762249d8c4b677540fbaa46e8827deb5fe Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 19 Jul 2017 15:40:26 +0200 Subject: [PATCH] [Fix #315] Order champs by procedure order in export --- app/models/dossier.rb | 8 ++--- .../api/v1/dossiers_controller_spec.rb | 8 ++--- spec/factories/type_de_champ_private.rb | 4 +-- spec/factories/type_de_champ_public.rb | 4 +-- spec/models/dossier_spec.rb | 2 +- spec/models/preference_list_dossier_spec.rb | 4 +-- spec/models/procedure_spec.rb | 33 +++++++++++++++++++ 7 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 61b7768c6..50b9f1ed3 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -242,8 +242,8 @@ class Dossier < ActiveRecord::Base def data_with_champs serialized_dossier = DossierTableExportSerializer.new(self) data = serialized_dossier.attributes.values - data += self.champs.order('type_de_champ_id ASC').map(&:value) - data += self.champs_private.order('type_de_champ_id ASC').map(&:value) + data += self.ordered_champs.map(&:value) + data += self.ordered_champs_private.map(&:value) data += self.export_entreprise_data.values return data end @@ -251,8 +251,8 @@ class Dossier < ActiveRecord::Base def export_headers serialized_dossier = DossierTableExportSerializer.new(self) headers = serialized_dossier.attributes.keys - headers += self.procedure.types_de_champ.order('id ASC').map { |types_de_champ| types_de_champ.libelle.parameterize.underscore.to_sym } - headers += self.procedure.types_de_champ_private.order('id ASC').map { |types_de_champ| types_de_champ.libelle.parameterize.underscore.to_sym } + headers += self.procedure.types_de_champ.order(:order_place).map { |types_de_champ| types_de_champ.libelle.parameterize.underscore.to_sym } + headers += self.procedure.types_de_champ_private.order(:order_place).map { |types_de_champ| types_de_champ.libelle.parameterize.underscore.to_sym } headers += self.export_entreprise_data.keys return headers end diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index efb1d8cf2..937b1e662 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -227,8 +227,8 @@ describe API::V1::DossiersController do subject { super()[:type_de_champ] } it { expect(subject.keys.include?(:id)).to be_truthy } - it { expect(subject[:libelle]).to eq('Description') } - it { expect(subject[:description]).to eq('description de votre projet') } + it { expect(subject[:libelle]).to include('Libelle du champ') } + it { expect(subject[:description]).to include('description du champ') } it { expect(subject.keys.include?(:order_place)).to be_truthy } it { expect(subject[:type_champ]).to eq('text') } end @@ -260,8 +260,8 @@ describe API::V1::DossiersController do subject { super()[:type_de_champ] } it { expect(subject.keys.include?(:id)).to be_truthy } - it { expect(subject[:libelle]).to eq('Description') } - it { expect(subject[:description]).to eq('description de votre projet') } + it { expect(subject[:libelle]).to include('Libelle champ privé') } + it { expect(subject[:description]).to include('description du champ privé') } it { expect(subject.keys.include?(:order_place)).to be_truthy } it { expect(subject[:type_champ]).to eq('text') } end diff --git a/spec/factories/type_de_champ_private.rb b/spec/factories/type_de_champ_private.rb index 356028d91..327a9e80f 100644 --- a/spec/factories/type_de_champ_private.rb +++ b/spec/factories/type_de_champ_private.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :type_de_champ_private do - libelle 'Description' - description 'description de votre projet' + sequence(:libelle) { |n| "Libelle champ privé #{n}" } + sequence(:description) { |n| "description du champ privé #{n}" } type_champ 'text' order_place 1 mandatory false diff --git a/spec/factories/type_de_champ_public.rb b/spec/factories/type_de_champ_public.rb index 307ec2077..3e1acf1e8 100644 --- a/spec/factories/type_de_champ_public.rb +++ b/spec/factories/type_de_champ_public.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :type_de_champ_public do - libelle 'Description' - description 'description de votre projet' + sequence(:libelle) { |n| "Libelle du champ #{n}" } + sequence(:description) { |n| "description du champ #{n}" } type_champ 'text' order_place 1 mandatory false diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 7caa81911..a8f4b16ad 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -463,7 +463,7 @@ describe Dossier do describe '#export_headers' do subject { dossier.export_headers } - it { expect(subject).to include(:description) } + it { expect(subject).to include(dossier.champs.first.libelle.parameterize.underscore.to_sym) } it { expect(subject).to include(:individual_gender) } it { expect(subject).to include(:individual_nom) } it { expect(subject).to include(:individual_prenom) } diff --git a/spec/models/preference_list_dossier_spec.rb b/spec/models/preference_list_dossier_spec.rb index 9ddb467f5..000ad7ff7 100644 --- a/spec/models/preference_list_dossier_spec.rb +++ b/spec/models/preference_list_dossier_spec.rb @@ -306,7 +306,7 @@ describe PreferenceListDossier do describe 'first champs' do subject { super()["type_de_champ_#{procedure.types_de_champ.first.id}"] } - it { expect(subject[:libelle]).to eq 'Description' } + it { expect(subject[:libelle]).to eq procedure.types_de_champ.first.libelle } it { expect(subject[:table]).to eq 'champs' } it { expect(subject[:attr]).to eq procedure.types_de_champ.first.id } it { expect(subject[:attr_decorate]).to eq 'value' } @@ -324,7 +324,7 @@ describe PreferenceListDossier do describe 'first champs' do subject { super()["type_de_champ_private_#{procedure.types_de_champ_private.first.id}"] } - it { expect(subject[:libelle]).to eq 'Description' } + it { expect(subject[:libelle]).to eq procedure.types_de_champ_private.first.libelle } it { expect(subject[:table]).to eq 'champs_private' } it { expect(subject[:attr]).to eq procedure.types_de_champ_private.first.id } it { expect(subject[:attr_decorate]).to eq 'value' } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 07dd724e9..249b21931 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -336,6 +336,39 @@ describe Procedure do it { expect(subject[:data].size).to eq(2) } it { expect(subject[:headers]).to eq(dossier.export_headers) } + + context 'with ordered champs' do + let(:tc_2) { create(:type_de_champ_public, order_place: 2) } + let(:tc_1) { create(:type_de_champ_public, order_place: 1) } + let(:tcp_2) { create(:type_de_champ_private, order_place: 2) } + let(:tcp_1) { create(:type_de_champ_private, order_place: 1) } + + before do + procedure.types_de_champ << tc_2 << tc_1 + procedure.types_de_champ_private << tcp_2 << tcp_1 + + dossier.build_default_champs + dossier.champs.find_by(type_de_champ: tc_1).update_attributes(value: "value 1") + dossier.champs.find_by(type_de_champ: tc_2).update_attributes(value: "value 2") + dossier.champs_private.find_by(type_de_champ: tcp_1).update_attributes(value: "private value 1") + dossier.champs_private.find_by(type_de_champ: tcp_2).update_attributes(value: "private value 2") + + dossier2.build_default_champs + dossier2.champs.find_by(type_de_champ: tc_1).update_attributes(value: "value 1") + dossier2.champs.find_by(type_de_champ: tc_2).update_attributes(value: "value 2") + dossier2.champs_private.find_by(type_de_champ: tcp_1).update_attributes(value: "private value 1") + dossier2.champs_private.find_by(type_de_champ: tcp_2).update_attributes(value: "private value 2") + end + + it { expect(subject[:headers].index(tc_1.libelle.parameterize.underscore.to_sym)).to be < subject[:headers].index(tc_2.libelle.parameterize.underscore.to_sym) } + it { expect(subject[:headers].index(tcp_1.libelle.parameterize.underscore.to_sym)).to be < subject[:headers].index(tcp_2.libelle.parameterize.underscore.to_sym) } + + it { expect(subject[:data][0].index("value 1")).to be < subject[:data].first.index("value 2") } + it { expect(subject[:data][0].index("private value 1")).to be < subject[:data].first.index("private value 2") } + + it { expect(subject[:data][1].index("value 1")).to be < subject[:data].first.index("value 2") } + it { expect(subject[:data][1].index("private value 1")).to be < subject[:data].first.index("private value 2") } + end end context 'when there is a draft dossier' do