demarches-normaliennes/app/models/column.rb

60 lines
2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2024-08-19 14:34:36 +02:00
class Column
# include validations to enable procedure_presentation.validate_associate,
# which enforces the deserialization of columns in the displayed_columns attribute
# and raises an error if a column is not found
include ActiveModel::Validations
TYPE_DE_CHAMP_TABLE = 'type_de_champ'
attr_reader :table, :column, :label, :type, :scope, :value_column, :filterable, :displayable
2024-07-19 18:03:15 +02:00
def initialize(procedure_id:, table:, column:, label: nil, type: :text, value_column: :value, filterable: true, displayable: true, scope: '')
2024-10-07 21:46:59 +02:00
@procedure_id = procedure_id
@table = table
@column = column
@label = label || I18n.t(column, scope: [:activerecord, :attributes, :procedure_presentation, :fields, table])
@type = type
@scope = scope
@value_column = value_column
@filterable = filterable
2024-09-02 17:25:46 +02:00
@displayable = displayable
end
2024-10-09 09:21:44 +02:00
# the id is a String to be used in forms
def id = h_id.to_json
2024-10-09 09:21:44 +02:00
# the h_id is a Hash and hold enough information to find the column
# in the ColumnType class, aka be able to do the h_id -> column conversion
2024-10-31 18:28:47 +01:00
def h_id = { procedure_id: @procedure_id, column_id: }
2024-10-09 09:21:44 +02:00
def ==(other) = h_id == other.h_id # using h_id instead of id to avoid inversion of keys
def to_json
{
table:, column:, label:, type:, scope:, value_column:, filterable:, displayable:
}
end
2024-10-07 15:00:22 +02:00
2024-10-09 15:16:13 +02:00
def notifications? = [table, column] == ['notifications', 'notifications']
def dossier_state? = [table, column] == ['self', 'state']
2024-10-28 10:09:39 +01:00
def groupe_instructeur? = [table, column] == ['groupe_instructeur', 'id']
2024-10-07 16:52:27 +02:00
def dossier_labels? = [table, column] == ['dossier_labels', 'label_id']
2024-10-28 10:09:39 +01:00
def type_de_champ? = table == TYPE_DE_CHAMP_TABLE
2024-10-07 15:00:22 +02:00
def self.find(h_id)
2024-10-11 11:23:36 +02:00
begin
procedure = Procedure.with_discarded.find(h_id[:procedure_id])
rescue ActiveRecord::RecordNotFound
raise ActiveRecord::RecordNotFound.new("Column: unable to find procedure #{h_id[:procedure_id]} from h_id #{h_id}")
end
procedure.find_column(h_id: h_id)
2024-10-07 15:00:22 +02:00
end
2024-10-29 16:38:17 +01:00
private
2024-10-31 18:28:47 +01:00
def column_id = "#{table}/#{column}"
end