-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rtl functionality to search results
- Loading branch information
1 parent
f8c58f3
commit 827586b
Showing
9 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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 be_an Array | ||
expect(presenter.label(:title)).to include '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 |