2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-22 21:49:31 +01:00
|
|
|
class ApplicationRecord < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
2018-11-20 22:59:51 +01:00
|
|
|
|
|
|
|
def self.record_from_typed_id(id)
|
|
|
|
class_name, record_id = GraphQL::Schema::UniqueWithinType.decode(id)
|
|
|
|
|
2022-11-01 12:39:11 +01:00
|
|
|
if class_name == 'Dossier'
|
|
|
|
Dossier.visible_by_administration.find(record_id)
|
|
|
|
elsif defined?(class_name)
|
2018-11-20 22:59:51 +01:00
|
|
|
Object.const_get(class_name).find(record_id)
|
|
|
|
else
|
|
|
|
raise ActiveRecord::RecordNotFound, "Unexpected object: #{class_name}"
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
raise ActiveRecord::RecordNotFound, e.message
|
|
|
|
end
|
|
|
|
|
2021-01-28 13:53:01 +01:00
|
|
|
def self.id_from_typed_id(id)
|
|
|
|
GraphQL::Schema::UniqueWithinType.decode(id)[1]
|
|
|
|
end
|
|
|
|
|
2023-02-23 11:00:09 +01:00
|
|
|
def self.stable_id_from_typed_id(prefixed_typed_id)
|
|
|
|
return nil unless prefixed_typed_id.starts_with?("champ_")
|
|
|
|
|
|
|
|
self.id_from_typed_id(prefixed_typed_id.gsub("champ_", "")).to_i
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-11-20 22:59:51 +01:00
|
|
|
def to_typed_id
|
|
|
|
GraphQL::Schema::UniqueWithinType.encode(self.class.name, id)
|
|
|
|
end
|
2023-02-22 19:32:25 +01:00
|
|
|
|
|
|
|
def to_typed_id_for_query
|
|
|
|
to_typed_id.delete("==")
|
|
|
|
end
|
2016-12-22 21:49:31 +01:00
|
|
|
end
|