Add revisions models and relationships

This commit is contained in:
Paul Chavard 2020-06-26 11:37:28 +02:00 committed by Paul Chavard
parent 35461f0159
commit 9de917592b
5 changed files with 51 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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