Skip to content

Commit f1088e4

Browse files
committed
Add generic document header, section, and footer components to render content into documents.
1 parent 09170e0 commit f1088e4

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

app/components/blacklight/document_component.html.erb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
itemtype: document.itemtype,
99
class: classes.flatten.join(' ') do %>
1010
<%= header %>
11+
<% header_components.each do |component| %>
12+
<%= render component.new(presenter: @presenter, counter: @counter, document_counter: @document_counter) %>
13+
<% end %>
14+
1115
<% if body.present? %>
1216
<%= body %>
1317
<% else %>
1418
<div class="document-main-section">
1519
<%= title %>
16-
<%= embed %>
17-
<%= content %>
18-
<%= metadata %>
20+
21+
<% section_components.each do |section| %>
22+
<%= section %>
23+
<% end %>
24+
1925
<% metadata_sections.each do |section| %>
2026
<%= section %>
2127
<% end %>
@@ -27,5 +33,10 @@
2733

2834
<%= thumbnail %>
2935
<% end %>
36+
37+
<% footer_components.each do |component| %>
38+
<%= render component.new(presenter: @presenter, counter: @counter, document_counter: @document_counter) %>
39+
<% end %>
40+
3041
<%= footer %>
3142
<% end %>

app/components/blacklight/document_component.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ def before_render
127127
end
128128
end
129129

130+
def header_components
131+
view_config.document_header_components
132+
end
133+
134+
def section_components
135+
return to_enum(:section_components) unless block_given?
136+
137+
view_config.document_section_components&.each do |component_or_key|
138+
case component_or_key
139+
when Symbol, String
140+
yield public_send(component_or_key)
141+
when Class
142+
yield render component_or_key.new(presenter: @presenter, counter: @counter, document_counter: @document_counter)
143+
end
144+
end
145+
end
146+
147+
def footer_components
148+
view_config.document_footer_components
149+
end
150+
130151
private
131152

132153
delegate :view_config, to: :@presenter

lib/blacklight/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ def initialized_default_configuration?
156156
document_metadata_component: Blacklight::DocumentMetadataComponent,
157157
document_thumbnail_component: Blacklight::Document::ThumbnailComponent,
158158
document_title_component: Blacklight::DocumentTitleComponent,
159+
document_header_components: [],
160+
document_section_components: [:embed, :content, :metadata],
161+
document_footer_components: [],
159162
sidebar_component: Blacklight::Search::SidebarComponent,
160163
dropdown_component: Blacklight::System::DropdownComponent,
161164
# solr field to use to render a document title

lib/blacklight/configuration/view_config.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class ViewConfig < Blacklight::OpenStructWithHashAccess
88
# @return [Class] document presenter class used by helpers and views
99
# @!attribute document_component
1010
# @return [Class] component class used to render a document; defaults to Blacklight::DocumentComponent
11+
# @!attribute document_header_components
12+
# @return [Array<Class>] component classes that render above the main document section
13+
# @!attribute document_section_components
14+
# @return [Array<Symbol, String, Class>] component classes (or slot names) that render the main document section below the title
15+
# @!attribute document_footer_components
16+
# @return [Array<Class>] component classes that render below the main document section
1117
# @!attribute document_title_component
1218
# @return [Class] component class used to render a document title
1319
# @!attribute document_metadata_component

spec/components/blacklight/document_component_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,30 @@ def call
269269
expect(page).to have_content "Prefix!"
270270
end
271271
end
272+
273+
context 'with configured components' do
274+
let(:static_component) do
275+
lambda { |text|
276+
Class.new(ViewComponent::Base) do
277+
class_attribute :text
278+
def self.name = 'StaticComponent'
279+
def initialize(**); end
280+
281+
def call
282+
text.html_safe
283+
end
284+
end.tap { |klass| klass.text = text }
285+
}
286+
end
287+
288+
it 'renders header, section, and footer components' do
289+
blacklight_config.index.document_header_components = [static_component.call('Header')]
290+
blacklight_config.index.document_section_components = [static_component.call('Section')]
291+
blacklight_config.index.document_footer_components = [static_component.call('Footer')]
292+
293+
render_inline component
294+
puts page.native.inner_html
295+
expect(page).to have_content('Header').and have_content('Section').and have_content('Footer')
296+
end
297+
end
272298
end

0 commit comments

Comments
 (0)