Order types de champ in scope

This commit is contained in:
Paul Chavard 2018-12-20 12:00:27 +01:00
parent bf8fa41020
commit eb9edc1cee
6 changed files with 18 additions and 29 deletions

View file

@ -3,9 +3,9 @@ require Rails.root.join('lib', 'percentile')
class Procedure < ApplicationRecord
MAX_DUREE_CONSERVATION = 36
has_many :types_de_piece_justificative, -> { order "order_place ASC" }, dependent: :destroy
has_many :types_de_champ, -> { public_only }, dependent: :destroy
has_many :types_de_champ_private, -> { private_only }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :types_de_piece_justificative, -> { ordered }, dependent: :destroy
has_many :types_de_champ, -> { public_only.ordered }, dependent: :destroy
has_many :types_de_champ_private, -> { private_only.ordered }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :dossiers
has_many :deleted_dossiers, dependent: :destroy
@ -137,13 +137,8 @@ class Procedure < ApplicationRecord
# Warning: dossier after_save build_default_champs must be removed
# to save a dossier created from this method
def new_dossier
champs = types_de_champ
.ordered
.map { |tdc| tdc.champ.build }
champs_private = types_de_champ_private
.ordered
.map { |tdc| tdc.champ.build }
champs = types_de_champ.map { |tdc| tdc.champ.build }
champs_private = types_de_champ_private.map { |tdc| tdc.champ.build }
Dossier.new(procedure: self, champs: champs, champs_private: champs_private)
end
@ -156,24 +151,16 @@ class Procedure < ApplicationRecord
service&.nom || organisation
end
def types_de_champ_ordered
types_de_champ.order(:order_place)
end
def types_de_champ_private_ordered
types_de_champ_private.order(:order_place)
end
def self.active(id)
publiees.find(id)
end
def switch_types_de_champ(index_of_first_element)
switch_list_order(types_de_champ_ordered, index_of_first_element)
switch_list_order(types_de_champ, index_of_first_element)
end
def switch_types_de_champ_private(index_of_first_element)
switch_list_order(types_de_champ_private_ordered, index_of_first_element)
switch_list_order(types_de_champ_private, index_of_first_element)
end
def switch_types_de_piece_justificative(index_of_first_element)

View file

@ -6,4 +6,5 @@ class TypeDePieceJustificative < ApplicationRecord
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :lien_demarche, format: { with: URI.regexp }, allow_blank: true, allow_nil: true
scope :ordered, -> { order(order_place: :asc) }
end

View file

@ -142,10 +142,10 @@ class ProcedureExportService
headers = ATTRIBUTES.map do |key|
label_for_export(key.to_s)
end
headers += @procedure.types_de_champ.ordered.reject(&:exclude_from_export?).map do |champ|
headers += @procedure.types_de_champ.reject(&:exclude_from_export?).map do |champ|
label_for_export(champ.libelle)
end
headers += @procedure.types_de_champ_private.ordered.reject(&:exclude_from_export?).map do |champ|
headers += @procedure.types_de_champ_private.reject(&:exclude_from_export?).map do |champ|
label_for_export(champ.libelle)
end
headers += ETABLISSEMENT_ATTRIBUTES.map do |key|

View file

@ -36,7 +36,7 @@ class TypesDeChampService
end
def types_de_champ
private? ? @procedure.types_de_champ_private_ordered.decorate : @procedure.types_de_champ_ordered.decorate
private? ? @procedure.types_de_champ_private.decorate : @procedure.types_de_champ.decorate
end
def new_type_de_champ

View file

@ -252,11 +252,11 @@ describe Procedure do
end
end
describe '#types_de_champ_ordered' do
describe '#types_de_champ (ordered)' do
let(:procedure) { create(:procedure) }
let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 1) }
let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 0) }
subject { procedure.types_de_champ_ordered }
subject { procedure.types_de_champ }
it { expect(subject.first).to eq(type_de_champ_1) }
it { expect(subject.last).to eq(type_de_champ_0) }
end
@ -432,8 +432,8 @@ describe Procedure do
end
it 'should keep types_de_champ ids stable' do
expect(subject.types_de_champ_ordered.first.id).not_to eq(procedure.types_de_champ_ordered.first.id)
expect(subject.types_de_champ_ordered.first.stable_id).to eq(procedure.types_de_champ_ordered.first.id)
expect(subject.types_de_champ.first.id).not_to eq(procedure.types_de_champ.first.id)
expect(subject.types_de_champ.first.stable_id).to eq(procedure.types_de_champ.first.id)
end
end

View file

@ -11,10 +11,11 @@ describe ProcedureExportService do
before do
# change one tdc place to check if the header is ordered
tdc_first = procedure.types_de_champ.ordered.first
tdc_last = procedure.types_de_champ.ordered.last
tdc_first = procedure.types_de_champ.first
tdc_last = procedure.types_de_champ.last
tdc_first.update(order_place: tdc_last.order_place + 1)
procedure.reload
end
context 'dossiers' do