2018-03-06 13:44:29 +01:00
|
|
|
|
class ProcedurePresentation < ApplicationRecord
|
2018-10-03 16:20:27 +02:00
|
|
|
|
EXTRA_SORT_COLUMNS = {
|
|
|
|
|
'notifications' => Set['notifications'],
|
|
|
|
|
'self' => Set['id', 'state']
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 17:03:30 +02:00
|
|
|
|
belongs_to :assign_to
|
2018-10-02 17:04:23 +02:00
|
|
|
|
|
|
|
|
|
delegate :procedure, to: :assign_to
|
|
|
|
|
|
|
|
|
|
validate :check_allowed_displayed_fields
|
|
|
|
|
validate :check_allowed_sort_column
|
|
|
|
|
validate :check_allowed_filter_columns
|
|
|
|
|
|
|
|
|
|
def check_allowed_displayed_fields
|
|
|
|
|
displayed_fields.each do |field|
|
|
|
|
|
table = field['table']
|
|
|
|
|
column = field['column']
|
2018-10-04 16:41:09 +02:00
|
|
|
|
if !dossier_field_service.valid_column?(procedure, table, column)
|
2018-10-02 17:04:23 +02:00
|
|
|
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_allowed_sort_column
|
|
|
|
|
table = sort['table']
|
|
|
|
|
column = sort['column']
|
|
|
|
|
if !valid_sort_column?(procedure, table, column)
|
|
|
|
|
errors.add(:sort, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_allowed_filter_columns
|
|
|
|
|
filters.each do |_, columns|
|
|
|
|
|
columns.each do |column|
|
|
|
|
|
table = column['table']
|
|
|
|
|
column = column['column']
|
2018-10-04 16:41:09 +02:00
|
|
|
|
if !dossier_field_service.valid_column?(procedure, table, column)
|
2018-10-02 17:04:23 +02:00
|
|
|
|
errors.add(:filters, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2018-10-04 16:41:09 +02:00
|
|
|
|
def dossier_field_service
|
|
|
|
|
@dossier_field_service ||= DossierFieldService.new
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-02 17:04:23 +02:00
|
|
|
|
def valid_sort_column?(procedure, table, column)
|
2018-10-04 16:41:09 +02:00
|
|
|
|
dossier_field_service.valid_column?(procedure, table, column) || EXTRA_SORT_COLUMNS[table]&.include?(column)
|
2018-10-02 17:04:23 +02:00
|
|
|
|
end
|
2017-10-02 17:03:30 +02:00
|
|
|
|
end
|