Skip to content

Commit

Permalink
feat(procedure): can filter from repetion content
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Aug 22, 2024
1 parent 6a642c3 commit a912410
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 34 deletions.
2 changes: 2 additions & 0 deletions app/models/column.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Column
TYPE_DE_CHAMP = 'type_de_champ'

attr_reader :table, :column, :label, :classname, :type, :scope, :value_column, :filterable, :displayable

def initialize(table:, column:, label: nil, type: :text, value_column: :value, filterable: true, displayable: true, classname: '', scope: '', virtual: nil)
Expand Down
22 changes: 13 additions & 9 deletions app/models/concerns/addressable_column_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@ module AddressableColumnConcern
extend ActiveSupport::Concern

included do
def columns(table:)
def columns(displayable: true)
super.concat([
Columns::JSONPathColumn.new(
table:,
table: Column::TYPE_DE_CHAMP,
virtual: true,
column: stable_id,
label: "#{libelle} – code postal (5 chiffres)",
type: :text,
value_column: ['postal_code']
value_column: ['postal_code'],
displayable:
),
Columns::JSONPathColumn.new(
table:,
table: Column::TYPE_DE_CHAMP,
virtual: true,
column: stable_id,
label: "#{libelle} – commune",
type: :text,
value_column: ['city_name']
value_column: ['city_name'],
displayable:
),
Columns::JSONPathColumn.new(
table:,
table: Column::TYPE_DE_CHAMP,
virtual: true,
column: stable_id,
label: "#{libelle} – département",
type: :enum,
value_column: ['departement_code']
value_column: ['departement_code'],
displayable:
),
Columns::JSONPathColumn.new(
table:,
table: Column::TYPE_DE_CHAMP,
virtual: true,
column: stable_id,
label: "#{libelle} – region",
type: :enum,
value_column: ['region_name']
value_column: ['region_name'],
displayable:
)
])
end
Expand Down
10 changes: 1 addition & 9 deletions app/models/concerns/columns_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module ColumnsConcern
extend ActiveSupport::Concern

included do
TYPE_DE_CHAMP = 'type_de_champ'

def find_column(id:) = columns.find { |f| f.id == id }

def columns
Expand Down Expand Up @@ -75,13 +73,7 @@ def moral_columns
end

def types_de_champ_columns
types_de_champ_for_procedure_presentation
.pluck(:type_champ, :libelle, :stable_id)
.reject { |(type_champ)| type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
.flat_map do |(type_champ, libelle, stable_id)|
tdc = TypeDeChamp.new(type_champ:, libelle:, stable_id:)
tdc.dynamic_type.columns(table: TYPE_DE_CHAMP)
end
aggregated_types_de_champ.flat_map(&:columns)
end
end
end
10 changes: 9 additions & 1 deletion app/models/procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def active_revision
brouillon? ? draft_revision : published_revision
end

def types_de_champ_for_procedure_presentation(parent = nil)
def aggregated_types_de_champ(parent = nil)
if brouillon?
if parent.nil?
TypeDeChamp.fillable
Expand Down Expand Up @@ -124,6 +124,14 @@ def types_de_champ_for_procedure_presentation(parent = nil)
end
end

def types_de_champ_for_procedure_export(parent = nil)
if parent.nil?
aggregated_types_de_champ.not_repetition
else
aggregated_types_de_champ(parent)
end
end

def types_de_champ_for_tags
TypeDeChamp
.fillable
Expand Down
2 changes: 1 addition & 1 deletion app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class TypeDeChamp < ApplicationRecord
has_one :revision, through: :revision_type_de_champ
has_one :procedure, through: :revision

delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelles_for_export, :libelle_for_export, :primary_options, :secondary_options, to: :dynamic_type
delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelles_for_export, :libelle_for_export, :primary_options, :secondary_options, :columns, to: :dynamic_type
delegate :used_by_routing_rules?, to: :revision_type_de_champ

class WithIndifferentAccess
Expand Down
6 changes: 6 additions & 0 deletions app/models/types_de_champ/repetition_type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ def libelle_for_export
# /\*?[] are invalid Excel worksheet characters
ActiveStorage::Filename.new(str.delete('[]*?')).sanitized
end

def columns(displayable: true)
@type_de_champ.procedure

Check warning on line 24 in app/models/types_de_champ/repetition_type_de_champ.rb

View check run for this annotation

Codecov / codecov/patch

app/models/types_de_champ/repetition_type_de_champ.rb#L24

Added line #L24 was not covered by tests
.aggregated_types_de_champ(@type_de_champ)
.flat_map { _1.columns(displayable: false) }

Check warning on line 26 in app/models/types_de_champ/repetition_type_de_champ.rb

View check run for this annotation

Codecov / codecov/patch

app/models/types_de_champ/repetition_type_de_champ.rb#L26

Added line #L26 was not covered by tests
end
end
7 changes: 4 additions & 3 deletions app/models/types_de_champ/type_de_champ_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ def champ_default_api_value(version = 2)
end
end

def columns(table:)
def columns(displayable: true)
[
Column.new(
table:,
table: Column::TYPE_DE_CHAMP,
column: stable_id.to_s,
label: libelle,
type: TypeDeChamp.filter_hash_type(type_champ),
value_column: TypeDeChamp.filter_hash_value_column(type_champ)
value_column: TypeDeChamp.filter_hash_value_column(type_champ),
displayable:
)
]
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/procedure_export_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def champs_repetables_options
.group_by(&:stable_id)

procedure
.types_de_champ_for_procedure_presentation
.types_de_champ_for_procedure_export
.repetition
.filter_map do |type_de_champ_repetition|
types_de_champ = procedure.types_de_champ_for_procedure_presentation(type_de_champ_repetition).to_a
types_de_champ = procedure.types_de_champ_for_procedure_export(type_de_champ_repetition).to_a

Check warning on line 113 in app/services/procedure_export_service.rb

View check run for this annotation

Codecov / codecov/patch

app/services/procedure_export_service.rb#L113

Added line #L113 was not covered by tests
rows = champs_by_stable_id.fetch(type_de_champ_repetition.stable_id, []).flat_map(&:rows_for_export)

if types_de_champ.present? && rows.present?
Expand Down Expand Up @@ -151,7 +151,7 @@ def options_for(table, format)
end

def spreadsheet_columns(format)
types_de_champ = procedure.types_de_champ_for_procedure_presentation.not_repetition.to_a
types_de_champ = procedure.types_de_champ_for_procedure_export.to_a

Proc.new do |instance|
instance.send(:"spreadsheet_columns_#{format}", types_de_champ: types_de_champ)
Expand Down
7 changes: 3 additions & 4 deletions spec/models/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2019,8 +2019,8 @@
let(:repetition_second_revision_champ) { dossier_second_revision.champs_public.find(&:repetition?) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossier_second_revision) { create(:dossier, procedure: procedure) }
let(:dossier_champs_for_export) { dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition) }
let(:dossier_second_revision_champs_for_export) { dossier_second_revision.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition) }
let(:dossier_champs_for_export) { dossier.champs_for_export(procedure.types_de_champ_for_procedure_export) }
let(:dossier_second_revision_champs_for_export) { dossier_second_revision.champs_for_export(procedure.types_de_champ_for_procedure_export) }

context "when procedure published" do
before do
Expand Down Expand Up @@ -2053,8 +2053,7 @@
draft.add_type_de_champ(type_champ: :communes, libelle: "communes", parent_stable_id: tdc_repetition.stable_id)

dossier_test = create(:dossier, procedure: proc_test)
repetition = proc_test.types_de_champ_for_procedure_presentation.repetition.first
type_champs = proc_test.types_de_champ_for_procedure_presentation(repetition).to_a
type_champs = proc_test.types_de_champ_for_procedure_export(tdc_repetition).to_a
expect(type_champs.size).to eq(1)
expect(dossier.champs_for_export(type_champs).size).to eq(3)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/procedure_presentation_and_revisions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

describe ProcedurePresentation do
describe "#types_de_champ_for_procedure_presentation" do
subject { procedure.types_de_champ_for_procedure_presentation.not_repetition.pluck(:libelle) }
describe "#types_de_champ_for_procedure_export" do
subject { procedure.types_de_champ_for_procedure_export.pluck(:libelle) }

context 'for a draft procedure' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :number, libelle: 'libelle 1' }]) }
Expand Down
4 changes: 2 additions & 2 deletions spec/models/procedure_revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
draft.reload
expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0, 1, 2, 3])
expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(2)
expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(type_de_champ_public)).to eq(2)
expect(draft.procedure.types_de_champ_for_procedure_export.index(type_de_champ_public)).to eq(2)
end

it 'move up' do
Expand All @@ -132,7 +132,7 @@
draft.reload
expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0, 1, 2, 3])
expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(0)
expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(last_type_de_champ)).to eq(0)
expect(draft.procedure.types_de_champ_for_procedure_export.index(last_type_de_champ)).to eq(0)
end
end

Expand Down

0 comments on commit a912410

Please sign in to comment.