Procedure: add #new_dossier
Create a new instance of a dossier and its champs without saving them in the db
This commit is contained in:
parent
2fee9cb99e
commit
c90ddb92a6
3 changed files with 39 additions and 0 deletions
|
@ -47,6 +47,20 @@ class Procedure < ApplicationRecord
|
||||||
validates :description, presence: true, allow_blank: false, allow_nil: false
|
validates :description, presence: true, allow_blank: false, allow_nil: false
|
||||||
validates :organisation, presence: true, allow_blank: false, allow_nil: false
|
validates :organisation, presence: true, allow_blank: false, allow_nil: false
|
||||||
|
|
||||||
|
# Warning: dossier after_save build_default_champs must be removed
|
||||||
|
# to save a dossier created from this method
|
||||||
|
def new_dossier
|
||||||
|
champs = types_de_champ
|
||||||
|
.ordered
|
||||||
|
.map { |tdc| tdc.champ.build }
|
||||||
|
|
||||||
|
champs_private = types_de_champ_private
|
||||||
|
.ordered
|
||||||
|
.map { |tdc| tdc.champ.build }
|
||||||
|
|
||||||
|
Dossier.new(procedure: self, champs: champs, champs_private: champs_private)
|
||||||
|
end
|
||||||
|
|
||||||
def hide!
|
def hide!
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
self.update(hidden_at: now)
|
self.update(hidden_at: now)
|
||||||
|
|
|
@ -28,6 +28,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
|
|
||||||
scope :public_only, -> { where(private: false) }
|
scope :public_only, -> { where(private: false) }
|
||||||
scope :private_only, -> { where(private: true) }
|
scope :private_only, -> { where(private: true) }
|
||||||
|
scope :ordered, -> { order(order_place: :asc) }
|
||||||
|
|
||||||
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
|
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
|
||||||
def build(params = {})
|
def build(params = {})
|
||||||
|
|
|
@ -636,4 +636,28 @@ describe Procedure do
|
||||||
it { is_expected.to eq("dossiers_procedure-#{procedure.id}_2018-01-02_23-11") }
|
it { is_expected.to eq("dossiers_procedure-#{procedure.id}_2018-01-02_23-11") }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#new_dossier' do
|
||||||
|
let(:procedure) do
|
||||||
|
procedure = create(:procedure)
|
||||||
|
|
||||||
|
create(:type_de_champ_text, procedure: procedure, order_place: 1)
|
||||||
|
create(:type_de_champ_number, procedure: procedure, order_place: 2)
|
||||||
|
create(:type_de_champ_textarea, :private, procedure: procedure)
|
||||||
|
|
||||||
|
procedure
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:dossier) { procedure.new_dossier }
|
||||||
|
|
||||||
|
it { expect(dossier.procedure).to eq(procedure) }
|
||||||
|
|
||||||
|
it { expect(dossier.champs.size).to eq(2) }
|
||||||
|
it { expect(dossier.champs[0].type).to eq("Champs::TextChamp") }
|
||||||
|
|
||||||
|
it { expect(dossier.champs_private.size).to eq(1) }
|
||||||
|
it { expect(dossier.champs_private[0].type).to eq("Champs::TextareaChamp") }
|
||||||
|
|
||||||
|
it { expect(Champ.count).to eq(0) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue