Skip to content

Commit

Permalink
Extract a citation component
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Sep 29, 2020
1 parent b1ed684 commit d2ec950
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<h1 class="modal-title"><%= title %></h1>

<%= @formats.each do |i18n_key, citation_method| %>
<h2><%= t(i18n_key) %></h2>
<%= @document.send(citation_method).html_safe %>
<% unless @formats.keys.last === i18n_key %><br/><br/><% end %>
<% end %>
</div>
24 changes: 24 additions & 0 deletions app/components/blacklight/document/citation_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Blacklight
module Document
class CitationComponent < ::ViewComponent::Base
DEFAULT_FORMATS = {
'blacklight.citation.mla': :export_as_mla_citation_txt,
'blacklight.citation.apa': :export_as_apa_citation_txt,
'blacklight.citation.chicago': :export_as_chicago_citation_txt
}.freeze

with_collection_parameter :document

def initialize(document:, formats: DEFAULT_FORMATS)
@document = document
@formats = formats.select { |_k, v| @document.respond_to?(v) }
end

def title
@view_context.document_heading(@document)
end
end
end
end
19 changes: 1 addition & 18 deletions app/views/catalog/_citation.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
<%= render Blacklight::System::ModalComponent.new do |component| %>
<% component.with(:title, t('blacklight.tools.citation')) %>
<% @documents&.each do |document| %>
<h1 class="modal-title"><%= document_heading(document) %></h1>

<% if document.respond_to?(:export_as_mla_citation_txt) %>
<h2><%= t('blacklight.citation.mla') %></h2>
<%= document.send(:export_as_mla_citation_txt).html_safe %><br/><br/>
<% end %>
<% if document.respond_to?(:export_as_apa_citation_txt) %>
<h2><%= t('blacklight.citation.apa') %></h2>
<%= document.send(:export_as_apa_citation_txt).html_safe %><br/><br/>
<% end %>
<% if document.respond_to?(:export_as_chicago_citation_txt) %>
<h2><%= t('blacklight.citation.chicago') %></h2>
<%= document.send(:export_as_chicago_citation_txt).html_safe %>
<% end %>
<% end %>
<%= render Blacklight::Document::CitationComponent.with_collection(@documents) if @documents.present? %>
<% end %>

0 comments on commit d2ec950

Please sign in to comment.