blacklight 5.11.3 → 5.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/app/helpers/blacklight/blacklight_helper_behavior.rb +2 -2
  4. data/app/helpers/blacklight/catalog_helper_behavior.rb +2 -4
  5. data/app/helpers/blacklight/facets_helper_behavior.rb +10 -6
  6. data/app/helpers/blacklight/url_helper_behavior.rb +1 -1
  7. data/app/views/bookmarks/_clear_bookmarks_widget.html.erb +1 -0
  8. data/app/views/bookmarks/index.html.erb +0 -1
  9. data/blacklight.gemspec +1 -1
  10. data/config/locales/blacklight.en.yml +1 -0
  11. data/lib/blacklight.rb +2 -1
  12. data/lib/blacklight/bookmarks.rb +2 -0
  13. data/lib/blacklight/catalog.rb +1 -1
  14. data/lib/blacklight/configuration.rb +8 -1
  15. data/lib/blacklight/configuration/fields.rb +20 -10
  16. data/lib/blacklight/document.rb +43 -13
  17. data/lib/blacklight/document_presenter.rb +8 -4
  18. data/lib/blacklight/facet.rb +6 -54
  19. data/lib/blacklight/request_builders.rb +2 -2
  20. data/lib/blacklight/search_builder.rb +48 -18
  21. data/lib/blacklight/search_helper.rb +10 -10
  22. data/lib/blacklight/solr.rb +1 -1
  23. data/lib/blacklight/solr/search_builder.rb +2 -265
  24. data/lib/blacklight/solr/search_builder_behavior.rb +274 -0
  25. data/lib/blacklight/solr_repository.rb +1 -1
  26. data/lib/blacklight/solr_response.rb +8 -16
  27. data/lib/blacklight/solr_response/facets.rb +133 -25
  28. data/lib/blacklight/solr_response/group_response.rb +1 -1
  29. data/lib/blacklight/solr_response/pagination_methods.rb +0 -17
  30. data/lib/generators/blacklight/install_generator.rb +6 -1
  31. data/lib/generators/blacklight/search_builder_generator.rb +20 -0
  32. data/lib/generators/blacklight/templates/search_builder.rb +3 -0
  33. data/lib/railties/blacklight.rake +1 -1
  34. data/spec/controllers/catalog_controller_spec.rb +9 -9
  35. data/spec/helpers/blacklight_helper_spec.rb +29 -179
  36. data/spec/helpers/facets_helper_spec.rb +37 -75
  37. data/spec/helpers/url_helper_spec.rb +1 -1
  38. data/spec/lib/blacklight/configuration_spec.rb +18 -1
  39. data/spec/lib/blacklight/document_spec.rb +62 -0
  40. data/spec/lib/blacklight/search_builder_spec.rb +15 -13
  41. data/spec/lib/blacklight/search_helper_spec.rb +15 -16
  42. data/spec/lib/blacklight/solr/document_spec.rb +5 -3
  43. data/spec/lib/blacklight/solr/search_builder_spec.rb +0 -5
  44. data/spec/lib/blacklight/solr_response/facets_spec.rb +144 -10
  45. data/spec/lib/blacklight/solr_response_spec.rb +5 -13
  46. data/spec/lib/document_presenter_spec.rb +23 -27
  47. data/spec/views/catalog/_facets.html.erb_spec.rb +1 -1
  48. data/spec/views/catalog/_index_default.erb_spec.rb +2 -13
  49. data/spec/views/catalog/_show_default.erb_spec.rb +1 -13
  50. metadata +10 -4
@@ -43,7 +43,7 @@ module Blacklight
43
43
  key = blacklight_config.http_method == :post ? :data : :params
44
44
  res = connection.send_and_receive(path, {key=>solr_params.to_hash, method:blacklight_config.http_method})
45
45
 
46
- solr_response = blacklight_config.response_model.new(res, solr_params, document_model: blacklight_config.document_model)
46
+ solr_response = blacklight_config.response_model.new(res, solr_params, document_model: blacklight_config.document_model, blacklight_config: blacklight_config)
47
47
 
48
48
  Blacklight.logger.debug("Solr query: #{solr_params.inspect}")
49
49
  Blacklight.logger.debug("Solr response: #{solr_response.inspect}") if defined?(::BLACKLIGHT_VERBOSE_LOGGING) and ::BLACKLIGHT_VERBOSE_LOGGING
@@ -1,4 +1,5 @@
1
1
  class Blacklight::SolrResponse < HashWithIndifferentAccess
2
+ extend Deprecation
2
3
 
3
4
  require 'blacklight/solr_response/pagination_methods'
4
5
 
@@ -16,44 +17,35 @@ class Blacklight::SolrResponse < HashWithIndifferentAccess
16
17
  include MoreLikeThis
17
18
 
18
19
  attr_reader :request_params
19
- attr_accessor :document_model
20
+ attr_accessor :document_model, :blacklight_config
20
21
 
21
22
  def initialize(data, request_params, options = {})
22
23
  super(force_to_utf8(data))
23
24
  @request_params = request_params
24
25
  self.document_model = options[:solr_document_model] || options[:document_model] || SolrDocument
26
+ self.blacklight_config = options[:blacklight_config]
25
27
  end
26
28
 
27
29
  def header
28
- self['responseHeader']
30
+ self['responseHeader'] || {}
29
31
  end
30
-
31
- def update(other_hash)
32
- other_hash.each_pair { |key, value| self[key] = value }
33
- self
34
- end
35
32
 
36
33
  def params
37
- (header and header['params']) ? header['params'] : request_params
34
+ header['params'] || request_params
38
35
  end
39
36
 
40
37
  def rows
41
- params[:rows].to_i
38
+ params[:rows].to_i
42
39
  end
43
40
 
44
41
  def sort
45
42
  params[:sort]
46
43
  end
47
44
 
48
- def docs
49
- @docs ||= begin
50
- response['docs'] || []
51
- end
52
- end
53
-
54
45
  def documents
55
- docs.collect{|doc| document_model.new(doc, self) }
46
+ @documents ||= (response['docs'] || []).collect{|doc| document_model.new(doc, self) }
56
47
  end
48
+ alias_method :docs, :documents
57
49
 
58
50
  def grouped
59
51
  @groups ||= self["grouped"].map do |field, group|
@@ -1,6 +1,7 @@
1
1
  require 'ostruct'
2
2
 
3
3
  module Blacklight::SolrResponse::Facets
4
+ extend Deprecation
4
5
 
5
6
  # represents a facet value; which is a field value and its hit count
6
7
  class FacetItem < OpenStruct
@@ -67,40 +68,29 @@ module Blacklight::SolrResponse::Facets
67
68
  end
68
69
 
69
70
  end
70
-
71
+
72
+ ##
73
+ # Get all the Solr facet data (fields, queries, pivots) as a hash keyed by
74
+ # both the Solr field name and/or by the blacklight field name
75
+ def aggregations
76
+ @aggregations ||= {}.merge(facet_field_aggregations).merge(facet_query_aggregations).merge(facet_pivot_aggregations)
77
+ end
78
+
71
79
  # @response.facets.each do |facet|
72
80
  # facet.name
73
81
  # facet.items
74
82
  # end
75
83
  # "caches" the result in the @facets instance var
76
84
  def facets
77
- @facets ||= begin
78
- facet_fields.map do |(facet_field_name,values_and_hits)|
79
- items = []
80
- options = {}
81
- values_and_hits.each_slice(2) do |k,v|
82
- items << FacetItem.new(:value => k, :hits => v)
83
- end
84
- options[:sort] = (params[:"f.#{facet_field_name}.facet.sort"] || params[:'facet.sort'])
85
- if params[:"f.#{facet_field_name}.facet.limit"] || params[:"facet.limit"]
86
- options[:limit] = (params[:"f.#{facet_field_name}.facet.limit"] || params[:"facet.limit"]).to_i
87
- end
88
-
89
- if params[:"f.#{facet_field_name}.facet.offset"] || params[:'facet.offset']
90
- options[:offset] = (params[:"f.#{facet_field_name}.facet.offset"] || params[:'facet.offset']).to_i
91
- end
92
- FacetField.new(facet_field_name, items, options)
93
- end
94
- end
85
+ aggregations.values
95
86
  end
96
-
87
+ deprecation_deprecate facets: :aggregations
88
+
97
89
  # pass in a facet field name and get back a Facet instance
98
90
  def facet_by_field_name(name)
99
- @facets_by_field_name ||= {}
100
- @facets_by_field_name[name] ||= (
101
- facets.detect{|facet|facet.name.to_s == name.to_s}
102
- )
91
+ aggregations[name]
103
92
  end
93
+ deprecation_deprecate facet_by_field_name: :aggregations
104
94
 
105
95
  def facet_counts
106
96
  @facet_counts ||= self['facet_counts'] || {}
@@ -108,7 +98,16 @@ module Blacklight::SolrResponse::Facets
108
98
 
109
99
  # Returns the hash of all the facet_fields (ie: {'instock_b' => ['true', 123, 'false', 20]}
110
100
  def facet_fields
111
- @facet_fields ||= facet_counts['facet_fields'] || {}
101
+ @facet_fields ||= begin
102
+ val = facet_counts['facet_fields'] || {}
103
+
104
+ # this is some old solr (1.4? earlier?) serialization of facet fields
105
+ if val.is_a? Array
106
+ Hash[val]
107
+ else
108
+ val
109
+ end
110
+ end
112
111
  end
113
112
 
114
113
  # Returns all of the facet queries
@@ -120,5 +119,114 @@ module Blacklight::SolrResponse::Facets
120
119
  def facet_pivot
121
120
  @facet_pivot ||= facet_counts['facet_pivot'] || {}
122
121
  end
122
+
123
+ private
124
+ ##
125
+ # Convert Solr responses of various json.nl flavors to
126
+ def list_as_hash solr_list
127
+ # map
128
+ if solr_list.values.first.is_a? Hash
129
+ solr_list
130
+ else
131
+ solr_list.each_with_object({}) do |(key, values), hash|
132
+ hash[key] = if values.first.is_a? Array
133
+ # arrarr
134
+ Hash[values]
135
+ else
136
+ # flat
137
+ Hash[values.each_slice(2).to_a]
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ ##
144
+ # Convert Solr's facet_field response into
145
+ # a hash of Blacklight::SolrResponse::Facet::FacetField objects
146
+ def facet_field_aggregations
147
+ list_as_hash(facet_fields).each_with_object({}) do |(facet_field_name, values), hash|
148
+ items = []
149
+ options = {}
150
+ values.each do |value, hits|
151
+ i = FacetItem.new(value: value, hits: hits)
152
+
153
+ # solr facet.missing serialization
154
+ if value.nil?
155
+ i.label = I18n.t(:"blacklight.search.fields.facet.missing.#{facet_field_name}", default: [:"blacklight.search.facets.missing"])
156
+ i.fq = "-#{facet_field_name}:[* TO *]"
157
+ end
158
+
159
+ items << i
160
+ end
161
+ options[:sort] = (params[:"f.#{facet_field_name}.facet.sort"] || params[:'facet.sort'])
162
+ if params[:"f.#{facet_field_name}.facet.limit"] || params[:"facet.limit"]
163
+ options[:limit] = (params[:"f.#{facet_field_name}.facet.limit"] || params[:"facet.limit"]).to_i
164
+ end
165
+
166
+ if params[:"f.#{facet_field_name}.facet.offset"] || params[:'facet.offset']
167
+ options[:offset] = (params[:"f.#{facet_field_name}.facet.offset"] || params[:'facet.offset']).to_i
168
+ end
169
+
170
+ hash[facet_field_name] = FacetField.new(facet_field_name, items, options)
171
+
172
+ if blacklight_config and !blacklight_config.facet_fields[facet_field_name]
173
+ # alias all the possible blacklight config names..
174
+ blacklight_config.facet_fields.select { |k,v| v.field == facet_field_name }.each do |key,_|
175
+ hash[key] = hash[facet_field_name]
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ ##
182
+ # Aggregate Solr's facet_query response into the virtual facet fields defined
183
+ # in the blacklight configuration
184
+ def facet_query_aggregations
185
+ return {} unless blacklight_config
186
+
187
+ blacklight_config.facet_fields.select { |k,v| v.query }.each_with_object({}) do |(field_name, facet_field), hash|
188
+ salient_facet_queries = facet_field.query.map { |k, x| x[:fq] }
189
+ items = []
190
+ facet_queries.select { |k,v| salient_facet_queries.include?(k) }.reject { |value, hits| hits == 0 }.map do |value,hits|
191
+ salient_fields = facet_field.query.select { |key, val| val[:fq] == value }
192
+ key = ((salient_fields.keys if salient_fields.respond_to? :keys) || salient_fields.first).first
193
+ items << Blacklight::SolrResponse::Facets::FacetItem.new(value: key, hits: hits, label: facet_field.query[key][:label])
194
+ end
195
+
196
+ hash[field_name] = Blacklight::SolrResponse::Facets::FacetField.new field_name, items
197
+ end
198
+ end
199
+
200
+ ##
201
+ # Convert Solr's facet_pivot response into
202
+ # a hash of Blacklight::SolrResponse::Facet::FacetField objects
203
+ def facet_pivot_aggregations
204
+ facet_pivot.each_with_object({}) do |(field_name, values), hash|
205
+ items = []
206
+ values.map do |lst|
207
+ items << construct_pivot_field(lst)
208
+ end
209
+
210
+ if blacklight_config and !blacklight_config.facet_fields[field_name]
211
+ # alias all the possible blacklight config names..
212
+ blacklight_config.facet_fields.select { |k,v| v.pivot and v.pivot.join(",") == field_name }.each do |key, _|
213
+ hash[key] = Blacklight::SolrResponse::Facets::FacetField.new key, items
214
+ end
215
+ end
216
+ end
217
+ end
218
+
219
+ ##
220
+ # Recursively parse the pivot facet response to build up the full pivot tree
221
+ def construct_pivot_field lst, parent_fq = {}
222
+ items = []
223
+
224
+ lst[:pivot].each do |i|
225
+ items << construct_pivot_field(i, parent_fq.merge({ lst[:field] => lst[:value] }))
226
+ end if lst[:pivot]
227
+
228
+ Blacklight::SolrResponse::Facets::FacetItem.new(value: lst[:value], hits: lst[:count], field: lst[:field], items: items, fq: parent_fq)
229
+ end
230
+
123
231
 
124
232
  end # end Facets
@@ -25,7 +25,7 @@ class Blacklight::SolrResponse::GroupResponse
25
25
  def total
26
26
  # ngroups is only available in Solr 4.1+
27
27
  # fall back on the number of facet items for that field?
28
- (group["ngroups"] || (response.facet_by_field_name(key) || []).length).to_s.to_i
28
+ (group["ngroups"] || (response.aggregations[key] || []).length).to_s.to_i
29
29
  end
30
30
 
31
31
  def start
@@ -14,21 +14,4 @@ module Blacklight::SolrResponse::PaginationMethods
14
14
  def total_count #:nodoc:
15
15
  total
16
16
  end
17
-
18
- def model_name
19
- if !docs.empty? and docs.first.respond_to? :model_name
20
- docs.first.model_name
21
- end
22
- end
23
-
24
- ## Methods in kaminari master that we'd like to use today.
25
- # Next page number in the collection
26
- def next_page
27
- current_page + 1 unless last_page?
28
- end
29
-
30
- # Previous page number in the collection
31
- def prev_page
32
- current_page - 1 unless first_page?
33
- end
34
17
  end
@@ -6,6 +6,7 @@ module Blacklight
6
6
  argument :model_name , type: :string , default: "user"
7
7
  argument :controller_name, type: :string , default: "catalog"
8
8
  argument :document_name, type: :string , default: "solr_document"
9
+ argument :search_builder_name, type: :string , default: "search_builder"
9
10
 
10
11
  class_option :devise , type: :boolean, default: false, aliases: "-d", desc: "Use Devise as authentication logic."
11
12
  class_option :jettywrapper, type: :boolean, default: false, desc: "Use jettywrapper to download and control Jetty"
@@ -55,6 +56,10 @@ module Blacklight
55
56
  generate 'blacklight:document', document_name
56
57
  end
57
58
 
59
+ def generate_search_builder
60
+ generate 'blacklight:search_builder', search_builder_name
61
+ end
62
+
58
63
  def generate_blacklight_models
59
64
  generate 'blacklight:models'
60
65
  end
@@ -103,7 +108,7 @@ EOF
103
108
  run "bundle install"
104
109
  end
105
110
 
106
- generate 'blacklight_marc:marc'
111
+ generate 'blacklight:marc:install'
107
112
  end
108
113
  end
109
114
  end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+
3
+ module Blacklight
4
+ class SearchBuilderGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ argument :model_name, :type => :string , :default => "search_builder"
10
+
11
+ desc """
12
+ This generator makes the following changes to your application:
13
+ 1. Creates a blacklight search builder in your /app/models directory
14
+ """
15
+ def create_search_builder
16
+ template "search_builder.rb", "app/models/#{model_name}.rb"
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ class SearchBuilder < Blacklight::SearchBuilder
2
+ include Blacklight::Solr::SearchBuilderBehavior
3
+ end
@@ -103,7 +103,7 @@ namespace :blacklight do
103
103
  print " - fetch: "
104
104
 
105
105
  begin
106
- doc_id = response.docs.first[SolrDocument.unique_key]
106
+ doc_id = response.documents.first.id
107
107
  response, doc = controller.fetch doc_id
108
108
 
109
109
  if response.header['status'] == 0 and doc
@@ -39,18 +39,18 @@ describe CatalogController do
39
39
  it "should have docs and facets for query with results", :integration => true do
40
40
  get :index, q: user_query
41
41
  expect(assigns_response.docs).to_not be_empty
42
- assert_facets_have_values(assigns_response.facets)
42
+ assert_facets_have_values(assigns_response.aggregations)
43
43
  end
44
44
  it "should have docs and facets for existing facet value", :integration => true do
45
45
  get :index, f: {"format" => 'Book'}
46
46
  expect(assigns_response.docs).to_not be_empty
47
- assert_facets_have_values(assigns_response.facets)
47
+ assert_facets_have_values(assigns_response.aggregations)
48
48
  end
49
49
  it "should have docs and facets for non-default results per page", :integration => true do
50
50
  num_per_page = 7
51
51
  get :index, :per_page => num_per_page
52
52
  expect(assigns_response.docs).to have(num_per_page).items
53
- assert_facets_have_values(assigns_response.facets)
53
+ assert_facets_have_values(assigns_response.aggregations)
54
54
  end
55
55
 
56
56
  it "should have docs and facets for second page", :integration => true do
@@ -58,14 +58,14 @@ describe CatalogController do
58
58
  get :index, :page => page
59
59
  expect(assigns_response.docs).to_not be_empty
60
60
  expect(assigns_response.params[:start].to_i).to eq (page-1) * @controller.blacklight_config[:default_solr_params][:rows]
61
- assert_facets_have_values(assigns_response.facets)
61
+ assert_facets_have_values(assigns_response.aggregations)
62
62
  end
63
63
 
64
64
  it "should have no docs or facet values for query without results", :integration => true do
65
65
  get :index, q: 'sadfdsafasdfsadfsadfsadf' # query for no results
66
66
 
67
67
  expect(assigns_response.docs).to be_empty
68
- assigns_response.facets.each do |facet|
68
+ assigns_response.aggregations.each do |key, facet|
69
69
  expect(facet.items).to be_empty
70
70
  end
71
71
  end
@@ -97,7 +97,7 @@ describe CatalogController do
97
97
  end
98
98
  it "should get facets when no query", :integration => true do
99
99
  get :index
100
- assert_facets_have_values(assigns_response.facets)
100
+ assert_facets_have_values(assigns_response.aggregations)
101
101
  end
102
102
  end
103
103
 
@@ -737,10 +737,10 @@ end
737
737
 
738
738
 
739
739
  # there must be at least one facet, and each facet must have at least one value
740
- def assert_facets_have_values(facets)
741
- expect(facets).to_not be_empty
740
+ def assert_facets_have_values(aggregations)
741
+ expect(aggregations).to_not be_empty
742
742
  # should have at least one value for each facet
743
- facets.each do |facet|
743
+ aggregations.each do |key, facet|
744
744
  expect(facet.items).to have_at_least(1).item
745
745
  end
746
746
  end
@@ -207,203 +207,53 @@ describe BlacklightHelper do
207
207
  end
208
208
  end
209
209
 
210
- describe "render_index_field_value" do
210
+ describe "#render_index_field_value" do
211
+ let(:presenter) { double }
211
212
  before do
212
- @config = Blacklight::Configuration.new.configure do |config|
213
- config.add_index_field 'qwer'
214
- config.add_index_field 'asdf', :helper_method => :render_asdf_index_field
215
- config.add_index_field 'link_to_search_true', :link_to_search => true
216
- config.add_index_field 'link_to_search_named', :link_to_search => :some_field
217
- config.add_index_field 'highlight', :highlight => true
218
- config.add_index_field 'solr_doc_accessor', :accessor => true
219
- config.add_index_field 'explicit_accessor', :accessor => :solr_doc_accessor
220
- config.add_index_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg
221
- end
222
- allow(helper).to receive(:blacklight_config).and_return(@config)
223
- end
224
-
225
- it "should check for an explicit value" do
226
- doc = double()
227
- expect(doc).to_not receive(:get).with('asdf', :sep => nil)
228
- value = helper.render_index_field_value :value => 'asdf', :document => doc, :field => 'asdf'
229
- expect(value).to eq 'asdf'
213
+ allow(helper).to receive(:presenter).with(doc).and_return(presenter)
230
214
  end
231
215
 
232
- it "should check for a helper method to call" do
233
- doc = double()
234
- allow(doc).to receive(:get).with('asdf', :sep => nil)
235
- allow(helper).to receive(:render_asdf_index_field).and_return('custom asdf value')
236
- value = helper.render_index_field_value :document => doc, :field => 'asdf'
237
- expect(value).to eq 'custom asdf value'
238
- end
239
-
240
- it "should check for a link_to_search" do
241
- doc = double()
242
- allow(doc).to receive(:get).with('link_to_search_true', :sep => nil).and_return('x')
243
- value = helper.render_index_field_value :document => doc, :field => 'link_to_search_true'
244
- expect(value).to eq helper.link_to("x", helper.search_action_path(:f => { :link_to_search_true => ['x'] }))
245
- end
246
-
247
- it "should check for a link_to_search with a field name" do
248
- doc = double()
249
- allow(doc).to receive(:get).with('link_to_search_named', :sep => nil).and_return('x')
250
- value = helper.render_index_field_value :document => doc, :field => 'link_to_search_named'
251
- expect(value).to eq helper.link_to("x", helper.search_action_path(:f => { :some_field => ['x'] }))
252
- end
253
-
254
- it "should gracefully handle when no highlight field is available" do
255
- doc = double()
256
- expect(doc).to_not receive(:get)
257
- allow(doc).to receive(:has_highlight_field?).and_return(false)
258
- value = helper.render_index_field_value :document => doc, :field => 'highlight'
259
- expect(value).to be_blank
260
- end
261
-
262
- it "should check for a highlighted field" do
263
- doc = double()
264
- expect(doc).to_not receive(:get)
265
- allow(doc).to receive(:has_highlight_field?).and_return(true)
266
- allow(doc).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe])
267
- value = helper.render_index_field_value :document => doc, :field => 'highlight'
268
- expect(value).to eq '<em>highlight</em>'
269
- end
270
-
271
- it "should check the document field value" do
272
- doc = double()
273
- allow(doc).to receive(:get).with('qwer', :sep => nil).and_return('document qwer value')
274
- value = helper.render_index_field_value :document => doc, :field => 'qwer'
275
- expect(value).to eq 'document qwer value'
276
- end
277
-
278
- it "should work with index fields that aren't explicitly defined" do
279
- doc = double()
280
- allow(doc).to receive(:get).with('mnbv', :sep => nil).and_return('document mnbv value')
281
- value = helper.render_index_field_value :document => doc, :field => 'mnbv'
282
- expect(value).to eq 'document mnbv value'
283
- end
216
+ let(:doc) { double }
217
+ let(:field) { "some_field" }
284
218
 
285
- it "should call an accessor on the solr document" do
286
- doc = double(:solr_doc_accessor => "123")
287
- value = helper.render_index_field_value :document => doc, :field => 'solr_doc_accessor'
288
- expect(value).to eq "123"
219
+ it "should pass the document and field through to the presenter" do
220
+ expect(presenter).to receive(:render_index_field_value).with(field, {})
221
+ helper.render_index_field_value(doc, field)
289
222
  end
290
223
 
291
- it "should call an explicit accessor on the solr document" do
292
- doc = double(:solr_doc_accessor => "123")
293
- value = helper.render_index_field_value :document => doc, :field => 'explicit_accessor'
294
- expect(value).to eq "123"
224
+ it "should allow the document and field to be passed as hash arguments" do
225
+ expect(presenter).to receive(:render_index_field_value).with(field, {})
226
+ helper.render_index_field_value(document: doc, field: field)
295
227
  end
296
228
 
297
- it "should call an implicit accessor on the solr document" do
298
- doc = double()
299
- expect(doc).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123")
300
- value = helper.render_index_field_value :document => doc, :field => 'explicit_accessor_with_arg'
301
- expect(value).to eq "123"
229
+ it "should allow additional options to be passed to the presenter" do
230
+ expect(presenter).to receive(:render_index_field_value).with(field, x: 1)
231
+ helper.render_index_field_value(document: doc, field: field, x: 1)
302
232
  end
303
233
  end
304
-
305
- describe "render_document_show_field_value" do
234
+
235
+ describe "#render_document_show_field_value" do
236
+ let(:presenter) { double }
306
237
  before do
307
- @config = Blacklight::Configuration.new.configure do |config|
308
- config.add_show_field 'qwer'
309
- config.add_show_field 'asdf', :helper_method => :render_asdf_document_show_field
310
- config.add_show_field 'link_to_search_true', :link_to_search => true
311
- config.add_show_field 'link_to_search_named', :link_to_search => :some_field
312
- config.add_show_field 'highlight', :highlight => true
313
- config.add_show_field 'solr_doc_accessor', :accessor => true
314
- config.add_show_field 'explicit_accessor', :accessor => :solr_doc_accessor
315
- config.add_show_field 'explicit_array_accessor', :accessor => [:solr_doc_accessor, :some_method]
316
- config.add_show_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg
317
- end
318
-
319
- allow(helper).to receive(:blacklight_config).and_return(@config)
320
- end
321
-
322
- it "should check for an explicit value" do
323
- doc = double()
324
- expect(doc).to_not receive(:get).with('asdf', :sep => nil)
325
- expect(helper).to_not receive(:render_asdf_document_show_field)
326
- value = helper.render_document_show_field_value :value => 'asdf', :document => doc, :field => 'asdf'
327
- expect(value).to eq 'asdf'
238
+ allow(helper).to receive(:presenter).with(doc).and_return(presenter)
328
239
  end
329
240
 
330
- it "should check for a helper method to call" do
331
- doc = double()
332
- allow(doc).to receive(:get).with('asdf', :sep => nil)
333
- allow(helper).to receive(:render_asdf_document_show_field).and_return('custom asdf value')
334
- value = helper.render_document_show_field_value :document => doc, :field => 'asdf'
335
- expect(value).to eq 'custom asdf value'
336
- end
337
-
338
- it "should check for a link_to_search" do
339
- doc = double()
340
- allow(doc).to receive(:get).with('link_to_search_true', :sep => nil).and_return('x')
341
- value = helper.render_document_show_field_value :document => doc, :field => 'link_to_search_true'
342
- expect(value).to eq helper.link_to("x", helper.search_action_path(:f => { :link_to_search_true => ['x'] }))
343
- end
344
-
345
- it "should check for a link_to_search with a field name" do
346
- doc = double()
347
- allow(doc).to receive(:get).with('link_to_search_named', :sep => nil).and_return('x')
348
- value = helper.render_document_show_field_value :document => doc, :field => 'link_to_search_named'
349
- expect(value).to eq helper.link_to("x", helper.search_action_path(:f => { :some_field => ['x'] }))
350
- end
351
-
352
- it "should gracefully handle when no highlight field is available" do
353
- doc = double()
354
- expect(doc).to_not receive(:get)
355
- allow(doc).to receive(:has_highlight_field?).and_return(false)
356
- value = helper.render_document_show_field_value :document => doc, :field => 'highlight'
357
- expect(value).to be_blank
358
- end
359
-
360
- it "should check for a highlighted field" do
361
- doc = double()
362
- expect(doc).to_not receive(:get)
363
- allow(doc).to receive(:has_highlight_field?).and_return(true)
364
- allow(doc).to receive(:highlight_field).with('highlight').and_return(['<em>highlight</em>'.html_safe])
365
- value = helper.render_document_show_field_value :document => doc, :field => 'highlight'
366
- expect(value).to eq '<em>highlight</em>'
367
- end
368
-
369
-
370
- it "should check the document field value" do
371
- doc = double()
372
- allow(doc).to receive(:get).with('qwer', :sep => nil).and_return('document qwer value')
373
- value = helper.render_document_show_field_value :document => doc, :field => 'qwer'
374
- expect(value).to eq 'document qwer value'
375
- end
376
-
377
- it "should work with show fields that aren't explicitly defined" do
378
- doc = double()
379
- allow(doc).to receive(:get).with('mnbv', :sep => nil).and_return('document mnbv value')
380
- value = helper.render_document_show_field_value :document => doc, :field => 'mnbv'
381
- expect(value).to eq 'document mnbv value'
382
- end
383
-
384
- it "should call an accessor on the solr document" do
385
- doc = double(:solr_doc_accessor => "123")
386
- value = helper.render_document_show_field_value :document => doc, :field => 'solr_doc_accessor'
387
- expect(value).to eq "123"
388
- end
241
+ let(:doc) { double }
242
+ let(:field) { "some_field" }
389
243
 
390
- it "should call an explicit accessor on the solr document" do
391
- doc = double(:solr_doc_accessor => "123")
392
- value = helper.render_document_show_field_value :document => doc, :field => 'explicit_accessor'
393
- expect(value).to eq "123"
244
+ it "should pass the document and field through to the presenter" do
245
+ expect(presenter).to receive(:render_document_show_field_value).with(field, {})
246
+ helper.render_document_show_field_value(doc, field)
394
247
  end
395
248
 
396
- it "should call an explicit array-style accessor on the solr document" do
397
- doc = double(:solr_doc_accessor => double(:some_method => "123"))
398
- value = helper.render_document_show_field_value :document => doc, :field => 'explicit_array_accessor'
399
- expect(value).to eq "123"
249
+ it "should allow the document and field to be passed as hash arguments" do
250
+ expect(presenter).to receive(:render_document_show_field_value).with(field, {})
251
+ helper.render_document_show_field_value(document: doc, field: field)
400
252
  end
401
253
 
402
- it "should call an accessor on the solr document with the field as an argument" do
403
- doc = double()
404
- expect(doc).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123")
405
- value = helper.render_document_show_field_value :document => doc, :field => 'explicit_accessor_with_arg'
406
- expect(value).to eq "123"
254
+ it "should allow additional options to be passed to the presenter" do
255
+ expect(presenter).to receive(:render_document_show_field_value).with(field, x: 1)
256
+ helper.render_document_show_field_value(document: doc, field: field, x: 1)
407
257
  end
408
258
  end
409
259