-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathcolumns_concern.rb
176 lines (134 loc) · 7.21 KB
/
columns_concern.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# frozen_string_literal: true
module ColumnsConcern
extend ActiveSupport::Concern
included do
# we cannot use column.id ( == { procedure_id, column_id }.to_json)
# as the order of the keys is not guaranteed
# instead, we are using h_id == { procedure_id:, column_id: }
# another way to find a column is to look for its label
def find_column(h_id: nil, label: nil)
column = columns.find { _1.h_id == h_id } if h_id.present?
column = columns.find { _1.label == label } if label.present?
# TODO: to remove after linked_drop_down column column_id migration
if column.nil? && h_id.is_a?(Hash) && h_id[:column_id].present?
h_id[:column_id].gsub!('->', '.')
column = columns.find { _1.h_id == h_id }
end
raise ActiveRecord::RecordNotFound.new("Column: unable to find h_id: #{h_id} or label: #{label} for procedure_id #{id}") if column.nil?
column
end
def columns
Current.procedure_columns ||= {}
Current.procedure_columns[id] ||= begin
columns = dossier_columns
columns.concat(standard_columns)
columns.concat(individual_columns) if for_individual
columns.concat(moral_columns) if !for_individual
columns.concat(procedure_chorus_columns) if chorusable? && chorus_configuration.complete?
columns.concat(types_de_champ_columns)
end
end
def usager_columns_for_export
columns = [dossier_id_column, user_email_for_display_column, user_france_connected_column]
columns.concat(individual_columns) if for_individual
columns.concat(moral_columns) if !for_individual
columns.concat(procedure_chorus_columns) if chorusable? && chorus_configuration.complete?
# ensure the columns exist in main list
# otherwise, they will be found by the find_column method
columns.filter { _1.id.in?(self.columns.map(&:id)) }
end
def dossier_columns_for_export
columns = [dossier_state_column, dossier_archived_column]
columns.concat(dossier_dates_columns)
columns.concat([dossier_motivation_column])
columns.concat(sva_svr_columns(for_export: true)) if sva_svr_enabled?
columns.concat([groupe_instructeurs_id_column, followers_instructeurs_email_column])
# ensure the columns exist in main list
# otherwise, they will be found by the find_column method
columns.filter { _1.id.in?(self.columns.map(&:id)) }
end
def dossier_id_column = dossier_col(table: 'self', column: 'id', type: :integer)
def dossier_state_column
options_for_select = I18n.t('instructeurs.dossiers.filterable_state').map(&:to_a).map(&:reverse)
dossier_col(table: 'self', column: 'state', type: :enum, options_for_select:, displayable: false)
end
def notifications_column = dossier_col(table: 'notifications', column: 'notifications', label: "notifications", filterable: false)
def sva_svr_columns(for_export: false)
scope = [:activerecord, :attributes, :procedure_presentation, :fields, :self]
columns = [
dossier_col(table: 'self', column: 'sva_svr_decision_on', type: :date,
label: I18n.t("#{sva_svr_decision}_decision_on", scope:, type: sva_svr_configuration.human_decision))
]
if !for_export
columns << dossier_col(table: 'self', column: 'sva_svr_decision_before', type: :date, displayable: false,
label: I18n.t("#{sva_svr_decision}_decision_before", scope:))
end
columns
end
def default_sorted_column
SortedColumn.new(column: notifications_column, order: 'desc')
end
def default_displayed_columns = [email_column]
private
def groupe_instructeurs_id_column = dossier_col(table: 'groupe_instructeur', column: 'id', type: :enum)
def followers_instructeurs_email_column = dossier_col(table: 'followers_instructeurs', column: 'email')
def dossier_archived_column = dossier_col(table: 'self', column: 'archived', type: :boolean, displayable: false, filterable: false);
def dossier_motivation_column = dossier_col(table: 'self', column: 'motivation', type: :text, displayable: false, filterable: false);
def user_email_for_display_column = dossier_col(table: 'self', column: 'user_email_for_display', filterable: false, displayable: false)
def user_france_connected_column = dossier_col(table: 'self', column: 'user_from_france_connect?', type: :boolean, filterable: false, displayable: false)
def dossier_labels_column = dossier_col(table: 'dossier_labels', column: 'label_id', type: :enum, options_for_select: labels.map { [_1.name, _1.id] })
def procedure_chorus_columns
['domaine_fonctionnel', 'referentiel_prog', 'centre_de_cout']
.map { |column| dossier_col(table: 'procedure', column:, displayable: false, filterable: false) }
end
def dossier_non_displayable_dates_columns
['updated_since', 'depose_since', 'en_construction_since', 'en_instruction_since', 'processed_since']
.map { |column| dossier_col(table: 'self', column:, type: :date, displayable: false) }
end
def dossier_dates_columns
['created_at', 'updated_at', 'last_champ_updated_at', 'depose_at', 'en_construction_at', 'en_instruction_at', 'processed_at']
.map { |column| dossier_col(table: 'self', column:, type: :datetime) }
end
def email_column
dossier_col(table: 'user', column: 'email')
end
def dossier_columns
columns = [dossier_id_column, notifications_column]
columns.concat([dossier_state_column])
columns.concat([dossier_archived_column])
columns.concat(dossier_dates_columns)
columns.concat([dossier_motivation_column])
columns.concat(sva_svr_columns(for_export: false)) if sva_svr_enabled?
columns.concat(dossier_non_displayable_dates_columns)
end
def standard_columns
[
email_column,
user_email_for_display_column,
followers_instructeurs_email_column,
groupe_instructeurs_id_column,
dossier_col(table: 'avis', column: 'question_answer', filterable: false),
user_france_connected_column,
dossier_labels_column
]
end
def individual_columns
['gender', 'nom', 'prenom'].map { |column| dossier_col(table: 'individual', column:) }
.concat ['mandataire_last_name', 'mandataire_first_name'].map { |column| dossier_col(table: 'self', column:) }
.concat ['for_tiers'].map { |column| dossier_col(table: 'self', column:, type: :boolean) }
end
def moral_columns
siret_column = dossier_col(table: 'etablissement', column: :siret)
etablissements = Etablissement::DISPLAYABLE_COLUMNS.map do |(column, attributes)|
dossier_col(table: 'etablissement', column:, type: attributes[:type], filterable: attributes.fetch(:filterable, true))
end
others = %w[code_postal].map { |column| dossier_col(table: 'etablissement', column:) }
for_export = Etablissement::EXPORTABLE_COLUMNS.map { |column| dossier_col(table: 'etablissement', column:) }
[siret_column, etablissements, others, for_export].flatten
end
def types_de_champ_columns
all_revisions_types_de_champ.flat_map { _1.columns(procedure: self) }
end
def dossier_col(**args) = Columns::DossierColumn.new(**(args.merge(procedure_id: id)))
end
end