demarches-normaliennes/app/models/application_record.rb

20 lines
523 B
Ruby
Raw Normal View History

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)
if defined?(class_name)
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
def to_typed_id
GraphQL::Schema::UniqueWithinType.encode(self.class.name, id)
end
2016-12-22 21:49:31 +01:00
end