reset procedure_presentation if a pb occurs with a column deserialization
This commit is contained in:
parent
112d49cb51
commit
16e93a217b
5 changed files with 51 additions and 27 deletions
|
@ -10,28 +10,31 @@ class AssignTo < ApplicationRecord
|
||||||
|
|
||||||
def procedure_presentation_or_default_and_errors
|
def procedure_presentation_or_default_and_errors
|
||||||
errors = reset_procedure_presentation_if_invalid
|
errors = reset_procedure_presentation_if_invalid
|
||||||
|
|
||||||
if self.procedure_presentation.nil?
|
if self.procedure_presentation.nil?
|
||||||
self.procedure_presentation = build_procedure_presentation
|
self.procedure_presentation = create_procedure_presentation!
|
||||||
self.procedure_presentation.save if procedure_presentation.valid? && !procedure_presentation.persisted?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
[self.procedure_presentation, errors]
|
[self.procedure_presentation, errors]
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def reset_procedure_presentation_if_invalid
|
def reset_procedure_presentation_if_invalid
|
||||||
if procedure_presentation&.invalid?
|
errors = begin
|
||||||
# This is a last defense against invalid `ProcedurePresentation`s persistently
|
procedure_presentation.errors if procedure_presentation&.invalid?
|
||||||
# hindering instructeurs. Whenever this gets triggered, it means that there is
|
rescue ActiveRecord::RecordNotFound => e
|
||||||
# a bug somewhere else that we need to fix.
|
[e.message]
|
||||||
|
end
|
||||||
|
|
||||||
errors = procedure_presentation.errors
|
if errors.present?
|
||||||
Sentry.capture_message(
|
Sentry.capture_message(
|
||||||
"Destroying invalid ProcedurePresentation",
|
"Destroying invalid ProcedurePresentation",
|
||||||
extra: { procedure_presentation: procedure_presentation.as_json }
|
extra: { procedure_presentation_id: procedure_presentation.id, errors: }
|
||||||
)
|
)
|
||||||
self.procedure_presentation = nil
|
self.procedure_presentation = nil
|
||||||
errors
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
errors
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Column
|
class Column
|
||||||
|
# include validations to enable procedure_presentation.validate_associate,
|
||||||
|
# which enforces the deserialization of columns in the displayed_columns attribute
|
||||||
|
# and raises an error if a column is not found
|
||||||
|
include ActiveModel::Validations
|
||||||
|
|
||||||
TYPE_DE_CHAMP_TABLE = 'type_de_champ'
|
TYPE_DE_CHAMP_TABLE = 'type_de_champ'
|
||||||
|
|
||||||
attr_reader :table, :column, :label, :type, :scope, :value_column, :filterable, :displayable
|
attr_reader :table, :column, :label, :type, :scope, :value_column, :filterable, :displayable
|
||||||
|
|
|
@ -24,8 +24,8 @@ class ProcedurePresentation < ApplicationRecord
|
||||||
|
|
||||||
before_create { self.displayed_columns = procedure.default_displayed_columns }
|
before_create { self.displayed_columns = procedure.default_displayed_columns }
|
||||||
|
|
||||||
validates_associated :a_suivre_filters, :suivis_filters, :traites_filters,
|
validates_associated :displayed_columns, :sorted_column, :a_suivre_filters, :suivis_filters,
|
||||||
:tous_filters, :supprimes_filters, :expirant_filters, :archives_filters
|
:traites_filters, :tous_filters, :supprimes_filters, :expirant_filters, :archives_filters
|
||||||
|
|
||||||
def filters_for(statut)
|
def filters_for(statut)
|
||||||
send(filters_name_for(statut))
|
send(filters_name_for(statut))
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class SortedColumn
|
class SortedColumn
|
||||||
|
# include validations to enable procedure_presentation.validate_associate,
|
||||||
|
# which enforces the deserialization of columns in the sorted_column attribute
|
||||||
|
# and raises an error if a column is not found
|
||||||
|
include ActiveModel::Validations
|
||||||
|
|
||||||
attr_reader :column, :order
|
attr_reader :column, :order
|
||||||
|
|
||||||
def initialize(column:, order:)
|
def initialize(column:, order:)
|
||||||
|
|
|
@ -9,32 +9,43 @@ describe AssignTo, type: :model do
|
||||||
let(:procedure_presentation_or_default) { procedure_presentation_and_errors.first }
|
let(:procedure_presentation_or_default) { procedure_presentation_and_errors.first }
|
||||||
let(:errors) { procedure_presentation_and_errors.second }
|
let(:errors) { procedure_presentation_and_errors.second }
|
||||||
|
|
||||||
context "without a procedure_presentation" do
|
context "without a preexisting procedure_presentation" do
|
||||||
it { expect(procedure_presentation_or_default).to be_persisted }
|
it 'creates a default pp' do
|
||||||
it { expect(procedure_presentation_or_default).to be_valid }
|
expect(procedure_presentation_or_default).to be_persisted
|
||||||
it { expect(errors).to be_nil }
|
expect(procedure_presentation_or_default).to be_valid
|
||||||
|
expect(errors).to be_nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a procedure_presentation" do
|
context "with a preexisting procedure_presentation" do
|
||||||
let!(:procedure_presentation) { ProcedurePresentation.create(assign_to: assign_to) }
|
let!(:procedure_presentation) { ProcedurePresentation.create(assign_to:) }
|
||||||
|
|
||||||
it { expect(procedure_presentation_or_default).to eq(procedure_presentation) }
|
it 'returns the preexisting pp' do
|
||||||
it { expect(procedure_presentation_or_default).to be_valid }
|
expect(procedure_presentation_or_default).to eq(procedure_presentation)
|
||||||
it { expect(errors).to be_nil }
|
expect(procedure_presentation_or_default).to be_valid
|
||||||
|
expect(errors).to be_nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with an invalid procedure_presentation" do
|
context "with an invalid procedure_presentation" do
|
||||||
let!(:procedure_presentation) do
|
let!(:procedure_presentation) do
|
||||||
pp = ProcedurePresentation.new(assign_to: assign_to, displayed_fields: [{ 'table' => 'invalid', 'column' => 'random' }])
|
pp = ProcedurePresentation.create(assign_to: assign_to)
|
||||||
pp.save(validate: false)
|
|
||||||
pp
|
sql = <<-SQL.squish
|
||||||
|
UPDATE procedure_presentations
|
||||||
|
SET displayed_columns = ARRAY['{\"procedure_id\":666}'::jsonb]
|
||||||
|
WHERE id = #{pp.id} ;
|
||||||
|
SQL
|
||||||
|
|
||||||
|
pp.class.connection.execute(sql)
|
||||||
|
|
||||||
|
assign_to.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect(procedure_presentation_or_default).to be_persisted }
|
|
||||||
it { expect(procedure_presentation_or_default).to be_valid }
|
|
||||||
it { expect(errors).to be_present }
|
|
||||||
it do
|
it do
|
||||||
procedure_presentation_or_default
|
expect(procedure_presentation_or_default).to be_persisted
|
||||||
|
expect(procedure_presentation_or_default).to be_valid
|
||||||
|
expect(errors).to be_present
|
||||||
expect(assign_to.procedure_presentation).not_to be(procedure_presentation)
|
expect(assign_to.procedure_presentation).not_to be(procedure_presentation)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue