From 9de917592bfee7588901cd2ff6b4cf2fc0aaabb5 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 26 Jun 2020 11:37:28 +0200 Subject: [PATCH] Add revisions models and relationships --- app/models/dossier.rb | 1 + app/models/procedure.rb | 7 ++++++ app/models/procedure_revision.rb | 16 +++++++++++++ .../procedure_revision_type_de_champ.rb | 24 +++++++++++++++++++ app/models/type_de_champ.rb | 3 +++ 5 files changed, 51 insertions(+) create mode 100644 app/models/procedure_revision.rb create mode 100644 app/models/procedure_revision_type_de_champ.rb diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 6502120b7..21a47cdcf 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -48,6 +48,7 @@ class Dossier < ApplicationRecord belongs_to :groupe_instructeur has_one :procedure, through: :groupe_instructeur + belongs_to :revision, class_name: 'ProcedureRevision', optional: true belongs_to :user accepts_nested_attributes_for :champs diff --git a/app/models/procedure.rb b/app/models/procedure.rb index df65c0de4..7a629858f 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -14,6 +14,9 @@ class Procedure < ApplicationRecord has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', inverse_of: :procedure, dependent: :destroy + has_many :revisions, class_name: 'ProcedureRevision', inverse_of: :procedure, dependent: :destroy + belongs_to :draft_revision, class_name: 'ProcedureRevision', optional: true + belongs_to :published_revision, class_name: 'ProcedureRevision', optional: true has_many :deleted_dossiers, dependent: :destroy has_one :module_api_carto, dependent: :destroy @@ -23,6 +26,10 @@ class Procedure < ApplicationRecord belongs_to :canonical_procedure, class_name: 'Procedure' belongs_to :service + def active_revision + brouillon? ? draft_revision : published_revision + end + has_many :administrateurs_procedures has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! } has_many :groupe_instructeurs, dependent: :destroy diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb new file mode 100644 index 000000000..05e70ed70 --- /dev/null +++ b/app/models/procedure_revision.rb @@ -0,0 +1,16 @@ +class ProcedureRevision < ApplicationRecord + belongs_to :procedure, -> { with_discarded }, inverse_of: :revisions + + has_many :revision_types_de_champ, -> { public_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision + has_many :revision_types_de_champ_private, -> { private_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision + has_many :types_de_champ, through: :revision_types_de_champ, source: :type_de_champ + has_many :types_de_champ_private, through: :revision_types_de_champ_private, source: :type_de_champ + + def draft? + procedure.draft_revision == self + end + + def locked? + !draft? + end +end diff --git a/app/models/procedure_revision_type_de_champ.rb b/app/models/procedure_revision_type_de_champ.rb new file mode 100644 index 000000000..36009f242 --- /dev/null +++ b/app/models/procedure_revision_type_de_champ.rb @@ -0,0 +1,24 @@ +class ProcedureRevisionTypeDeChamp < ApplicationRecord + belongs_to :revision, class_name: 'ProcedureRevision' + belongs_to :type_de_champ + + scope :ordered, -> { order(:position) } + scope :public_only, -> { joins(:type_de_champ).where(types_de_champ: { private: false }) } + scope :private_only, -> { joins(:type_de_champ).where(types_de_champ: { private: true }) } + + before_create :set_position + + def private? + type_de_champ.private? + end + + private + + def set_position + self.position ||= if private? + revision.types_de_champ_private.size + else + revision.types_de_champ.size + end + end +end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 20dff4fd7..5c599e61e 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -31,11 +31,14 @@ class TypeDeChamp < ApplicationRecord } belongs_to :procedure + belongs_to :revision, class_name: 'ProcedureRevision' belongs_to :parent, class_name: 'TypeDeChamp' has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', inverse_of: :parent, dependent: :destroy store_accessor :options, :cadastres, :quartiers_prioritaires, :parcelles_agricoles, :old_pj, :drop_down_options, :skip_pj_validation + has_many :revision_types_de_champ, class_name: 'ProcedureRevisionTypeDeChamp', dependent: :destroy, inverse_of: :type_de_champ + delegate :tags_for_template, to: :dynamic_type class WithIndifferentAccess