Skip to content

Commit

Permalink
Merge 1abe669 into fa96057
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 10, 2018
2 parents fa96057 + 1abe669 commit b7fc583
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 150 deletions.
16 changes: 12 additions & 4 deletions app/presenters/blacklight/index_presenter.rb
Expand Up @@ -48,12 +48,20 @@ def render_document_index_label(*args)
#
# Allow an extention point where information in the document
# may drive the value of the field
# @param [String] field
# @param [Configuration::Field, String] field_or_name
# @param [Hash] options
# @option options [String] :value
def field_value field, options = {}
field_config = field_config(field)
field_values(field_config, options)
def field_value field_or_name, options = {}
field = case field_or_name
when String
Deprecation.warn(self, "You provided a String value to IndexPresenter#field_value " \
"Provide a Blacklight::Configuration::Field instead. field_value() will not accept " \
"strings in Blacklight 7")
field_config(field_or_name)
else
field_or_name
end
field_values(field, options)
end

# @deprecated
Expand Down
13 changes: 11 additions & 2 deletions app/presenters/blacklight/show_presenter.rb
Expand Up @@ -74,10 +74,19 @@ def document_heading
#
# Allow an extention point where information in the document
# may drive the value of the field
# @param [String] field
# @param [Configuration::Field, String] field_or_name
# @param [Hash] options
# @option options [String] :value
def field_value field, options={}
def field_value field_or_name, options={}
field = case field_or_name
when String
Deprecation.warn(self, "You provided a String value to ShowPresenter#field_value " \
"Provide a Blacklight::Configuration::Field instead. field_value() will not accept " \
"strings in Blacklight 7")
field_config(field_or_name)
else
field_or_name
end
field_values(field_config(field), options)
end

Expand Down
6 changes: 3 additions & 3 deletions app/views/catalog/_index_default.html.erb
@@ -1,11 +1,11 @@
<% doc_presenter = index_presenter(document) %>
<% doc_presenter = index_presenter(document) %>
<%# default partial to display solr document fields in catalog index view -%>
<dl class="document-metadata dl-horizontal dl-invert">

<% index_fields(document).each do |field_name, field| -%>
<% if should_render_index_field? document, field %>
<dt class="blacklight-<%= field_name.parameterize %>"><%= render_index_field_label document, field: field_name %></dt>
<dd class="blacklight-<%= field_name.parameterize %>"><%= doc_presenter.field_value field_name %></dd>
<dd class="blacklight-<%= field_name.parameterize %>"><%= doc_presenter.field_value field %></dd>
<% end -%>
<% end -%>

Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_show_default.html.erb
Expand Up @@ -4,7 +4,7 @@
<% document_show_fields(document).each do |field_name, field| -%>
<% if should_render_show_field? document, field %>
<dt class="blacklight-<%= field_name.parameterize %>"><%= render_document_show_field_label document, field: field_name %></dt>
<dd class="blacklight-<%= field_name.parameterize %>"><%= doc_presenter.field_value field_name %></dd>
<dd class="blacklight-<%= field_name.parameterize %>"><%= doc_presenter.field_value field %></dd>
<% end -%>
<% end -%>
</dl>
156 changes: 94 additions & 62 deletions spec/presenters/index_presenter_spec.rb
Expand Up @@ -16,103 +16,136 @@
SolrDocument.new(id: 1,
'link_to_search_true' => 'x',
'link_to_search_named' => 'x',
'qwer' => 'document qwer value',
'mnbv' => 'document mnbv value')
'qwer' => 'document qwer value')
end

before do
allow(request_context).to receive(:search_state).and_return(search_state)
end

describe "field_value" do
describe '#field_value' do
subject { presenter.field_value field }

let(:field) { config.index_fields[field_name] }
let(:field_name) { 'asdf' }
let(:config) do
Blacklight::Configuration.new.configure do |config|
config.add_index_field 'qwer'
config.add_index_field 'asdf', :helper_method => :render_asdf_index_field
config.add_index_field 'link_to_search_true', :link_to_search => true
config.add_index_field 'link_to_search_named', :link_to_search => :some_field
config.add_index_field 'highlight', :highlight => true
config.add_index_field 'solr_doc_accessor', :accessor => true
config.add_index_field 'explicit_accessor', :accessor => :solr_doc_accessor
config.add_index_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg
config.add_index_field 'asdf', helper_method: :render_asdf_index_field
config.add_index_field 'link_to_search_true', link_to_search: true
config.add_index_field 'link_to_search_named', link_to_search: :some_field
config.add_index_field 'highlight', highlight: true
config.add_index_field 'solr_doc_accessor', accessor: true
config.add_index_field 'explicit_accessor', accessor: :solr_doc_accessor
config.add_index_field 'alias', field: 'qwer'
config.add_index_field 'with_default', default: 'value'
end
end
it "checks for an explicit value" do
value = subject.field_value 'asdf', :value => 'asdf'
expect(value).to eq 'asdf'
end

it "checks for a helper method to call" do
allow(request_context).to receive(:render_asdf_index_field).and_return('custom asdf value')
value = subject.field_value 'asdf'
expect(value).to eq 'custom asdf value'
context 'when a name is provided' do
subject { presenter.field_value 'qwer' }

it 'raises a deprecation' do
expect(Deprecation).to receive(:warn)
expect(subject).to eq 'document qwer value'
end
end

it "checks for a link_to_search" do
allow(request_context).to receive(:search_action_path).with('f' => { 'link_to_search_true' => ['x'] }).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
value = subject.field_value 'link_to_search_true'
expect(value).to eq 'bar'
context 'when an explicit value is provided' do
subject { presenter.field_value field, value: 'asdf' }

it { is_expected.to eq 'asdf' }
end

it "checks for a link_to_search with a field name" do
allow(request_context).to receive(:search_action_path).with('f' => { 'some_field' => ['x'] }).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
value = subject.field_value 'link_to_search_named'
expect(value).to eq 'bar'
context 'when field has a helper method' do
before do
allow(request_context).to receive(:render_asdf_index_field).and_return('custom asdf value')
end

it { is_expected.to eq 'custom asdf value' }
end

it "gracefully handles when no highlight field is available" do
allow(document).to receive(:has_highlight_field?).and_return(false)
value = subject.field_value 'highlight'
expect(value).to be_blank
context 'when field has link_to_search with true' do
before do
allow(request_context).to receive(:search_action_path).with('f' => { 'link_to_search_true' => ['x'] }).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
end

let(:field_name) { 'link_to_search_true' }

it { is_expected.to eq 'bar' }
end

it "checks for a highlighted field" do
allow(document).to receive(:has_highlight_field?).and_return(true)
allow(document).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe])
value = subject.field_value 'highlight'
expect(value).to eq '<em>highlight</em>'
context 'when field has link_to_search with a field name' do
before do
allow(request_context).to receive(:search_action_path).with('f' => { 'some_field' => ['x'] }).and_return('/foo')
allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar')
end

let(:field_name) { 'link_to_search_named' }

it { is_expected.to eq 'bar' }
end

it "checks the document field value" do
value = subject.field_value 'qwer'
expect(value).to eq 'document qwer value'
context 'when no highlight field is available' do
before do
allow(document).to receive(:has_highlight_field?).and_return(false)
end

let(:field_name) { 'highlight' }

it { is_expected.to be_blank }
end

it "works with index fields that aren't explicitly defined" do
value = subject.field_value 'mnbv'
expect(value).to eq 'document mnbv value'
context 'when highlight field is available' do
before do
allow(document).to receive(:has_highlight_field?).and_return(true)
allow(document).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe])
end

let(:field_name) { 'highlight' }

it { is_expected.to eq '<em>highlight</em>' }
end

it "calls an accessor on the solr document" do
allow(document).to receive_messages(solr_doc_accessor: "123")
value = subject.field_value 'solr_doc_accessor'
expect(value).to eq "123"
context 'when no options are provided' do
let(:field_name) { 'qwer' }

it "checks the document field value" do
expect(subject).to eq 'document qwer value'
end
end

it "calls an explicit accessor on the solr document" do
allow(document).to receive_messages(solr_doc_accessor: "123")
value = subject.field_value 'explicit_accessor'
expect(value).to eq "123"
context 'when accessor is true' do
before do
allow(document).to receive_messages(solr_doc_accessor: "123")
end

let(:field_name) { 'solr_doc_accessor' }

it { is_expected.to eq '123' }
end

it "calls an accessor on the solr document with the field as an argument" do
allow(document).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123")
value = subject.field_value 'explicit_accessor_with_arg'
expect(value).to eq "123"
context 'when accessor is set to a value' do
let(:field_name) { 'explicit_accessor' }

it 'calls the accessor with the field_name as the argument' do
expect(document).to receive(:solr_doc_accessor).with('explicit_accessor').and_return("123")

expect(subject).to eq '123'
end
end

it "supports solr field configuration" do
value = subject.field_value 'alias'
expect(value).to eq "document qwer value"
context 'when the field is an alias' do
let(:field_name) { 'alias' }

it { is_expected.to eq 'document qwer value' }
end

it "supports default values in the field configuration" do
value = subject.field_value 'with_default'
expect(value).to eq "value"
context 'when the field has a default' do
let(:field_name) { 'with_default' }

it { is_expected.to eq 'value' }
end
end

Expand Down Expand Up @@ -170,4 +203,3 @@
end
end
end

0 comments on commit b7fc583

Please sign in to comment.