From 9904dabffefbc3ddf010c21d9b820ba429e1b76c Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 5 Apr 2022 18:11:12 +0200 Subject: [PATCH] feat(procedure_presentation): pass filters as serialized snapshot --- app/models/export.rb | 33 +++++++++++++------ app/models/procedure_presentation.rb | 4 +++ ...cedure_presentation_snapshot_to_exports.rb | 5 +++ db/schema.rb | 3 +- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20220405163206_add_procedure_presentation_snapshot_to_exports.rb diff --git a/app/models/export.rb b/app/models/export.rb index f97673248..78e595546 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -2,14 +2,15 @@ # # Table name: exports # -# id :bigint not null, primary key -# format :string not null -# key :text not null -# statut :string default("tous") -# time_span_type :string default("everything"), not null -# created_at :datetime not null -# updated_at :datetime not null -# procedure_presentation_id :bigint +# id :bigint not null, primary key +# format :string not null +# key :text not null +# procedure_presentation_snapshot :jsonb +# statut :string default("tous") +# time_span_type :string default("everything"), not null +# created_at :datetime not null +# updated_at :datetime not null +# procedure_presentation_id :bigint # class Export < ApplicationRecord MAX_DUREE_CONSERVATION_EXPORT = 3.hours @@ -60,6 +61,8 @@ class Export < ApplicationRecord end def compute + load_snapshot! + file.attach( io: io, filename: filename, @@ -78,7 +81,11 @@ class Export < ApplicationRecord end def old? - updated_at < 20.minutes.ago + updated_at < 20.minutes.ago || filters_changed? + end + + def filters_changed? + procedure_presentation&.snapshot != procedure_presentation_snapshot end def filtered? @@ -98,7 +105,7 @@ class Export < ApplicationRecord end def self.find_or_create_export(format, groupe_instructeurs, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) - create_with(groupe_instructeurs: groupe_instructeurs, procedure_presentation: procedure_presentation) + create_with(groupe_instructeurs: groupe_instructeurs, procedure_presentation: procedure_presentation, procedure_presentation_snapshot: procedure_presentation&.snapshot) .includes(:procedure_presentation) .create_or_find_by(format: format, time_span_type: time_span_type, @@ -147,6 +154,12 @@ class Export < ApplicationRecord private + def load_snapshot! + if procedure_presentation_snapshot.present? + procedure_presentation.attributes = procedure_presentation_snapshot + end + end + def dossiers_for_export @dossiers_for_export ||= begin dossiers = Dossier.where(groupe_instructeur: groupe_instructeurs) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 5eaf64138..4382a02b5 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -263,6 +263,10 @@ class ProcedurePresentation < ApplicationRecord }) end + def snapshot + slice(:filters, :sort, :displayed_fields) + end + private def field_id(field) diff --git a/db/migrate/20220405163206_add_procedure_presentation_snapshot_to_exports.rb b/db/migrate/20220405163206_add_procedure_presentation_snapshot_to_exports.rb new file mode 100644 index 000000000..2ac3dd06b --- /dev/null +++ b/db/migrate/20220405163206_add_procedure_presentation_snapshot_to_exports.rb @@ -0,0 +1,5 @@ +class AddProcedurePresentationSnapshotToExports < ActiveRecord::Migration[6.1] + def change + add_column :exports, :procedure_presentation_snapshot, :jsonb + end +end diff --git a/db/schema.rb b/db/schema.rb index 36b33644d..4cff9290a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_05_110354) do +ActiveRecord::Schema.define(version: 2022_04_05_163206) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -432,6 +432,7 @@ ActiveRecord::Schema.define(version: 2022_04_05_110354) do t.string "format", null: false t.text "key", null: false t.bigint "procedure_presentation_id" + t.jsonb "procedure_presentation_snapshot" t.string "statut", default: "tous" t.string "time_span_type", default: "everything", null: false t.datetime "updated_at", null: false