2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
class Column
|
2024-10-11 15:29:52 +02:00
|
|
|
# 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
|
|
|
|
|
2024-08-22 12:37:10 +02:00
|
|
|
TYPE_DE_CHAMP_TABLE = 'type_de_champ'
|
|
|
|
|
2024-10-09 09:51:47 +02:00
|
|
|
attr_reader :table, :column, :label, :type, :scope, :value_column, :filterable, :displayable
|
2024-07-19 18:03:15 +02:00
|
|
|
|
2024-10-09 09:51:47 +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
|
2024-07-19 11:16:40 +02:00
|
|
|
@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
|
2024-07-19 11:16:40 +02:00
|
|
|
end
|
|
|
|
|
2024-10-09 09:21:44 +02:00
|
|
|
# the id is a String to be used in forms
|
2024-10-07 15:01:40 +02:00
|
|
|
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-07 15:01:40 +02:00
|
|
|
def h_id = { procedure_id: @procedure_id, column_id: "#{table}/#{column}" }
|
2024-10-09 09:21:44 +02:00
|
|
|
|
2024-10-07 15:01:40 +02:00
|
|
|
def ==(other) = h_id == other.h_id # using h_id instead of id to avoid inversion of keys
|
2024-07-19 11:16:40 +02:00
|
|
|
|
|
|
|
def to_json
|
|
|
|
{
|
2024-10-09 09:51:47 +02:00
|
|
|
table:, column:, label:, type:, scope:, value_column:, filterable:, displayable:
|
2024-07-19 11:16:40 +02:00
|
|
|
}
|
|
|
|
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']
|
|
|
|
def type_de_champ? = table == TYPE_DE_CHAMP_TABLE
|
2024-10-08 19:40:19 +02:00
|
|
|
|
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
|
|
|
|
2024-10-31 21:36:13 +01:00
|
|
|
def value(champ)
|
2024-10-29 16:38:17 +01:00
|
|
|
return if champ.nil?
|
|
|
|
|
2024-10-31 21:36:13 +01:00
|
|
|
value = typed_value(champ)
|
|
|
|
if default_column?
|
|
|
|
cast_value(value, from_type: champ.last_write_column_type, to_type: type)
|
2024-10-29 16:38:17 +01:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-10-31 21:36:13 +01:00
|
|
|
def typed_value(champ)
|
|
|
|
value = string_value(champ)
|
|
|
|
parse_value(value, type: champ.last_write_column_type)
|
2024-10-29 16:38:17 +01:00
|
|
|
end
|
|
|
|
|
2024-10-31 21:36:13 +01:00
|
|
|
def string_value(champ) = champ.public_send(value_column)
|
|
|
|
def default_column? = value_column.in?([:value, :external_id])
|
2024-10-29 16:38:17 +01:00
|
|
|
|
2024-10-31 21:36:13 +01:00
|
|
|
def parse_value(value, type:)
|
2024-10-29 16:38:17 +01:00
|
|
|
return if value.blank?
|
|
|
|
|
|
|
|
case type
|
|
|
|
when :boolean
|
|
|
|
parse_boolean(value)
|
|
|
|
when :integer
|
|
|
|
value.to_i
|
|
|
|
when :decimal
|
|
|
|
value.to_f
|
|
|
|
when :datetime
|
|
|
|
parse_datetime(value)
|
|
|
|
when :date
|
|
|
|
parse_datetime(value)&.to_date
|
|
|
|
when :enums
|
|
|
|
parse_enums(value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_value(value, from_type:, to_type:)
|
|
|
|
return if value.blank?
|
|
|
|
return value if from_type == to_type
|
|
|
|
|
|
|
|
case [from_type, to_type]
|
|
|
|
when [:integer, :decimal] # recast numbers automatically
|
|
|
|
value.to_f
|
|
|
|
when [:decimal, :integer] # may lose some data, but who cares ?
|
|
|
|
value.to_i
|
|
|
|
when [:integer, :text], [:decimal, :text] # number to text
|
|
|
|
value.to_s
|
|
|
|
when [:enum, :enums] # single list can become multi
|
|
|
|
[value]
|
|
|
|
when [:enum, :text] # single list can become text
|
|
|
|
value
|
|
|
|
when [:enums, :enum] # multi list can become single list
|
|
|
|
value.first
|
|
|
|
when [:enums, :text] # multi list can become text
|
|
|
|
value.join(', ')
|
|
|
|
when [:date, :datetime] # date <=> datetime
|
|
|
|
value.to_datetime
|
|
|
|
when [:datetime, :date] # may lose some data, but who cares ?
|
|
|
|
value.to_date
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_boolean(value)
|
|
|
|
case value
|
|
|
|
when 'true', 'on', '1'
|
|
|
|
true
|
|
|
|
when 'false'
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_enums(value)
|
|
|
|
JSON.parse(value)
|
|
|
|
rescue JSON::ParserError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_datetime(value)
|
|
|
|
Time.zone.parse(value)
|
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end
|
2024-07-19 11:16:40 +02:00
|
|
|
end
|