From e3697bd9761641e9efe8bfecde68fe652765541e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 7 Oct 2024 15:01:40 +0200 Subject: [PATCH] colonne.id = { procedure_id:, column_id: }.to_json because: - id should be a string as other id - id need procedure_id to allow ColumnType.deserialize(id) -> Column as the columns are built by a procedure --- .../instructeurs/procedures_controller.rb | 2 +- app/models/column.rb | 10 +++------- app/models/concerns/columns_concern.rb | 5 ++++- app/models/procedure_presentation.rb | 14 +++++++++----- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index f8a332189..302bde7b1 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -158,7 +158,7 @@ module Instructeurs @statut = statut @procedure = procedure @procedure_presentation = procedure_presentation - @column = procedure.find_column(id: params[:column]) + @column = procedure.find_column(h_id: JSON.parse(params[:column], symbolize_names: true)) end def remove_filter diff --git a/app/models/column.rb b/app/models/column.rb index 240a11937..a7f74fa69 100644 --- a/app/models/column.rb +++ b/app/models/column.rb @@ -18,13 +18,9 @@ class Column @displayable = displayable end - def id - "#{table}/#{column}" - end - - def ==(other) - other.to_json == to_json - end + def id = h_id.to_json + def h_id = { procedure_id: @procedure_id, column_id: "#{table}/#{column}" } + def ==(other) = h_id == other.h_id # using h_id instead of id to avoid inversion of keys def to_json { diff --git a/app/models/concerns/columns_concern.rb b/app/models/concerns/columns_concern.rb index 772f5bc76..ea88cb73a 100644 --- a/app/models/concerns/columns_concern.rb +++ b/app/models/concerns/columns_concern.rb @@ -4,7 +4,10 @@ module ColumnsConcern extend ActiveSupport::Concern included do - def find_column(id:) = columns.find { |f| f.id == id } + def find_column(h_id: nil, label: nil) + return columns.find { _1.h_id == h_id } if h_id.present? + return columns.find { _1.label == label } if label.present? + end def columns columns = dossier_columns diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index de6d162d4..7cd4e0f48 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -81,8 +81,10 @@ class ProcedurePresentation < ApplicationRecord end def add_filter(statut, column_id, value) + h_id = JSON.parse(column_id, symbolize_names: true) + if value.present? - column = procedure.find_column(id: column_id) + column = procedure.find_column(h_id:) case column.table when TYPE_DE_CHAMP @@ -103,7 +105,8 @@ class ProcedurePresentation < ApplicationRecord end def remove_filter(statut, column_id, value) - column = procedure.find_column(id: column_id) + h_id = JSON.parse(column_id, symbolize_names: true) + column = procedure.find_column(h_id:) updated_filters = filters.dup updated_filters[statut] = filters[statut].reject do |filter| @@ -114,8 +117,8 @@ class ProcedurePresentation < ApplicationRecord end def update_displayed_fields(column_ids) - column_ids = Array.wrap(column_ids) - columns = column_ids.map { |id| procedure.find_column(id:) } + 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) @@ -125,7 +128,8 @@ class ProcedurePresentation < ApplicationRecord end def update_sort(column_id, order) - column = procedure.find_column(id: column_id) + h_id = JSON.parse(column_id, symbolize_names: true) + column = procedure.find_column(h_id:) update!(sort: { TABLE => column.table,