Skip to content

Commit

Permalink
Extract components for document behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Sep 29, 2020
1 parent 60a88c3 commit 3baca35
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="card">
<div class="card-header">More Like This</div>
<div class="card-body">
<ul>
<%= @document.more_like_this.each do |document| %>
<li class="more_like_this_document">
<span class="mlt_title"><%= link_to_document document %></span>
</li>
<% end %>
</ul>
</div>
</div>
21 changes: 21 additions & 0 deletions app/components/blacklight/document/more_like_this_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Blacklight
module Document
class MoreLikeThisComponent < ::ViewComponent::Base
with_collection_parameter :document

def initialize(document:)
@document = document
end

def render?
@document.more_like_this.present?
end

def link_to_document(*args)
@view_context.link_to_document(*args)
end
end
end
end
10 changes: 10 additions & 0 deletions app/components/blacklight/search_context_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class='pagination-search-widgets'>

<div class="page-links">
<%= link_to_previous_document @search_context[:prev] %> |

<%= item_page_entry_info %> |

<%= link_to_next_document @search_context[:next] %>
</div>
</div>
30 changes: 30 additions & 0 deletions app/components/blacklight/search_context_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Blacklight
class SearchContextComponent < ::ViewComponent::Base
with_collection_parameter :search_context

def initialize(search_context:, search_session:)
@search_context = search_context
@search_session = search_session
end

def render?
@search_context.present? && (@search_context[:prev] || @search_context[:next])
end

def item_page_entry_info
Deprecation.silence(Blacklight::CatalogHelperBehavior) do
@view_context.item_page_entry_info
end
end

def link_to_previous_document(*args)
@view_context.link_to_previous_document(*args)
end

def link_to_next_document(*args)
@view_context.link_to_next_document(*args)
end
end
end
1 change: 1 addition & 0 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def item_page_entry_info
total: number_with_delimiter(search_session['total']),
count: search_session['total'].to_i).html_safe
end
deprecation_deprecate item_page_entry_info: 'Use Blacklight::SearchContextComponent methods instead'

##
# Look up search field user-displayable label
Expand Down
13 changes: 1 addition & 12 deletions app/views/catalog/_previous_next_doc.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
<% if @search_context[:prev] || @search_context[:next] %>
<div class='pagination-search-widgets'>

<div class="page-links">
<%= link_to_previous_document @search_context[:prev] %> |

<%= item_page_entry_info %> |

<%= link_to_next_document @search_context[:next] %>
</div>
</div>
<% end %>
<%= render(Blacklight::SearchContextComponent.new(search_context: @search_context, search_session: search_session)) %>
12 changes: 1 addition & 11 deletions app/views/catalog/_show_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
<%= render 'show_tools' %>
<% unless document.more_like_this.empty? %>
<div class="card">
<div class="card-header">More Like This</div>
<div class="card-body">
<%= render collection: document.more_like_this,
partial: 'show_more_like_this',
as: :document %>
</div>
</div>
<% end %>
<%= render(Blacklight::Document::MoreLikeThisComponent.new(document: document)) %>
4 changes: 4 additions & 0 deletions spec/views/catalog/_previous_next_doc.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

RSpec.describe "catalog/_previous_next_doc.html.erb" do
before do
allow(view).to receive(:search_session).and_return({})
end

it "without next or previous does not render content" do
assign(:search_context, {})
render
Expand Down

0 comments on commit 3baca35

Please sign in to comment.