-
Notifications
You must be signed in to change notification settings - Fork 118
/
active_fedora_dummy_model.rb
80 lines (72 loc) · 1.82 KB
/
active_fedora_dummy_model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
module Hyrax
##
# @api public
#
# Given a model class and an +id+, provides +ActiveModel+ style methods. This
# is a tool for providing route resolution and other +ActiveModel+ behavior
# for +ActiveFedora+ without loading the object from the fedora backend.
#
# @note this was originally implemented for +SolrDocument+ as
# +Hyrax::SolrDocumentBehavior::ModelWrapper+, but is useful in the more
# general case that we know the model class and id, but don't have a full
# model object.
#
class ActiveFedoraDummyModel
##
# @api public
#
# @param [Class] model
# @param [String, nil] id
def initialize(model, id)
@model = model
@id = id
end
##
# @api public
def persisted?
true
end
##
# @api public
def to_param
@id
end
##
# @api public
def to_key
[@id]
end
##
# @api public
def model_name
@model.model_name
end
##
# @api public
# @return [String]
def human_readable_type
@model.human_readable_type
end
##
# @api public
#
# @note uses the @model's `._to_partial_path` if implemented, otherwise
# constructs a default
def to_partial_path
return @model._to_partial_path if
@model.respond_to?(:_to_partial_path)
"hyrax/#{model_name.collection}/#{model_name.element}"
end
##
# @api public
def to_global_id
# this covers the use case of creating a non Valkyrie::Resource, while using Valkyrie
if model_name.name.constantize <= Valkyrie::Resource
URI::GID.build app: GlobalID.app, model_name: Hyrax::ValkyrieGlobalIdProxy.to_s, model_id: @id
else
URI::GID.build app: GlobalID.app, model_name: model_name.name, model_id: @id
end
end
end
end