Add type_de_champ mutation methods to revision
This commit is contained in:
parent
bc09b61684
commit
0a0912503b
3 changed files with 252 additions and 1 deletions
|
@ -6,6 +6,69 @@ class ProcedureRevision < ApplicationRecord
|
|||
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 add_type_de_champ(params)
|
||||
params[:procedure] = procedure
|
||||
params[:revision] = self
|
||||
|
||||
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.size
|
||||
end.create(params)
|
||||
elsif params[:private]
|
||||
types_de_champ_private.create(params)
|
||||
else
|
||||
types_de_champ.create(params)
|
||||
end
|
||||
end
|
||||
|
||||
def find_or_clone_type_de_champ(id)
|
||||
type_de_champ = find_type_de_champ_by_id(id)
|
||||
|
||||
if type_de_champ.revision == self
|
||||
type_de_champ
|
||||
elsif type_de_champ.parent.present?
|
||||
find_or_clone_type_de_champ(type_de_champ.parent.stable_id).types_de_champ.find_by!(stable_id: id)
|
||||
else
|
||||
type_de_champ.revise!
|
||||
end
|
||||
end
|
||||
|
||||
def move_type_de_champ(id, position)
|
||||
type_de_champ = find_type_de_champ_by_id(id)
|
||||
|
||||
if type_de_champ.parent.present?
|
||||
repetition_type_de_champ = find_or_clone_type_de_champ(id).parent
|
||||
|
||||
move_type_de_champ_hash(repetition_type_de_champ.types_de_champ.to_a, type_de_champ, position).each do |(id, position)|
|
||||
repetition_type_de_champ.types_de_champ.find(id).update!(order_place: position)
|
||||
end
|
||||
elsif type_de_champ.private?
|
||||
move_type_de_champ_hash(types_de_champ_private.to_a, type_de_champ, position).each do |(id, position)|
|
||||
revision_types_de_champ_private.find_by!(type_de_champ_id: id).update!(position: position)
|
||||
end
|
||||
else
|
||||
move_type_de_champ_hash(types_de_champ.to_a, type_de_champ, position).each do |(id, position)|
|
||||
revision_types_de_champ.find_by!(type_de_champ_id: id).update!(position: position)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def remove_type_de_champ(id)
|
||||
type_de_champ = find_type_de_champ_by_id(id)
|
||||
|
||||
if type_de_champ.revision == self
|
||||
type_de_champ.destroy
|
||||
elsif type_de_champ.parent.present?
|
||||
find_or_clone_type_de_champ(id).destroy
|
||||
elsif type_de_champ.private?
|
||||
types_de_champ_private.delete(type_de_champ)
|
||||
else
|
||||
types_de_champ.delete(type_de_champ)
|
||||
end
|
||||
end
|
||||
|
||||
def draft?
|
||||
procedure.draft_revision == self
|
||||
end
|
||||
|
@ -13,4 +76,32 @@ class ProcedureRevision < ApplicationRecord
|
|||
def locked?
|
||||
!draft?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_type_de_champ_by_id(id)
|
||||
types_de_champ.find_by(stable_id: id) ||
|
||||
types_de_champ_private.find_by(stable_id: id) ||
|
||||
types_de_champ_in_repetition.find_by!(stable_id: id)
|
||||
end
|
||||
|
||||
def types_de_champ_in_repetition
|
||||
parent_ids = types_de_champ.repetition.ids + types_de_champ_private.repetition.ids
|
||||
TypeDeChamp.where(parent_id: parent_ids)
|
||||
end
|
||||
|
||||
def move_type_de_champ_hash(types_de_champ, type_de_champ, new_index)
|
||||
old_index = types_de_champ.index(type_de_champ)
|
||||
|
||||
if types_de_champ.delete_at(old_index)
|
||||
types_de_champ.insert(new_index, type_de_champ)
|
||||
.map.with_index do |type_de_champ, index|
|
||||
# FIXUP: needed during transition to revisions
|
||||
type_de_champ.update!(order_place: index)
|
||||
[type_de_champ.id, index]
|
||||
end
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,7 +31,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
}
|
||||
|
||||
belongs_to :procedure
|
||||
belongs_to :revision, class_name: 'ProcedureRevision'
|
||||
belongs_to :revision, class_name: 'ProcedureRevision', optional: true
|
||||
|
||||
belongs_to :parent, class_name: 'TypeDeChamp'
|
||||
has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', inverse_of: :parent, dependent: :destroy
|
||||
|
@ -63,6 +63,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
scope :private_only, -> { where(private: true) }
|
||||
scope :ordered, -> { order(order_place: :asc) }
|
||||
scope :root, -> { where(parent_id: nil) }
|
||||
scope :repetition, -> { where(type_champ: type_champs.fetch(:repetition)) }
|
||||
|
||||
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
|
||||
def build(params = {})
|
||||
|
@ -228,6 +229,13 @@ class TypeDeChamp < ApplicationRecord
|
|||
GraphQL::Schema::UniqueWithinType.encode('Champ', stable_id)
|
||||
end
|
||||
|
||||
def revise!
|
||||
types_de_champ_association = private? ? :revision_types_de_champ_private : :revision_types_de_champ
|
||||
association = revision.send(types_de_champ_association).find_by!(type_de_champ: self)
|
||||
association.update!(type_de_champ: deep_clone(include: [:types_de_champ], &method(:clone_attachments)))
|
||||
association.type_de_champ
|
||||
end
|
||||
|
||||
FEATURE_FLAGS = {}
|
||||
|
||||
def self.type_de_champ_types_for(procedure, user)
|
||||
|
@ -312,4 +320,23 @@ class TypeDeChamp < ApplicationRecord
|
|||
types_de_champ.destroy_all
|
||||
end
|
||||
end
|
||||
|
||||
def clone_attachments(original, kopy)
|
||||
if original.is_a?(TypeDeChamp)
|
||||
clone_attachment(:piece_justificative_template, original, kopy)
|
||||
end
|
||||
end
|
||||
|
||||
def clone_attachment(attribute, original, kopy)
|
||||
original_attachment = original.send(attribute)
|
||||
if original_attachment.attached?
|
||||
kopy.send(attribute).attach({
|
||||
io: StringIO.new(original_attachment.download),
|
||||
filename: original_attachment.filename,
|
||||
content_type: original_attachment.content_type,
|
||||
# we don't want to run virus scanner on cloned file
|
||||
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
133
spec/models/procedure_revision_spec.rb
Normal file
133
spec/models/procedure_revision_spec.rb
Normal file
|
@ -0,0 +1,133 @@
|
|||
describe ProcedureRevision do
|
||||
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
|
||||
let(:revision) { procedure.active_revision }
|
||||
let(:type_de_champ) { revision.types_de_champ.first }
|
||||
let(:type_de_champ_private) { revision.types_de_champ_private.first }
|
||||
let(:type_de_champ_repetition) do
|
||||
type_de_champ = revision.types_de_champ.repetition.first
|
||||
type_de_champ.update(stable_id: 3333)
|
||||
type_de_champ
|
||||
end
|
||||
|
||||
before do
|
||||
RevisionsMigration.add_revisions(procedure)
|
||||
end
|
||||
|
||||
describe '#add_type_de_champ' do
|
||||
it 'type_de_champ' do
|
||||
expect(revision.types_de_champ.size).to eq(2)
|
||||
revision.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text"
|
||||
})
|
||||
procedure.reload
|
||||
expect(revision.types_de_champ.size).to eq(3)
|
||||
expect(procedure.types_de_champ.size).to eq(3)
|
||||
end
|
||||
|
||||
it 'type_de_champ_private' do
|
||||
expect(revision.types_de_champ_private.size).to eq(1)
|
||||
revision.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text",
|
||||
private: true
|
||||
})
|
||||
procedure.reload
|
||||
expect(revision.types_de_champ_private.size).to eq(2)
|
||||
expect(procedure.types_de_champ_private.size).to eq(2)
|
||||
end
|
||||
|
||||
it 'type_de_champ_repetition' do
|
||||
expect(type_de_champ_repetition.types_de_champ.size).to eq(1)
|
||||
revision.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text",
|
||||
parent_id: type_de_champ_repetition.stable_id
|
||||
})
|
||||
type_de_champ_repetition.reload
|
||||
expect(type_de_champ_repetition.types_de_champ.size).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#move_type_de_champ' do
|
||||
let(:procedure) { create(:procedure, :with_type_de_champ, types_de_champ_count: 4) }
|
||||
let(:last_type_de_champ) { revision.types_de_champ.last }
|
||||
|
||||
it 'move down' do
|
||||
expect(revision.types_de_champ.index(type_de_champ)).to eq(0)
|
||||
revision.move_type_de_champ(type_de_champ.stable_id, 2)
|
||||
revision.reload
|
||||
expect(revision.types_de_champ.index(type_de_champ)).to eq(2)
|
||||
end
|
||||
|
||||
it 'move up' do
|
||||
expect(revision.types_de_champ.index(last_type_de_champ)).to eq(3)
|
||||
revision.move_type_de_champ(last_type_de_champ.stable_id, 0)
|
||||
revision.reload
|
||||
expect(revision.types_de_champ.index(last_type_de_champ)).to eq(0)
|
||||
end
|
||||
|
||||
context 'repetition' do
|
||||
let(:procedure) { create(:procedure, :with_repetition) }
|
||||
let(:type_de_champ) { type_de_champ_repetition.types_de_champ.first }
|
||||
let(:last_type_de_champ) { type_de_champ_repetition.types_de_champ.last }
|
||||
|
||||
before do
|
||||
revision.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text",
|
||||
parent_id: type_de_champ_repetition.stable_id
|
||||
})
|
||||
revision.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text",
|
||||
parent_id: type_de_champ_repetition.stable_id
|
||||
})
|
||||
type_de_champ_repetition.reload
|
||||
end
|
||||
|
||||
it 'move down' do
|
||||
expect(type_de_champ_repetition.types_de_champ.index(type_de_champ)).to eq(0)
|
||||
revision.move_type_de_champ(type_de_champ.stable_id, 2)
|
||||
type_de_champ_repetition.reload
|
||||
expect(type_de_champ_repetition.types_de_champ.index(type_de_champ)).to eq(2)
|
||||
end
|
||||
|
||||
it 'move up' do
|
||||
expect(type_de_champ_repetition.types_de_champ.index(last_type_de_champ)).to eq(2)
|
||||
revision.move_type_de_champ(last_type_de_champ.stable_id, 0)
|
||||
type_de_champ_repetition.reload
|
||||
expect(type_de_champ_repetition.types_de_champ.index(last_type_de_champ)).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#remove_type_de_champ' do
|
||||
it 'type_de_champ' do
|
||||
expect(revision.types_de_champ.size).to eq(2)
|
||||
revision.remove_type_de_champ(type_de_champ.stable_id)
|
||||
procedure.reload
|
||||
expect(revision.types_de_champ.size).to eq(1)
|
||||
expect(procedure.types_de_champ.size).to eq(1)
|
||||
end
|
||||
|
||||
it 'type_de_champ_private' do
|
||||
expect(revision.types_de_champ_private.size).to eq(1)
|
||||
revision.remove_type_de_champ(type_de_champ_private.stable_id)
|
||||
procedure.reload
|
||||
expect(revision.types_de_champ_private.size).to eq(0)
|
||||
expect(procedure.types_de_champ_private.size).to eq(0)
|
||||
end
|
||||
|
||||
it 'type_de_champ_repetition' do
|
||||
expect(type_de_champ_repetition.types_de_champ.size).to eq(1)
|
||||
expect(revision.types_de_champ.size).to eq(2)
|
||||
revision.remove_type_de_champ(type_de_champ_repetition.types_de_champ.first.stable_id)
|
||||
procedure.reload
|
||||
type_de_champ_repetition.reload
|
||||
expect(type_de_champ_repetition.types_de_champ.size).to eq(0)
|
||||
expect(revision.types_de_champ.size).to eq(2)
|
||||
expect(procedure.types_de_champ.size).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue