/
work_show_presenter.rb
145 lines (119 loc) · 5.33 KB
/
work_show_presenter.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
module CurationConcerns
class WorkShowPresenter
include ModelProxy
include PresentsAttributes
attr_accessor :solr_document, :current_ability, :request
class_attribute :collection_presenter_class, :file_presenter_class, :work_presenter_class
# modify this attribute to use an alternate presenter class for the collections
self.collection_presenter_class = CollectionPresenter
# modify this attribute to use an alternate presenter class for the files
self.file_presenter_class = FileSetPresenter
# modify this attribute to use an alternate presenter class for the child works
self.work_presenter_class = self
# Methods used by blacklight helpers
delegate :has?, :first, :fetch, :export_formats, :export_as, to: :solr_document
# @param [SolrDocument] solr_document
# @param [Ability] current_ability
# @param [ActionDispatch::Request] request the http request context
def initialize(solr_document, current_ability, request = nil)
@solr_document = solr_document
@current_ability = current_ability
@request = request
end
def page_title
title.first
end
# CurationConcern methods
delegate :stringify_keys, :human_readable_type, :collection?, :representative_id, :to_s,
to: :solr_document
# Metadata Methods
delegate :title, :date_created, :date_modified, :date_uploaded, :description,
:creator, :contributor, :subject, :publisher, :language, :embargo_release_date,
:lease_expiration_date, :rights, :source, :thumbnail_id, :representative_id,
to: :solr_document
# @return [Array<FileSetPresenter>] presenters for the orderd_members that are FileSets
def file_set_presenters
@file_set_presenters ||= member_presenters(ordered_ids & file_set_ids)
end
def workflow
@workflow ||= WorkflowPresenter.new(solr_document, current_ability)
end
# @return FileSetPresenter presenter for the representative FileSets
def representative_presenter
return nil if representative_id.blank?
@representative_presenter ||=
begin
result = member_presenters([representative_id]).first
if result.respond_to?(:representative_presenter)
result.representative_presenter
else
result
end
end
end
# @return [Array<WorkShowPresenter>] presenters for the ordered_members that are not FileSets
def work_presenters
@work_presenters ||= member_presenters(ordered_ids - file_set_ids, work_presenter_class)
end
# @param [Array<String>] ids a list of ids to build presenters for
# @param [Class] presenter_class the type of presenter to build
# @return [Array<presenter_class>] presenters for the ordered_members (not filtered by class)
def member_presenters(ids = ordered_ids, presenter_class = composite_presenter_class)
PresenterFactory.build_presenters(ids,
presenter_class,
*presenter_factory_arguments)
end
def composite_presenter_class
CompositePresenterFactory.new(file_presenter_class, work_presenter_class, ordered_ids & file_set_ids)
end
# @return [Array<CollectionPresenter>] presenters for the collections that this work is a member of
def collection_presenters
PresenterFactory.build_presenters(in_collection_ids,
collection_presenter_class,
*presenter_factory_arguments)
end
def link_name
current_ability.can?(:read, id) ? to_s : 'File'
end
def export_as_nt
graph.dump(:ntriples)
end
def export_as_jsonld
graph.dump(:jsonld, standard_prefixes: true)
end
def export_as_ttl
graph.dump(:ttl)
end
private
def graph
GraphExporter.new(solr_document, request).fetch
end
def presenter_factory_arguments
[current_ability, request]
end
# @return [Array<String>] ids of the collections that this work is a member of
def in_collection_ids
ActiveFedora::SolrService.query("{!field f=member_ids_ssim}#{id}",
fl: ActiveFedora.id_field)
.map { |x| x.fetch(ActiveFedora.id_field) }
end
# TODO: Extract this to ActiveFedora::Aggregations::ListSource
def ordered_ids
@ordered_ids ||= begin
ActiveFedora::SolrService.query("proxy_in_ssi:#{id}",
fl: "ordered_targets_ssim")
.flat_map { |x| x.fetch("ordered_targets_ssim", []) }
end
end
# These are the file sets that belong to this work, but not necessarily
# in order.
def file_set_ids
@file_set_ids ||= begin
ActiveFedora::SolrService.query("{!field f=has_model_ssim}FileSet",
fl: ActiveFedora.id_field,
fq: "{!join from=ordered_targets_ssim to=id}id:\"#{id}/list_source\"")
.flat_map { |x| x.fetch(ActiveFedora.id_field, []) }
end
end
end
end