blacklight_browse_nearby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/LICENSE +14 -0
  2. data/README.rdoc +118 -0
  3. data/Rakefile +23 -0
  4. data/SOLR_README.rdoc +144 -0
  5. data/app/assets/javascripts/blacklight_browse_nearby/blacklight_browse_nearby.js +12 -0
  6. data/app/assets/stylesheets/blacklight_browse_nearby/blacklight_browse_nearby.css.scss +11 -0
  7. data/app/controllers/blacklight_browse_nearby_controller.rb +25 -0
  8. data/app/helpers/blacklight_browse_nearby_helper.rb +7 -0
  9. data/app/views/blacklight_browse_nearby/_browse_view_pagination.html.erb +6 -0
  10. data/app/views/blacklight_browse_nearby/_nearby_controls.html.erb +11 -0
  11. data/app/views/blacklight_browse_nearby/_nearby_item.html.erb +8 -0
  12. data/app/views/blacklight_browse_nearby/_nearby_items.html.erb +22 -0
  13. data/app/views/blacklight_browse_nearby/index.html.erb +3 -0
  14. data/app/views/blacklight_browse_nearby/index.js.erb +2 -0
  15. data/app/views/catalog/_document_header.html.erb +15 -0
  16. data/config/locales/blacklight_browse_nearby.en.yml +11 -0
  17. data/config/routes.rb +3 -0
  18. data/lib/blacklight_browse_nearby.rb +156 -0
  19. data/lib/blacklight_browse_nearby/controller.rb +16 -0
  20. data/lib/blacklight_browse_nearby/engine.rb +4 -0
  21. data/lib/blacklight_browse_nearby/version.rb +3 -0
  22. data/lib/blacklight_browse_nearby_config.rb +11 -0
  23. data/lib/generators/blacklight_browse_nearby_generator.rb +42 -0
  24. data/lib/tasks/blacklight_browse_nearby_tasks.rake +4 -0
  25. data/spec/acceptance/blacklight_browse_nearby_spec.rb +283 -0
  26. data/spec/internal/app/controllers/application_controller.rb +3 -0
  27. data/spec/internal/app/models/solr_document.rb +3 -0
  28. data/spec/internal/config/database.yml +3 -0
  29. data/spec/internal/config/routes.rb +6 -0
  30. data/spec/internal/config/solr.yml +6 -0
  31. data/spec/internal/db/combustion_test.sqlite +0 -0
  32. data/spec/internal/db/schema.rb +3 -0
  33. data/spec/internal/log/test.log +289 -0
  34. data/spec/internal/public/favicon.ico +0 -0
  35. data/spec/spec_helper.rb +19 -0
  36. metadata +209 -0
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resources :nearby, :controller => "blacklight_browse_nearby"
3
+ end
@@ -0,0 +1,156 @@
1
+ require "blacklight"
2
+ require "blacklight_browse_nearby/engine"
3
+ require "blacklight_browse_nearby_config"
4
+ class BlacklightBrowseNearby
5
+
6
+ autoload :Controller, "blacklight_browse_nearby/controller"
7
+
8
+ include Blacklight::SolrHelper
9
+ include Blacklight::Configurable
10
+
11
+ attr_reader :documents, :original_document
12
+ def initialize(object_id, options={})
13
+ @opts = options
14
+ @documents = get_nearby_documents(object_id)
15
+ end
16
+
17
+ # Returns an array of documents "nearby" the given object ID based on page number.
18
+ # Negative page numbers will return documents "behind" the given object ID.
19
+ # No page number (or 0) will return documents "behind" the given object ID, the given object, and documents "in front of" the given object ID.
20
+ # Positive page numbers will return documents "in front of" the given object ID.
21
+ def get_nearby_documents(id)
22
+ @original_document = get_solr_response_for_doc_id(id).last # returns an array with a response object and the document.
23
+ return [] unless document_has_required_fields?
24
+ shelfkey = get_value_from_combined_key(combined_key, shelfkey_field)
25
+ reverse_shelfkey = get_value_from_combined_key(combined_key, reverse_shelfkey_field)
26
+ if normalized_page == 0
27
+ previous_documents = get_next_documents_from_field_value(reverse_shelfkey, reverse_shelfkey_field)
28
+ next_documents = get_next_documents_from_field_value(shelfkey, shelfkey_field)
29
+ documents = [previous_documents, @original_document, next_documents].flatten
30
+ elsif @opts[:page].to_i < 0
31
+ documents = get_next_documents_from_field_value(reverse_shelfkey, reverse_shelfkey_field)
32
+ elsif @opts[:page].to_i > 0
33
+ documents = get_next_documents_from_field_value(shelfkey, shelfkey_field)
34
+ end
35
+ documents
36
+ end
37
+
38
+
39
+ # Returns an array of documents forward of the given term in the given field. These documents are sorted by the configured solr field.
40
+ # Using solr's termsComponent we request the next terms from the given field and value. This works for backward sorting by using a reverse-sort keys.
41
+ # We then request the documents for those terms from solr via Blacklight's get_solr_response_for_field_values
42
+ def get_next_documents_from_field_value(value, field)
43
+ terms = get_ordered_terms(value, field)
44
+ get_solr_response_for_field_values(field, terms, :per_page=>terms.length).last.sort{|a,b| a[shelfkey_field] <=> b[shelfkey_field] }
45
+ end
46
+
47
+ # Returns an array of the next terms using solr's termsComponent.
48
+ # The number of terms requested/returned from solr may be much larger than what is returned by this method when paging.
49
+ # The pagination happens here and we paginate the returned terms before we request the related documents. This keeps the URLs free of sortkey values.
50
+ def get_ordered_terms(value, field)
51
+ solr_options = {
52
+ :"terms.fl" => field,
53
+ :"terms.lower" => value,
54
+ :"terms.limit" => total_terms
55
+ }
56
+ response = Blacklight.solr.send_and_receive("#{BlacklightBrowseNearby::Engine.config.request_handler}", {:params=>solr_options})
57
+ response["terms"][field].select{|term| term.is_a?(String) }[start_of_terms..(total_terms-1)]
58
+ end
59
+
60
+ # Returns an integer representing the beginning of the range of terms we'll request documents for.
61
+ def start_of_terms
62
+ return 0 if normalized_page == 0
63
+ total_terms - hits_requested
64
+ end
65
+
66
+ # Returns an integer representing the total number of terms to request from solr.
67
+ def total_terms
68
+ return original_query_offset if normalized_page == 0
69
+ (hits_requested * normalized_page) + original_query_offset
70
+ end
71
+
72
+ # Returns an integer representing the number of terms that were requested. Falls back to the provided configuration option in BlacklightBrowseNearby::Engine.config.default_hits
73
+ def hits_requested
74
+ return BlacklightBrowseNearby::Engine.config.default_hits.to_i if @opts[:number].blank?
75
+ @opts[:number].to_i
76
+ end
77
+
78
+ # Returns an integer representing the number of items that would have been intitally requested from page 0. This is necessary to get the appropriate total_terms integer.
79
+ def original_query_offset
80
+ (hits_requested - 1) / 2
81
+ end
82
+
83
+ # Returns an integer representing a normalized page number.
84
+ # This method will return a 0 in the absense of a page option and will turn any negative integer into a positive integer.
85
+ def normalized_page
86
+ return 0 if @opts[:page].blank?
87
+ @opts[:page].to_s.gsub("-","").to_i
88
+ end
89
+
90
+ # Convenience method to return the value field from the original document. This is necessary for supportin the multi-valued field UI.
91
+ def potential_values
92
+ @original_document[value_field]
93
+ end
94
+
95
+ # Convenience method to return the value from the original document that is preferred_value aware. This is necessary for supporting the multi-valued field UI.
96
+ def current_value
97
+ get_value_from_combined_key(combined_key, value_field)
98
+ end
99
+
100
+ # Convenience method to return the combined key from the original document that is preferred_value aware. This is necessary for supporting multi-valued fields.
101
+ def combined_key
102
+ return @original_document[combined_key_field].first if (@original_document[combined_key_field].length == 1 or @opts[:preferred_value].blank?)
103
+ @original_document[combined_key_field].each do |key|
104
+ return key if get_value_from_combined_key(key, value_field) == @opts[:preferred_value]
105
+ end
106
+ end
107
+
108
+ # Returns a hash that will be used by BlacklightSolrHelper.
109
+ # The params hash can be passed in as an option on initialization (although the code isn't currently doing that).
110
+ def params
111
+ @opts[:params] || {}
112
+ end
113
+
114
+ protected
115
+
116
+ # Returns a boolean validating that the given document has all the required fields for the browsing.
117
+ def document_has_required_fields?
118
+ [value_field, reverse_shelfkey_field, shelfkey_field, combined_key_field].each do |field|
119
+ return false if @original_document[field].blank?
120
+ end
121
+ true
122
+ end
123
+
124
+ # Returns a string value from a given combined key and field name.
125
+ # This takes advantage of the combined_key_pattern field to determine field position in the combined_key value.
126
+ def get_value_from_combined_key(key, field)
127
+ index = BlacklightBrowseNearby::Engine.config.combined_key_pattern.split(delimiter).map{|p|p.strip}.index(field)
128
+ key.split(delimiter)[index].strip
129
+ end
130
+
131
+ # Convenience method to return a configuration option.
132
+ def value_field
133
+ BlacklightBrowseNearby::Engine.config.value_field
134
+ end
135
+
136
+ # Convenience method to return a configuration option.
137
+ def reverse_shelfkey_field
138
+ BlacklightBrowseNearby::Engine.config.reverse_sortkey_field
139
+ end
140
+
141
+ # Convenience method to return a configuration option.
142
+ def shelfkey_field
143
+ BlacklightBrowseNearby::Engine.config.sortkey_field
144
+ end
145
+
146
+ # Convenience method to return a configuration option.
147
+ def combined_key_field
148
+ BlacklightBrowseNearby::Engine.config.combined_key_field
149
+ end
150
+
151
+ # Convenience method to return a configuration option.
152
+ def delimiter
153
+ BlacklightBrowseNearby::Engine.config.key_delimiter
154
+ end
155
+
156
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module BlacklightBrowseNearby::Controller
3
+ extend ActiveSupport::Concern
4
+ include ActionView::Helpers::CaptureHelper
5
+ include ActionView::Context
6
+
7
+ included do
8
+ before_filter :blacklight_browse_nearby, :only => :show
9
+ end
10
+
11
+ protected
12
+ def blacklight_browse_nearby
13
+ @nearby = BlacklightBrowseNearby.new(params[:id])
14
+ @blacklight_browse_nearby_items = @nearby.documents
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ class BlacklightBrowseNearby
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class BlacklightBrowseNearby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ BlacklightBrowseNearby::Engine.config.value_field = "value_display"
2
+ BlacklightBrowseNearby::Engine.config.sortkey_field = "shelfkey"
3
+ BlacklightBrowseNearby::Engine.config.reverse_sortkey_field = "reverse_shelfkey"
4
+ BlacklightBrowseNearby::Engine.config.combined_key_field = "combined_shelfkey"
5
+ BlacklightBrowseNearby::Engine.config.key_delimiter = "-|-"
6
+ BlacklightBrowseNearby::Engine.config.combined_key_pattern = "#{BlacklightBrowseNearby::Engine.config.value_field} #{BlacklightBrowseNearby::Engine.config.key_delimiter} #{BlacklightBrowseNearby::Engine.config.sortkey_field} #{BlacklightBrowseNearby::Engine.config.key_delimiter} #{BlacklightBrowseNearby::Engine.config.reverse_sortkey_field}"
7
+ BlacklightBrowseNearby::Engine.config.request_handler = "alphaTerms"
8
+ BlacklightBrowseNearby::Engine.config.default_hits = "5"
9
+ BlacklightBrowseNearby::Engine.config.full_view_default_hits = "11"
10
+ BlacklightBrowseNearby::Engine.config.nearby_fields = [BlacklightBrowseNearby::Engine.config.value_field]
11
+ BlacklightBrowseNearby::Engine.config.link_field = "title_display"
@@ -0,0 +1,42 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rails/generators'
3
+ require 'rails/generators/migration'
4
+ class BlacklightBrowseNearbyGenerator < Rails::Generators::Base
5
+
6
+ desc """
7
+ This generator makes the following changes to your application:
8
+ 1. Adds Controller behavior to the application's Blacklight generated CatalogController.
9
+ 2. Adds a line in your application.css and application.js to load the BlacklightBrowseNearby assets
10
+ """
11
+
12
+ # Add BlacklightBrowseNearby::CatalogExtension to the application's Blacklight generated CatalogController.
13
+ def inject_blacklight_browse_nearby_controller_behavior
14
+ unless IO.read("app/controllers/catalog_controller.rb").include?("BlacklightBrowseNearby::CatalogExtension")
15
+ inject_into_class "app/controllers/catalog_controller.rb", "CatalogController" do
16
+ " # Adds a before filter to load nearby items\n" +
17
+ " include BlacklightBrowseNearby::Controller\n\n"
18
+ end
19
+ end
20
+ end
21
+
22
+ # insert require statements into application level CSS/JS manifestes.
23
+ def inject_blacklight_browse_nearby_require
24
+ unless IO.read("app/assets/stylesheets/application.css").include?("Required by BlacklightBrowseNearby")
25
+ insert_into_file "app/assets/stylesheets/application.css", :after => "/*" do
26
+ %q{
27
+ * Required by BlacklightBrowseNearby:
28
+ *= require blacklight_browse_nearby/blacklight_browse_nearby
29
+ *}
30
+ end
31
+ end
32
+ unless IO.read("app/assets/javascripts/application.js").include?("Required by BlacklightBrowseNearby")
33
+ insert_into_file "app/assets/javascripts/application.js", :before => "//= require_tree ." do
34
+ %q{// Required by BlacklightBrowseNearby:
35
+ //= require blacklight_browse_nearby/blacklight_browse_nearby
36
+ }
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :blacklight_browse_nearby do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,283 @@
1
+ require "spec_helper"
2
+ describe "BlacklightBrowseNearby" do
3
+ before(:each) do
4
+ @document_response = {"hits" => "1"}
5
+ @original_document = {"id" => "666", "value_display" => ["FFFF", "NNNN"], "shelfkey" => ["ffff", "nnnn"], "reverse_shelfkey" => ["zzzz", "mmmm"], "combined_shelfkey" => ["FFFF -|- ffff -|- uuuu", "NNNN -|- nnnn -|- mmmm"]}
6
+ @previous_terms = {"terms" => {"reverse_shelfkey" => ["yyyy", 1, "xxxx", 1, "wwww", 1, "vvvv", 1, "uuuu", 1]}}
7
+ @next_terms = {"terms" => {"shelfkey" => ["gggg", 1, "hhhh", 1, "iiii", 1, "jjjj", 1, "kkkk", 1]}}
8
+ @previous_documents = [{"id"=>"222", "value_display" => "BBBB"},
9
+ {"id"=>"333", "value_display" => "CCCC"},
10
+ {"id"=>"444", "value_display" => "DDDD"},
11
+ {"id"=>"555", "value_display" => "EEEE"}]
12
+ @next_documents = [{"id"=>"777", "value_display" => "GGGG"},
13
+ {"id"=>"888", "value_display" => "HHHH"},
14
+ {"id"=>"999", "value_display" => "IIII"},
15
+ {"id"=>"1010", "value_display" => "JJJJ"}]
16
+ end
17
+ it "should combine the previous, current, and next documents returned from solr" do
18
+ Blacklight.stub(:solr).and_return(mock("solr"))
19
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
20
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
21
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
22
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
23
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
24
+ docs = BlacklightBrowseNearby.new("123").documents
25
+ docs.should be_a(Array)
26
+ docs.length.should == 9
27
+ docs.map{|d| d["value_display"] }.should == [@previous_documents,@original_document,@next_documents].flatten.map{|d| d["value_display"]}
28
+ docs.map{|d| d["value_display"] }.should == ["BBBB", "CCCC", "DDDD", "EEEE", ["FFFF", "NNNN"], "GGGG", "HHHH", "IIII", "JJJJ"]
29
+ end
30
+
31
+ it "should return an embty array if the object does not have all the required fields" do
32
+ Blacklight.stub(:solr).and_return(mock("solr"))
33
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response, {"id"=>"someid", "title_display" => "MyDocument"}])
34
+ BlacklightBrowseNearby.new("123").documents.should be_blank
35
+ end
36
+
37
+ describe "originating document" do
38
+ it "should have the original document available" do
39
+ Blacklight.stub(:solr).and_return(mock("solr"))
40
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
41
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
42
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
43
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
44
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
45
+ nearby = BlacklightBrowseNearby.new("123")
46
+ nearby.original_document.should == @original_document
47
+ end
48
+ describe "current_value" do
49
+ it "should return the current value of the originating document" do
50
+ Blacklight.stub(:solr).and_return(mock("solr"))
51
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
52
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
53
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
54
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
55
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
56
+ nearby = BlacklightBrowseNearby.new("123")
57
+ nearby.current_value.should == @original_document["value_display"].first
58
+ end
59
+ it "should return the preferred value if one is provided " do
60
+ Blacklight.stub(:solr).and_return(mock("solr"))
61
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"nnnn", :"terms.limit" => 2}}).and_return(@next_terms)
62
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"mmmm", :"terms.limit"=>2}}).and_return(@previous_terms)
63
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
64
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
65
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
66
+ nearby = BlacklightBrowseNearby.new("123", :preferred_value=>"NNNN")
67
+ nearby.current_value.should == @original_document["value_display"].last
68
+ end
69
+ end
70
+ describe "potential_values" do
71
+ it "should return all the value fields from the current document" do
72
+ Blacklight.stub(:solr).and_return(mock("solr"))
73
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
74
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
75
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
76
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
77
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
78
+ nearby = BlacklightBrowseNearby.new("123")
79
+ nearby.potential_values.should == @original_document["value_display"]
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "Options" do
85
+ describe "start_of_terms" do
86
+ it "should start at the beginning of the terms when there is no page" do
87
+ Blacklight.stub(:solr).and_return(mock("solr"))
88
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
89
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
90
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
91
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
92
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
93
+ nearby = BlacklightBrowseNearby.new("123")
94
+ nearby.start_of_terms.should == 0
95
+ end
96
+ it "should start at the last set of terms based on the number requested" do
97
+ Blacklight.stub(:solr).and_return(mock("solr"))
98
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 7}}).and_return(@next_terms)
99
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
100
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["iiii", "jjjj", "kkkk"], {:per_page=>3}).and_return([@document_response, @next_documents]) # this isn't the right set of documents, but that's not what we're testing.
101
+ nearby = BlacklightBrowseNearby.new("123", :page => "1", :number => 5)
102
+ nearby.start_of_terms.should == 2
103
+ end
104
+ end
105
+
106
+ describe "total_terms" do
107
+ it "should return half of the number requested when on the inital query" do
108
+ Blacklight.stub(:solr).and_return(mock("solr"))
109
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 3}}).and_return(@next_terms)
110
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>3}}).and_return(@previous_terms)
111
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
112
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx", "wwww"], {:per_page=>3}).and_return([@document_response, @previous_documents])
113
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh", "iiii"], {:per_page=>3}).and_return([@document_response, @next_documents])
114
+ nearby = BlacklightBrowseNearby.new("123", :number => 7)
115
+ nearby.total_terms.should == 3
116
+ end
117
+ it "should return the correct number of of terms when paging" do
118
+ Blacklight.stub(:solr).and_return(mock("solr"))
119
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 4}}).and_return(@next_terms)
120
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
121
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["hhhh", "iiii", "jjjj"], {:per_page=>3}).and_return([@document_response, @next_documents]) # this isn't the right set of documents, but that's not what we're testing.
122
+ nearby = BlacklightBrowseNearby.new("123", :page => "1", :number => "3")
123
+ nearby.total_terms.should == 4
124
+ end
125
+ end
126
+
127
+ describe "hits_requested" do
128
+ it "should return the configured default when no page option is provided" do
129
+ Blacklight.stub(:solr).and_return(mock("solr"))
130
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
131
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
132
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
133
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
134
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
135
+ nearby = BlacklightBrowseNearby.new("123")
136
+ nearby.hits_requested.should == 5
137
+ end
138
+ it "should return the number option is one if provided" do
139
+ Blacklight.stub(:solr).and_return(mock("solr"))
140
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 3}}).and_return(@next_terms)
141
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>3}}).and_return(@previous_terms)
142
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
143
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx", "wwww"], {:per_page=>3}).and_return([@document_response, @previous_documents])
144
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh", "iiii"], {:per_page=>3}).and_return([@document_response, @next_documents])
145
+ nearby = BlacklightBrowseNearby.new("123", :number => 7)
146
+ nearby.hits_requested.should == 7
147
+ end
148
+ end
149
+
150
+ describe "original_query_offset" do
151
+ it "should return half the configured items in the absence of a requested number" do
152
+ Blacklight.stub(:solr).and_return(mock("solr"))
153
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
154
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
155
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
156
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
157
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
158
+ nearby = BlacklightBrowseNearby.new("123")
159
+ nearby.original_query_offset.should == 2
160
+ end
161
+ it "should return half the requested items in the presence of a number" do
162
+ Blacklight.stub(:solr).and_return(mock("solr"))
163
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 3}}).and_return(@next_terms)
164
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>3}}).and_return(@previous_terms)
165
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
166
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx", "wwww"], {:per_page=>3}).and_return([@document_response, @previous_documents])
167
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh", "iiii"], {:per_page=>3}).and_return([@document_response, @next_documents])
168
+ nearby = BlacklightBrowseNearby.new("123", :number => 7)
169
+ nearby.original_query_offset.should == 3
170
+ end
171
+ end
172
+
173
+ describe "normalized_page" do
174
+ it "should return 0 when no page option is provided" do
175
+ Blacklight.stub(:solr).and_return(mock("solr"))
176
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
177
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
178
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
179
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
180
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
181
+ nearby = BlacklightBrowseNearby.new("123")
182
+ nearby.normalized_page.should == 0
183
+ end
184
+ it "should return a positive integer when a negative page is requested" do
185
+ Blacklight.stub(:solr).and_return(mock("solr"))
186
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>7}}).and_return(@previous_terms)
187
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
188
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["wwww", "vvvv", "uuuu"], {:per_page=>3}).and_return([@document_response, @previous_documents]) # this isn't the right set of documents, but that's not what we're testing.
189
+ nearby = BlacklightBrowseNearby.new("123", :page => "-1")
190
+ nearby.normalized_page.should == 1
191
+ end
192
+ end
193
+
194
+ describe "Number option" do
195
+ it "should return the correct documents when pages are added" do
196
+ Blacklight.stub(:solr).and_return(mock("solr"))
197
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
198
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
199
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
200
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents]) # this isn't the right set of documents, but that's not what we're testing.
201
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents]) # this isn't the right set of documents, but that's not what we're testing.
202
+ nearby = BlacklightBrowseNearby.new("123", :number => "5")
203
+ nearby.total_terms.should == 2
204
+ nearby.start_of_terms.should == 0
205
+ end
206
+ end
207
+ describe "Page option" do
208
+ it "should handle simple paging w/ no number provided" do
209
+ Blacklight.stub(:solr).and_return(mock("solr"))
210
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 7}}).and_return(@next_terms)
211
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
212
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["iiii", "jjjj", "kkkk"], {:per_page=>3}).and_return([@document_response, @next_documents]) # this isn't the right set of documents, but that's not what we're testing.
213
+ nearby = BlacklightBrowseNearby.new("123", :page => "1")
214
+ nearby.total_terms.should == 7
215
+ nearby.start_of_terms.should == 2
216
+ end
217
+ it "should properly paginate the total and start hit counts" do
218
+ Blacklight.stub(:solr).and_return(mock("solr"))
219
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 7}}).and_return(@next_terms)
220
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
221
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["iiii", "jjjj", "kkkk"], {:per_page=>3}).and_return([@document_response, @next_documents]) # this isn't the right set of documents, but that's not what we're testing.
222
+ nearby = BlacklightBrowseNearby.new("123", :number => "5", :page => "1")
223
+ nearby.total_terms.should == 7
224
+ nearby.start_of_terms.should == 2
225
+ end
226
+ it "should properly handle negative page numbers" do
227
+ Blacklight.stub(:solr).and_return(mock("solr"))
228
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>7}}).and_return(@previous_terms)
229
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
230
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["wwww", "vvvv", "uuuu"], {:per_page=>3}).and_return([@document_response, @previous_documents]) # this isn't the right set of documents, but that's not what we're testing.
231
+ nearby = BlacklightBrowseNearby.new("123", :number => "5", :page => "-1")
232
+ nearby.total_terms.should == 7
233
+ nearby.start_of_terms.should == 2
234
+ end
235
+ end
236
+ end
237
+
238
+ describe "combined key" do
239
+ it "should correctly parse from the pattern" do
240
+ Blacklight.stub(:solr).and_return(mock("solr"))
241
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
242
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
243
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
244
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
245
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
246
+ nearby = BlacklightBrowseNearby.new("123")
247
+ combined_key = "value_displayMATCH -|- shelfkeyMATCH -|- reverse_shelfkeyMATCH"
248
+ ["shelfkey", "value_display", "reverse_shelfkey"].each do |part|
249
+ nearby.send(:get_value_from_combined_key, combined_key, part).should == "#{part}MATCH"
250
+ end
251
+ end
252
+ it "should return the first key when a preferred value is not requested" do
253
+ Blacklight.stub(:solr).and_return(mock("solr"))
254
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"ffff", :"terms.limit" => 2}}).and_return(@next_terms)
255
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"uuuu", :"terms.limit"=>2}}).and_return(@previous_terms)
256
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
257
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
258
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
259
+ nearby = BlacklightBrowseNearby.new("123")
260
+ nearby.combined_key.should == "FFFF -|- ffff -|- uuuu"
261
+ end
262
+ it "should return the first key when there is only one available" do
263
+ Blacklight.stub(:solr).and_return(mock("solr"))
264
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"nnnn", :"terms.limit" => 2}}).and_return(@next_terms)
265
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"mmmm", :"terms.limit"=>2}}).and_return(@previous_terms)
266
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document.merge("combined_shelfkey"=>[@original_document["combined_shelfkey"].last])])
267
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
268
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
269
+ nearby = BlacklightBrowseNearby.new("123")
270
+ nearby.combined_key.should == "NNNN -|- nnnn -|- mmmm"
271
+ end
272
+ it "should return the key matching the preferred value if one is given" do
273
+ Blacklight.stub(:solr).and_return(mock("solr"))
274
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"shelfkey", :"terms.lower"=>"nnnn", :"terms.limit" => 2}}).and_return(@next_terms)
275
+ Blacklight.solr.should_receive(:send_and_receive).with("alphaTerms", {:params => {:"terms.fl"=>"reverse_shelfkey", :"terms.lower"=>"mmmm", :"terms.limit"=>2}}).and_return(@previous_terms)
276
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_doc_id).and_return([@document_response,@original_document])
277
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("reverse_shelfkey", ["yyyy", "xxxx"], {:per_page=>2}).and_return([@document_response, @previous_documents])
278
+ BlacklightBrowseNearby.any_instance.stub(:get_solr_response_for_field_values).with("shelfkey", ["gggg", "hhhh"], {:per_page=>2}).and_return([@document_response, @next_documents])
279
+ nearby = BlacklightBrowseNearby.new("123", :preferred_value=>"NNNN")
280
+ nearby.combined_key.should == "NNNN -|- nnnn -|- mmmm"
281
+ end
282
+ end
283
+ end