Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rtl functionality to search results #126

Merged
merged 1 commit into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Metrics/MethodLength:
Enabled: false

Metrics/BlockLength:
Max: 28
Max: 31

Style/BlockDelimiters:
Exclude:
Expand Down
2 changes: 2 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ RSpec/MultipleExpectations:
# Configuration parameters: MaxNesting.
RSpec/NestedGroups:
Exclude:
- 'spec/helpers/index_helper_spec.rb'
- 'spec/models/iiif_resource_spec.rb'

# Offense count: 2
Expand All @@ -43,4 +44,5 @@ RSpec/SubjectStub:
# Configuration parameters: IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
- 'spec/presenters/rtl_index_presenter_spec.rb'
- 'spec/presenters/rtl_show_presenter_spec.rb'
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require File.expand_path('../config/application', __FILE__)
require 'rubocop/rake_task'
require 'sneakers/tasks'
require 'solr_wrapper'
require 'solr_wrapper/rake_task'

Rails.application.load_tasks

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def search_across_settings

blacklight_config.add_facet_field 'readonly_language_ssim', label: 'Language'
blacklight_config.add_facet_field 'readonly_format_ssim', label: 'Format'
blacklight_config.show.document_presenter_class = RTLShowPresenter
unique_custom_fields.each do |field|
blacklight_config.add_show_field field.field, label: field.configuration["label"]
end
Expand Down Expand Up @@ -48,6 +47,7 @@ def unique_custom_fields

# solr field configuration for search results/index views
config.index.title_field = 'full_title_ssim'
config.index.display_title_field = 'readonly_title_tesim'
config.add_show_field 'creator_ssim', label: 'Creator'

config.add_search_field 'all_fields', label: 'Everything'
Expand All @@ -60,5 +60,7 @@ def unique_custom_fields
config.add_facet_fields_to_solr_request!
config.add_field_configuration_to_solr_request!
config.response_model = AdjustedGroupedResponse
config.show.document_presenter_class = RTLShowPresenter
config.index.document_presenter_class = RTLIndexPresenter
end
end
18 changes: 18 additions & 0 deletions app/helpers/index_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module IndexHelper
def render_index_document(document)
field = index_presenter(document).label(document_show_link_field(document))
span = []
if !field.is_a?(Array)
span << content_tag(:span, style: 'display: block;', dir: field.dir) do
link_to(field, url_for_document(document), document_link_params(document, {}))
end
else
field.each do |value|
span << content_tag(:span, style: 'display: block;', dir: value.dir) do
link_to(value, url_for_document(document), document_link_params(document, {}))
end
end
end
safe_join span
end
end
43 changes: 43 additions & 0 deletions app/presenters/rtl_index_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class RTLIndexPresenter < ::Blacklight::IndexPresenter
# Override the IndexPresenter label method to render
# the label as an Array if it's multivalued.
def label(field_or_string_or_proc, opts = {})
config = Blacklight::Configuration::NullField.new
value = case field_or_string_or_proc
when Symbol
config = field_config(field_or_string_or_proc)
value_from_symbol(field_or_string_or_proc)
when Proc
field_or_string_or_proc.call(@document, opts)
when String
field_or_string_or_proc
end
value ||= @document.id
label_value(value, config)
end

private

# Checks if the requested field is the title field and if the configured
# display title field exists on the document. If so, it returns the
# display title field value. This method allows pom to properly display
# records with non-standard title fields.
def value_from_symbol(field)
default_title_field = @configuration.index.title_field.to_sym
display_title_field = @configuration.index.display_title_field.to_sym

if field == default_title_field && @document.key?(display_title_field)
@document[display_title_field]
else
@document[field]
end
end

def label_value(value, config)
if value.is_a?(Array) && value.count > 1
value.collect { |v| field_values(config, value: v) }
else
field_values(config, value: value)
end
end
end
25 changes: 25 additions & 0 deletions app/views/catalog/_index_header_default.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%# header bar for doc items in index view -%>
<div class="documentHeader row">
<%# main title container for doc partial view
How many bootstrap columns need to be reserved
for bookmarks control depends on size.
-%>
<% document_actions = capture do %>
<% # bookmark functions for items/docs -%>
<%= render_index_doc_actions document, wrapping_class: "index-document-functions col-sm-3 col-lg-2" %>
<% end %>

<h3 class="index_title document-title-heading <%= document_actions.present? ? "col-sm-9 col-lg-10" : "col-md-12" %>">

<% if counter = document_counter_with_offset(document_counter) %>
<span style="" class="document-counter col-sm-1">
<%= t('blacklight.search.documents.counter', counter: counter) %>
</span>
<% end %>
<div class="col-sm-11">
<%= render_index_document document %>
</div>
</h3>

<%= document_actions %>
</div>
61 changes: 61 additions & 0 deletions spec/helpers/index_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'rails_helper'

describe IndexHelper do
let(:helper) { TestingHelper.new }
let(:index_presenter) { instance_double('RTLIndexPresenter', class: RTLIndexPresenter) }
let(:document) do
{
title: 'title',
alternate_title: 'alternate_title'
}
end
before do
class TestingHelper
include IndexHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Context
end
end
after do
Object.send(:remove_const, :TestingHelper)
end

describe '#render_index_document' do
before do
allow(helper).to receive(:index_presenter).and_return(index_presenter)
allow(helper).to receive(:document_show_link_field).and_return(:title)
allow(helper).to receive(:url_for_document).and_return('link')
allow(helper).to receive(:document_link_params).and_return({})
allow(index_presenter).to receive(:label).and_return(label)
end

context 'when given a ltr label' do
let(:label) { 'title' }

it 'returns a single ltr span tag' do
tag = helper.render_index_document(document)
expect(tag).to eq '<span style="display: block;" dir="ltr"><a href="link">title</a></span>'
end
end

context 'when given a rtl label' do
let(:label) { 'تضيح المقال' }

it 'returns a single rtl span tag' do
tag = helper.render_index_document(document)
expect(tag).to eq '<span style="display: block;" dir="rtl"><a href="link">تضيح المقال</a></span>'
end
end

context 'when given a multivalued title' do
let(:label) { ['تضيح المقال', 'title'] }

it 'returns multiple span tags' do
tag = helper.render_index_document(document)
expect(tag).to eq '<span style="display: block;" dir="rtl"><a href="link">تضيح المقال</a></span>'\
'<span style="display: block;" dir="ltr"><a href="link">title</a></span>'
end
end
end
end
64 changes: 64 additions & 0 deletions spec/presenters/rtl_index_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'rails_helper'

RSpec.describe RTLIndexPresenter do
subject(:presenter) { described_class.new(document, double(blacklight_config: blacklight_config)) }

let(:document) do
{
title: title,
alternate_title: alternate_title
}
end
let(:title) { ['تضيح المقال'] }
let(:alternate_title) { ['a different title'] }
let(:index_config) { double(title_field: 'title', display_title_field: '') }
let(:field_config) { double }
let(:blacklight_config) do
double(
index: index_config,
index_fields: { field: field_config }
)
end

before do
allow(field_config).to receive(:to_h).and_return({})
end

describe '#label' do
context 'when given a single-valued title' do
it 'renders as a String' do
expect(presenter.label(:title)).to eq 'تضيح المقال'
end
end

context 'when given a multivalued title' do
let(:title) { ['تضيح المقال', 'Tawḍīḥ al-maqāl'] }

it 'renders as an Array' do
expect(presenter.label(:title)).to match_array ['تضيح المقال', 'Tawḍīḥ al-maqāl']
end
end

context 'when configured with a display title field' do
let(:index_config) { double(title_field: 'title', display_title_field: 'alternate_title') }

it 'renders the display title field' do
expect(presenter.label(:title)).to eq 'a different title'
end
end

context 'when passed a string' do
it 'renders the string as the label' do
label_value = 'a string'
expect(presenter.label(label_value)).to eq 'a string'
end
end

context 'when passed a proc' do
it 'calls the proc' do
label_value = proc { 'a string' }
expect(presenter.label(label_value)).to eq 'a string'
end
end
end
end