remove set_position

This commit is contained in:
simon lehericey 2022-05-12 14:18:28 +02:00
parent 84d2e38494
commit 7f23449f24
4 changed files with 35 additions and 32 deletions

View file

@ -39,15 +39,36 @@ class ProcedureRevision < ApplicationRecord
end
def add_type_de_champ(params)
if params[:parent_id]
find_or_clone_type_de_champ(params.delete(:parent_id))
.types_de_champ
.tap do |types_de_champ|
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params).migrate_parent!
parent_stable_id = params[:parent_id]
coordinate = {}
if parent_stable_id.present?
clone_parent_to_draft_revision(parent_stable_id)
parent = types_de_champ.find_by(stable_id: parent_stable_id)
parent_coordinate = revision_types_de_champ.find_by(type_de_champ: parent)
coordinate[:parent_id] = parent_coordinate.id
coordinate[:position] = children_of(parent).count
# old system
params[:order_place] = coordinate[:position]
params[:parent_id] = parent.id
elsif params[:private]
coordinate[:position] = revision_types_de_champ_private.count
else
types_de_champ.create(params)
coordinate[:position] = revision_types_de_champ_public.count
end
tdc = TypeDeChamp.new(params)
if tdc.save
coordinate[:type_de_champ] = tdc
revision_types_de_champ.create!(coordinate)
end
tdc
rescue => e
TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) }
end
def find_or_clone_type_de_champ(stable_id)

View file

@ -22,8 +22,6 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
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
@ -37,18 +35,4 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
revision.revision_types_de_champ_public
end
end
private
def set_position
self.position ||= begin
types_de_champ = (private? ? revision.revision_types_de_champ_private : revision.revision_types_de_champ_public).filter(&:persisted?)
if types_de_champ.present?
types_de_champ.last.position + 1
else
0
end
end
end
end

View file

@ -353,11 +353,12 @@ def add_types_de_champs(types_de_champ, to: nil, scope: :public)
revision = to
association_name = scope == :private ? :revision_types_de_champ_private : :revision_types_de_champ_public
types_de_champ.each do |type_de_champ|
types_de_champ.each.with_index do |type_de_champ, i|
type_de_champ.private = (scope == :private)
revision.public_send(association_name) << build(:procedure_revision_type_de_champ,
revision: revision,
position: type_de_champ.order_place,
position: i,
type_de_champ: type_de_champ)
end
end

View file

@ -17,16 +17,13 @@ FactoryBot.define do
if evaluator.procedure
revision = evaluator.procedure.active_revision
build(:procedure_revision_type_de_champ,
position: evaluator.position,
evaluator.procedure.save
create(:procedure_revision_type_de_champ,
position: evaluator.position || 0,
revision: revision,
type_de_champ: type_de_champ)
if type_de_champ.private?
revision.types_de_champ_private << type_de_champ
else
revision.types_de_champ_public << type_de_champ
end
elsif evaluator.parent
type_de_champ.order_place = evaluator.position || evaluator.parent.types_de_champ.size
evaluator.parent.types_de_champ << type_de_champ