-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pj_list): extract pj list in a concern and simplify
- Loading branch information
Showing
5 changed files
with
103 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module PiecesJointesListConcern | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def pieces_jointes_list? | ||
pieces_jointes_list(public_only: true) | ||
end | ||
|
||
def pieces_jointes_list_without_conditionnal | ||
pieces_jointes_list(public_only: true, wrap_with_parent: true, without_conditional: true) | ||
end | ||
|
||
def pieces_jointes_list_with_conditionnal | ||
pieces_jointes_list(public_only: true, wrap_with_parent: true, conditional_only: true) | ||
end | ||
|
||
def pieces_jointes_exportables_list | ||
pieces_jointes_list(with_titre_identite: false) | ||
end | ||
|
||
def pieces_jointes_list( | ||
with_titre_identite: true, | ||
public_only: false, | ||
without_conditional: false, | ||
conditional_only: false, | ||
wrap_with_parent: false | ||
) | ||
coordinates = active_revision.revision_types_de_champ | ||
.includes(:type_de_champ, revision_types_de_champ: :type_de_champ) | ||
|
||
coordinates = coordinates.public_only if public_only | ||
|
||
type_champ = ['piece_justificative'] | ||
type_champ << 'titre_identite' if with_titre_identite | ||
|
||
coordinates = coordinates.where(types_de_champ: { type_champ: }) | ||
|
||
if conditional_only | ||
coordinates = coordinates.where.not(types_de_champ: { condition: nil }) | ||
elsif without_conditional | ||
coordinates = coordinates.where(types_de_champ: { condition: nil }) | ||
end | ||
|
||
return coordinates.map(&:type_de_champ) if !wrap_with_parent | ||
|
||
# we want pj in the form of [[pj1], [pj2, repetition], [pj3, repetition]] | ||
coordinates | ||
.map { |c| c.child? ? [c, c.parent] : [c] } | ||
.map { _1.map(&:type_de_champ) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
describe PiecesJointesListConcern do | ||
describe '#pieces_jointes_list' do | ||
include Logic | ||
let(:procedure) { create(:procedure, types_de_champ_public:, types_de_champ_private:) } | ||
let(:types_de_champ_public) do | ||
[ | ||
{ type: :integer_number, stable_id: 900 }, | ||
{ type: :piece_justificative, libelle: "pj1", stable_id: 910 }, | ||
{ type: :piece_justificative, libelle: "pj-cond", stable_id: 911, condition: ds_eq(champ_value(900), constant(1)) }, | ||
{ type: :repetition, libelle: "Répétition", stable_id: 920, children: [{ type: :piece_justificative, libelle: "pj2", stable_id: 921 }] }, | ||
{ type: :titre_identite, libelle: "pj3", stable_id: 930 } | ||
] | ||
end | ||
|
||
let(:types_de_champ_private) do | ||
[ | ||
{ type: :integer_number, stable_id: 950 }, | ||
{ type: :piece_justificative, libelle: "pj5", stable_id: 960 }, | ||
{ type: :piece_justificative, libelle: "pj-cond2", stable_id: 961, condition: ds_eq(champ_value(900), constant(1)) }, | ||
{ type: :repetition, libelle: "Répétition2", stable_id: 970, children: [{ type: :piece_justificative, libelle: "pj6", stable_id: 971 }] } | ||
] | ||
end | ||
|
||
let(:types_de_champ) { procedure.active_revision.types_de_champ } | ||
def find_by_stable_id(stable_id) = types_de_champ.find { _1.stable_id == stable_id } | ||
|
||
let(:pj1) { find_by_stable_id(910) } | ||
let(:pjcond) { find_by_stable_id(911) } | ||
let(:repetition) { find_by_stable_id(920) } | ||
let(:pj2) { find_by_stable_id(921) } | ||
let(:pj3) { find_by_stable_id(930) } | ||
|
||
let(:pj5) { find_by_stable_id(960) } | ||
let(:pjcond2) { find_by_stable_id(961) } | ||
let(:repetition2) { find_by_stable_id(970) } | ||
let(:pj6) { find_by_stable_id(971) } | ||
|
||
it "returns the list of pieces jointes without conditional" do | ||
expect(procedure.pieces_jointes_list_without_conditionnal).to match_array([[pj1], [pj2, repetition], [pj3]]) | ||
end | ||
|
||
it "returns the list of pieces jointes having conditional" do | ||
expect(procedure.pieces_jointes_list_with_conditionnal).to match_array([[pjcond]]) | ||
end | ||
|
||
it "returns the list of pieces jointes with private, without parent repetition, without titre identite" do | ||
expect(procedure.pieces_jointes_exportables_list.map(&:libelle)).to match_array([pj1, pj2, pjcond, pj5, pjcond2, pj6].map(&:libelle)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters