Skip to content

Commit 1975a05

Browse files
committed
Extract CollectionComponent and NonCollectionComponent from DocumentComponent
This eliminates a number of conditionals around whether the document is a collection or not.
1 parent aa0db80 commit 1975a05

5 files changed

+148
-40
lines changed

app/components/arclight/document_component.html.erb app/components/arclight/collection_component.html.erb

+20-34
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
<%= render Arclight::DocumentActionsComponent.new(presenter: presenter) %>
1414
</div>
1515

16-
17-
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.component_metadata_partials || [], metadata_attr: { layout: Arclight::UpperMetadataLayoutComponent }, presenter: presenter, classes: ['al-metadata-section', 'breadcrumb-item', "breadcrumb-item-#{document.parents.length + 3}"]) unless document.collection? %>
18-
1916
<div class='row'>
2017
<div class='col-md-12'>
2118
<%= embed %>
@@ -27,25 +24,20 @@
2724
<ul class='nav nav-tabs nav-fill' role='tablist' aria-label='<%= t('arclight.views.show.tablist_nav') %>'>
2825
<li class='nav-item flex-fill'>
2926
<a class='nav-link p-1 p-sm-2 active' data-toggle='tab' data-bs-toggle='tab' href='#context' role='tab'>
30-
<%= document.collection? ? t('arclight.views.show.overview') : t('arclight.views.show.context') %>
27+
<%= t('arclight.views.show.overview') %>
3128
</a>
3229
</li>
3330

34-
<% if document.collection? %>
3531
<li class='nav-item flex-fill'>
36-
<a class='nav-link p-1 p-sm-2 disabled' data-toggle='tab' data-bs-toggle='tab' href='#contents' role='tab' data-hierarchy-enable-me='true'>
32+
<a class='nav-link p-1 p-sm-2 disabled' data-toggle='tab' data-bs-toggle='tab' href='#contents' role='tab' data-hierarchy-enable-me='true'>
3733
<%= t 'arclight.views.show.no_contents' %>
38-
</a>
34+
</a>
3935
</li>
40-
<% end %>
4136

4237
<% if online_content? %>
4338
<li class='nav-item flex-fill'>
4439
<a class='nav-link p-1 p-sm-2' data-toggle='tab' data-bs-toggle='tab' href='#online-content' role='tab' data-arclight-online-content-tab='true'>
4540
<%= t 'arclight.views.show.online_content' %>
46-
<% unless document.collection? %>
47-
<span data-arclight-online-content-tab-count/>
48-
<% end %>
4941
</a>
5042
</li>
5143
<% end %>
@@ -62,34 +54,32 @@
6254
<div class='row'>
6355
<div class='col-md-12'>
6456
<div class='tab-content'>
65-
<% if document.collection? %>
66-
<div class='tab-pane active' id='context' role='tabpanel'>
67-
<div class="row">
68-
<div class="col-md-3 order-md-2">
57+
<div class='tab-pane active' id='context' role='tabpanel'>
58+
<div class="row">
59+
<div class="col-md-3 order-md-2">
6960
<nav class='al-sidebar-navigation-context al-sticky-sidebar'
70-
id="collection-context-sidebar"
71-
aria-label='<%= t('arclight.views.show.context_nav.title') %>'>
72-
<ul class='nav nav-pills flex-sm-row flex-md-column text-sm-left text-md-right'>
61+
id="collection-context-sidebar"
62+
aria-label='<%= t('arclight.views.show.context_nav.title') %>'>
63+
<ul class='nav nav-pills flex-sm-row flex-md-column text-sm-left text-md-right'>
7364
<% blacklight_config.show.metadata_partials&.select { |item| presenter.with_field_group(item).fields_to_render.any? }&.each do |item| %>
74-
<li class='nav-item'>
65+
<li class='nav-item'>
7566
<%= link_to t("arclight.views.show.sections.#{item}"), "##{t("arclight.views.show.sections.#{item}").parameterize}", class: 'nav-link' %>
76-
</li>
67+
</li>
7768
<% end %>
78-
</ul>
69+
</ul>
7970
</nav>
80-
</div>
71+
</div>
8172

82-
<div class="col-md-9 order-md-1">
73+
<div class="col-md-9 order-md-1">
8374
<h2 class="sr-only visually-hidden"><%= t 'arclight.views.show.context' %></h2>
8475
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.metadata_partials || [], presenter: presenter, heading: true) %>
85-
</div>
8676
</div>
8777
</div>
88-
<% end %>
78+
</div>
8979

90-
<div class='tab-pane <%= document.collection? ? '': 'active' %>' id='<%= document.collection? ? 'contents' : 'context' %>' role='tabpanel'>
91-
<h2 class="sr-only visually-hidden" data-sr-enable-me='true' data-has-contents=<%= t 'arclight.views.show.has_content' %>><%= document.collection? ? t('arclight.views.show.has_content') : t('arclight.views.show.context') %></h2>
92-
<%= content_tag(:div, id: document.collection? ? nil : t("arclight.views.show.sections.collection_context_field").parameterize) do %>
80+
<div class='tab-pane' id='contents' role='tabpanel'>
81+
<h2 class="sr-only visually-hidden" data-sr-enable-me='true' data-has-contents=<%= t 'arclight.views.show.has_content' %>><%= t('arclight.views.show.has_content') %></h2>
82+
<%= content_tag(:div) do %>
9383
<%= generic_context_navigation(@document) %>
9484
<% end %>
9585
</div>
@@ -106,7 +96,7 @@
10696
name: document.collection_name,
10797
access: 'online',
10898
view: 'online_contents',
109-
parent: document.collection? ? nil : document.reference,
99+
parent: nil,
110100
search_field: 'within_collection'
111101
}
112102
}
@@ -117,11 +107,7 @@
117107
<div class='tab-pane' id='access' role='tabpanel'>
118108
<h2 class="sr-only visually-hidden"><%= t 'arclight.views.show.access' %></h2>
119109

120-
<% if @document.collection? %>
121-
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.context_access_tab_items || [], presenter: presenter) %>
122-
<% else %>
123-
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.component_access_tab_items || [], presenter: presenter) %>
124-
<% end %>
110+
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.context_access_tab_items || [], presenter: presenter) %>
125111
</div>
126112
</div>
127113
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Arclight
4+
# Render a single collection document
5+
class CollectionComponent < Blacklight::DocumentComponent
6+
def blacklight_config
7+
presenter.configuration
8+
end
9+
attr_reader :document
10+
11+
delegate :online_content?, to: :document
12+
end
13+
end

app/components/arclight/document_component.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
module Arclight
44
# Render a single document
55
class DocumentComponent < Blacklight::DocumentComponent
6-
attr_reader :document
7-
8-
def online_content?
9-
document.online_content? && (document.collection? || document.children?)
6+
def initialize(document:, **kwargs) # rubocop:disable Lint/MissingSuper
7+
@document = document
8+
@kwargs = kwargs
109
end
1110

12-
def blacklight_config
13-
presenter.configuration
11+
attr_reader :document
12+
13+
def call
14+
if document.collection?
15+
render CollectionComponent.new(document: document, **@kwargs)
16+
else
17+
render NonCollectionComponent.new(document: document, **@kwargs)
18+
end
1419
end
1520
end
1621
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<div class='d-md-flex justify-content-between al-show'>
2+
<div class='al-show-breadcrumb'>
3+
<nav aria-label="breadcrumb">
4+
<%= render Arclight::BreadcrumbsHierarchyComponent.new(presenter: presenter) %>
5+
6+
<%= content_tag :h1, class: "breadcrumb-item breadcrumb-item-#{document.parents.length + 2} media" do %>
7+
<span class="media-body" aria-hidden="true"><%= blacklight_icon document_or_parent_icon(document) %></span>
8+
<span class="col"><%= document.normalized_title %></span>
9+
<% end %>
10+
</nav>
11+
</div>
12+
13+
<%= render Arclight::DocumentActionsComponent.new(presenter: presenter) %>
14+
</div>
15+
16+
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.component_metadata_partials || [], metadata_attr: { layout: Arclight::UpperMetadataLayoutComponent }, presenter: presenter, classes: ['al-metadata-section', 'breadcrumb-item', "breadcrumb-item-#{document.parents.length + 3}"]) %>
17+
18+
<div class='row'>
19+
<div class='col-md-12'>
20+
<%= embed %>
21+
</div>
22+
</div>
23+
24+
<div class='row'>
25+
<div class='col-md-12'>
26+
<ul class='nav nav-tabs nav-fill' role='tablist' aria-label='<%= t('arclight.views.show.tablist_nav') %>'>
27+
<li class='nav-item flex-fill'>
28+
<a class='nav-link p-1 p-sm-2 active' data-toggle='tab' data-bs-toggle='tab' href='#context' role='tab'>
29+
<%= t('arclight.views.show.context') %>
30+
</a>
31+
</li>
32+
33+
<% if online_content? %>
34+
<li class='nav-item flex-fill'>
35+
<a class='nav-link p-1 p-sm-2' data-toggle='tab' data-bs-toggle='tab' href='#online-content' role='tab' data-arclight-online-content-tab='true'>
36+
<%= t 'arclight.views.show.online_content' %>
37+
<span data-arclight-online-content-tab-count/>
38+
</a>
39+
</li>
40+
<% end %>
41+
42+
<li class='nav-item flex-fill'>
43+
<a class='nav-link p-1 p-sm-2' data-toggle='tab' data-bs-toggle='tab' href='#access' role='tab'>
44+
<%= t 'arclight.views.show.access' %>
45+
</a>
46+
</li>
47+
</ul>
48+
</div>
49+
</div>
50+
51+
<div class='row'>
52+
<div class='col-md-12'>
53+
<div class='tab-content'>
54+
<div class='tab-pane active' id='context' role='tabpanel'>
55+
<h2 class="sr-only visually-hidden" data-sr-enable-me='true' data-has-contents=<%= t 'arclight.views.show.has_content' %>><%= t('arclight.views.show.context') %></h2>
56+
<%= content_tag(:div, id: t("arclight.views.show.sections.collection_context_field").parameterize) do %>
57+
<%= generic_context_navigation(@document) %>
58+
<% end %>
59+
</div>
60+
61+
<% if online_content? %>
62+
<div class='tab-pane' id='online-content' role='tabpanel'>
63+
<h2 class="sr-only visually-hidden"><%= t 'arclight.views.show.online_content' %></h2>
64+
<%= content_tag(
65+
:div, '',
66+
class: 'al-contents',
67+
data: {
68+
arclight: {
69+
path: search_catalog_path,
70+
name: document.collection_name,
71+
access: 'online',
72+
view: 'online_contents',
73+
parent: document.reference,
74+
search_field: 'within_collection'
75+
}
76+
}
77+
) %>
78+
</div>
79+
<% end %>
80+
81+
<div class='tab-pane' id='access' role='tabpanel'>
82+
<h2 class="sr-only visually-hidden"><%= t 'arclight.views.show.access' %></h2>
83+
84+
<%= render Arclight::MetadataSectionComponent.with_collection(blacklight_config.show.component_access_tab_items || [], presenter: presenter) %>
85+
</div>
86+
</div>
87+
</div>
88+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module Arclight
4+
# Render a single non-collection document
5+
class NonCollectionComponent < Blacklight::DocumentComponent
6+
attr_reader :document
7+
8+
def blacklight_config
9+
presenter.configuration
10+
end
11+
12+
def online_content?
13+
document.online_content? && document.children?
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)