From d1de1587d33c1f9209892611338c1bdd04367d1e Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 2 Sep 2020 11:46:12 +0200 Subject: [PATCH 1/3] Always serialize type_de_champ stable_id as id --- .../new_administrateur/types_de_champ_controller.rb | 3 +-- app/models/type_de_champ.rb | 8 ++------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/controllers/new_administrateur/types_de_champ_controller.rb b/app/controllers/new_administrateur/types_de_champ_controller.rb index b88cb1642..ea7023757 100644 --- a/app/controllers/new_administrateur/types_de_champ_controller.rb +++ b/app/controllers/new_administrateur/types_de_champ_controller.rb @@ -44,7 +44,6 @@ module NewAdministrateur { type_de_champ: type_de_champ.as_json( except: [ - :id, :created_at, :options, :order_place, @@ -64,7 +63,7 @@ module NewAdministrateur :piece_justificative_template_url, :quartiers_prioritaires ] - ).merge(id: TypeDeChamp.format_stable_id(type_de_champ.stable_id)) + ) } end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index e0ff6990d..fff12bd04 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -304,7 +304,7 @@ class TypeDeChamp < ApplicationRecord def read_attribute_for_serialization(name) if name == 'id' - self.class.format_stable_id(stable_id) + stable_id else super end @@ -317,15 +317,11 @@ class TypeDeChamp < ApplicationRecord # This is only needed for a clean migration without downtime. We want to ensure # that if editor send a simple id because it was loaded before deployment # we would still do the right thing. - def self.format_stable_id(stable_id) - "stable:#{stable_id}" - end - def self.to_stable_id(id_or_stable_id) if id_or_stable_id.to_s =~ /^stable:/ id_or_stable_id.to_s.gsub(/^stable:/, '') else - find(id_or_stable_id).stable_id + id_or_stable_id end end From 11a1e44fbe227f42dc948fa745b8a68c1f0d3291 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 2 Sep 2020 12:32:44 +0200 Subject: [PATCH 2/3] Remove procedure_id from type_de_champ --- app/models/type_de_champ.rb | 27 +++++++++---------- ...03047_remove_type_de_champ_procedure_id.rb | 5 ++++ db/schema.rb | 4 +-- 3 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20200902103047_remove_type_de_champ_procedure_id.rb diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index fff12bd04..a3ee48103 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -2,20 +2,19 @@ # # Table name: types_de_champ # -# id :integer not null, primary key -# description :text -# libelle :string -# mandatory :boolean default(FALSE) -# options :jsonb -# order_place :integer -# private :boolean default(FALSE), not null -# type_champ :string -# created_at :datetime -# updated_at :datetime -# parent_id :bigint -# procedure_id :integer -# revision_id :bigint -# stable_id :bigint +# id :integer not null, primary key +# description :text +# libelle :string +# mandatory :boolean default(FALSE) +# options :jsonb +# order_place :integer +# private :boolean default(FALSE), not null +# type_champ :string +# created_at :datetime +# updated_at :datetime +# parent_id :bigint +# revision_id :bigint +# stable_id :bigint # class TypeDeChamp < ApplicationRecord self.ignored_columns = ['procedure_id'] diff --git a/db/migrate/20200902103047_remove_type_de_champ_procedure_id.rb b/db/migrate/20200902103047_remove_type_de_champ_procedure_id.rb new file mode 100644 index 000000000..8a4f5fba7 --- /dev/null +++ b/db/migrate/20200902103047_remove_type_de_champ_procedure_id.rb @@ -0,0 +1,5 @@ +class RemoveTypeDeChampProcedureId < ActiveRecord::Migration[6.0] + def change + remove_column :types_de_champ, :procedure_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 985446dae..2c5243048 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: 2020_08_19_153016) do +ActiveRecord::Schema.define(version: 2020_09_02_103047) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -611,7 +611,6 @@ ActiveRecord::Schema.define(version: 2020_08_19_153016) do t.string "libelle" t.string "type_champ" t.integer "order_place" - t.integer "procedure_id" t.text "description" t.boolean "mandatory", default: false t.boolean "private", default: false, null: false @@ -623,7 +622,6 @@ ActiveRecord::Schema.define(version: 2020_08_19_153016) do t.bigint "revision_id" t.index ["parent_id"], name: "index_types_de_champ_on_parent_id" t.index ["private"], name: "index_types_de_champ_on_private" - t.index ["procedure_id"], name: "index_types_de_champ_on_procedure_id" t.index ["revision_id"], name: "index_types_de_champ_on_revision_id" t.index ["stable_id"], name: "index_types_de_champ_on_stable_id" end From 6d4425885da7bc8a2afb71c7a964378089d21364 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 3 Sep 2020 18:28:49 +0200 Subject: [PATCH 3/3] Fix champ order joins sql error --- app/models/champ.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index b4231596c..a4f5b35ad 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -48,7 +48,13 @@ class Champ < ApplicationRecord scope :private_only, -> { where(private: true) } scope :ordered, -> { includes(:type_de_champ).order(:row, 'types_de_champ.order_place') } scope :public_ordered, -> { public_only.joins(dossier: { revision: :revision_types_de_champ }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) } - scope :private_ordered, -> { private_only.joins(dossier: { revision: :revision_types_de_champ_private }).where('procedure_revision_types_de_champ.type_de_champ_id = champs.type_de_champ_id').order(:position) } + # we need to do private champs order as manual join to avoid conflicting join names + scope :private_ordered, -> do + private_only.joins('INNER JOIN types_de_champ types_de_champ_private + ON types_de_champ_private.id = champs.type_de_champ_id AND types_de_champ_private.private = true + INNER JOIN procedure_revision_types_de_champ procedure_revision_types_de_champ_private + ON procedure_revision_types_de_champ_private.type_de_champ_id = types_de_champ_private.id').order(:position) + end scope :root, -> { where(parent_id: nil) }