Fix champ repetition belongs_to associations

This commit is contained in:
Paul Chavard 2018-12-20 15:04:13 +01:00
parent 531feb3515
commit 1beefe4469
5 changed files with 61 additions and 13 deletions

View file

@ -1,4 +1,6 @@
class Champs::RepetitionChamp < Champ
before_save :setup_dossier
has_many :champs, -> { ordered }, foreign_key: :parent_id, dependent: :destroy
accepts_nested_attributes_for :champs, allow_destroy: true
@ -10,4 +12,10 @@ class Champs::RepetitionChamp < Champ
def search_terms
# The user cannot enter any information here so it doesnt make much sense to search
end
def setup_dossier
champs.each do |champ|
champ.dossier = dossier
end
end
end

View file

@ -4,8 +4,8 @@ class Procedure < ApplicationRecord
MAX_DUREE_CONSERVATION = 36
has_many :types_de_piece_justificative, -> { ordered }, dependent: :destroy
has_many :types_de_champ, -> { public_only.ordered }, dependent: :destroy
has_many :types_de_champ_private, -> { private_only.ordered }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :types_de_champ, -> { root.public_only.ordered }, dependent: :destroy
has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :dossiers
has_many :deleted_dossiers, dependent: :destroy

View file

@ -61,12 +61,14 @@ class TypeDeChamp < ApplicationRecord
after_initialize :set_dynamic_type
after_create :populate_stable_id
before_save :setup_procedure
attr_reader :dynamic_type
scope :public_only, -> { where(private: false) }
scope :private_only, -> { where(private: true) }
scope :ordered, -> { order(order_place: :asc) }
scope :root, -> { where(parent_id: nil) }
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
def build(params = {})
@ -82,6 +84,7 @@ class TypeDeChamp < ApplicationRecord
has_one_attached :piece_justificative_template
accepts_nested_attributes_for :drop_down_list
accepts_nested_attributes_for :types_de_champ, allow_destroy: true
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :type_champ, presence: true, allow_blank: false, allow_nil: false
@ -160,6 +163,12 @@ class TypeDeChamp < ApplicationRecord
private
def setup_procedure
types_de_champ.each do |type_de_champ|
type_de_champ.procedure = procedure
end
end
def populate_stable_id
if !stable_id
update_column(:stable_id, id)