From 5f6d8e93ca1053e34ba84f74cb0539aea87d9c32 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 7 Oct 2024 22:04:44 +0200 Subject: [PATCH] update_display double write --- app/models/procedure.rb | 8 ------ app/models/procedure_presentation.rb | 8 ++++-- spec/models/procedure_presentation_spec.rb | 29 ++++++++++++++++++++++ spec/models/procedure_spec.rb | 4 --- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 168c486ec..10377d4ff 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -613,14 +613,6 @@ class Procedure < ApplicationRecord end end - def self.default_sort - { - 'table' => 'self', - 'column' => 'id', - 'order' => 'desc' - } - end - def whitelist! touch(:whitelisted_at) end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 59b7470c6..1d2134e9c 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -137,10 +137,14 @@ class ProcedurePresentation < ApplicationRecord h_ids = Array.wrap(column_ids).map { |id| JSON.parse(id, symbolize_names: true) } columns = h_ids.map { |h_id| procedure.find_column(h_id:) } - update!(displayed_fields: columns) + update!( + displayed_fields: columns, + displayed_columns: columns.map(&:h_id) + ) if !sort_to_column_id(sort).in?(column_ids) - update!(sort: Procedure.default_sort) + default_column_id = procedure.dossier_id_column.id + update_sort(default_column_id, "desc") end end diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 87892f1aa..5754b80da 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -944,6 +944,35 @@ describe ProcedurePresentation do end end + describe '#update_displayed_fields' do + let(:procedure_presentation) do + create(:procedure_presentation, assign_to:).tap do |pp| + pp.update_sort(procedure.find_column(label: 'Demandeur').id, 'desc') + end + end + + subject do + procedure_presentation.update_displayed_fields([ + procedure.find_column(label: 'En construction le').id, + procedure.find_column(label: 'Mis à jour le').id + ]) + end + + it 'should update displayed_fields' do + expect(procedure_presentation.displayed_columns).to eq([]) + + subject + + expect(procedure_presentation.displayed_columns).to eq([ + { "column_id" => "self/en_construction_at", "procedure_id" => procedure.id }, + { "column_id" => "self/updated_at", "procedure_id" => procedure.id } + ]) + + expect(procedure_presentation.sorted_column['id']).to eq("column_id" => "self/id", "procedure_id" => procedure.id) + expect(procedure_presentation.sorted_column['order']).to eq('desc') + end + end + describe '#update_sort' do let(:procedure_presentation) { create(:procedure_presentation, assign_to:) } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index cda2f8330..4f580d727 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1450,10 +1450,6 @@ describe Procedure do end end - describe ".default_sort" do - it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) } - end - describe "#organisation_name" do subject { procedure.organisation_name } context 'when the procedure has a service (and no organization)' do