-
Notifications
You must be signed in to change notification settings - Fork 253
/
render_partials_helper.rb
205 lines (186 loc) · 7.14 KB
/
render_partials_helper.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# frozen_string_literal: true
module Blacklight::RenderPartialsHelper
##
# Render the document index view
#
# @param [Array<SolrDocument>] documents list of documents to render
# @param [Hash] locals to pass to the render call
# @return [String]
def render_document_index documents = nil, locals = {}
documents ||= @document_list
render_document_index_with_view(document_index_view_type, documents, locals)
end
##
# Render the document index for a grouped response
def render_grouped_document_index
render 'catalog/group_default'
end
##
# Return the list of partials for a given solr document
# @param [SolrDocument] doc solr document to render partials for
# @param [Array<String>] partials list of partials to render
# @param [Hash] locals local variables to pass to the render call
# @return [String]
def render_document_partials(doc, partials = [], locals = {})
safe_join(partials.map do |action_name|
render_document_partial(doc, action_name, locals)
end, "\n")
end
##
# Given a doc and a base name for a partial, this method will attempt to render
# an appropriate partial based on the document format and view type.
#
# If a partial that matches the document format is not found,
# render a default partial for the base name.
#
# @see #document_partial_path_templates
#
# @param [SolrDocument] doc
# @param [String] base_name base name for the partial
# @param [Hash] locals local variables to pass through to the partials
def render_document_partial(doc, base_name, locals = {})
format = document_partial_name(doc, base_name)
view_type = document_index_view_type
template = cached_view ['show', view_type, base_name, format].join('_') do
find_document_show_template_with_view(view_type, base_name, format, locals)
end
if template
template.render(self, locals.merge(document: doc))
else
''
end
end
##
# Render the document index for the given view type with the
# list of documents.
#
# This method will interpolate the list of templates with
# the current view, and gracefully handles missing templates.
#
# @see #document_index_path_templates
#
# @param [String] view type
# @param [Array<SolrDocument>] documents list of documents to render
# @param [Hash] locals to pass to the render call
# @return [String]
def render_document_index_with_view view, documents, locals = {}
template = cached_view ['index', view].join('_') do
find_document_index_template_with_view(view, locals)
end
if template
template.render(self, locals.merge(documents: documents))
else
''
end
end
##
# A list of document partial templates to attempt to render
#
# @see #render_document_index_with_view
# @return [Array<String>]
def document_index_path_templates
# first, the legacy template names for backwards compatbility
# followed by the new, inheritable style
# finally, a controller-specific path for non-catalog subclasses
@document_index_path_templates ||= [
"document_%{index_view_type}",
"catalog/document_%{index_view_type}",
"catalog/document_list"
]
end
protected
##
# Return a partial name for rendering a document
# this method can be overridden in order to transform the value
# (e.g. 'PdfBook' => 'pdf_book')
#
# @param [SolrDocument] document
# @param [String, Array] display_type a value suggestive of a partial
# @return [String] the name of the partial to render
# @example
# type_field_to_partial_name(['a book-article'])
# => 'a_book_article'
if Rails.version >= '5.0.0'
def type_field_to_partial_name(document, display_type)
# using "_" as sep. to more closely follow the views file naming conventions
# parameterize uses "-" as the default sep. which throws errors
underscore = '_'.freeze
Array(display_type).join(' '.freeze).tr('-'.freeze, underscore).parameterize(separator: underscore)
end
else
def type_field_to_partial_name(document, display_type)
# using "_" as sep. to more closely follow the views file naming conventions
# parameterize uses "-" as the default sep. which throws errors
underscore = '_'.freeze
Array(display_type).join(' '.freeze).tr('-'.freeze, underscore).parameterize(underscore)
end
end
##
# Return a normalized partial name for rendering a single document
#
# @param [SolrDocument] document
# @param [Symbol] base_name base name for the partial
# @return [String]
def document_partial_name(document, base_name = nil)
view_config = blacklight_config.view_config(:show)
display_type = if base_name and view_config.key? :"#{base_name}_display_type_field"
document[view_config[:"#{base_name}_display_type_field"]]
end
display_type ||= document[view_config.display_type_field]
display_type ||= 'default'
type_field_to_partial_name(document, display_type)
end
##
# A list of document partial templates to try to render for a document
#
# The partial names will be interpolated with the following variables:
# - action_name: (e.g. index, show)
# - index_view_type: (the current view type, e.g. list, gallery)
# - format: the document's format (e.g. book)
#
# @see #render_document_partial
def document_partial_path_templates
# first, the legacy template names for backwards compatbility
# followed by the new, inheritable style
# finally, a controller-specific path for non-catalog subclasses
@partial_path_templates ||= [
"%{action_name}_%{index_view_type}_%{format}",
"%{action_name}_%{index_view_type}_default",
"%{action_name}_%{format}",
"%{action_name}_default",
"catalog/%{action_name}_%{format}",
"catalog/_%{action_name}_partials/%{format}",
"catalog/_%{action_name}_partials/default"
]
end
private
def find_document_show_template_with_view view_type, base_name, format, locals
document_partial_path_templates.each do |str|
partial = str % { action_name: base_name, format: format, index_view_type: view_type }
logger.debug "Looking for document partial #{partial}"
template = lookup_context.find_all(partial, lookup_context.prefixes + [""], true, locals.keys + [:document], {}).first
return template if template
end
nil
end
def find_document_index_template_with_view view, locals
document_index_path_templates.each do |str|
partial = str % { index_view_type: view }
logger.debug "Looking for document index partial #{partial}"
template = lookup_context.find_all(partial, lookup_context.prefixes + [""], true, locals.keys + [:documents], {}).first
return template if template
end
nil
end
##
# @param key fetches or writes data to a cache, using the given key.
# @yield the block to evaluate (and cache) if there is a cache miss
def cached_view key
@view_cache ||= {}
if @view_cache.key?(key)
@view_cache[key]
else
@view_cache[key] = yield
end
end
end