<%= link_to_previous_page @pagination, raw(t('views.pagination.previous')), :params => params, :param_name => Blacklight::Solr::FacetPaginator.request_keys[:page], :class => 'btn btn-link', :data => {:ajax_modal => "preserve"} do %>
<%= raw(t('views.pagination.previous')) %>
diff --git a/app/views/catalog/facet.html.erb b/app/views/catalog/facet.html.erb
index 6da0051b58..70764e550d 100644
--- a/app/views/catalog/facet.html.erb
+++ b/app/views/catalog/facet.html.erb
@@ -8,18 +8,7 @@
-
-
- <% @pagination.items.each do |item| %>
- -
- <% if facet_in_params?( params[:id], item.value ) %>
- <%= render_selected_facet_value(params[:id], item) %>
- <% else %>
- <%= render_facet_value(params[:id], item) %>
- <% end -%>
-
- <% end %>
-
+ <%= render_facet_limit(@display_facet, layout: false) %>
diff --git a/lib/blacklight/catalog.rb b/lib/blacklight/catalog.rb
index 875749ab16..cea2399ff1 100644
--- a/lib/blacklight/catalog.rb
+++ b/lib/blacklight/catalog.rb
@@ -64,7 +64,12 @@ def update
# displays values and pagination links for a single facet field
def facet
@facet = blacklight_config.facet_fields[params[:id]]
- @pagination = get_facet_pagination(@facet.field, params)
+ @response = get_facet_field_response(@facet.field, params)
+ @display_facet = @response.facets.first
+
+ # @pagination was deprecated in Blacklight 5.1
+ @pagination = facet_paginator(@facet, @display_facet)
+
respond_to do |format|
# Draw the facet selector for users who have javascript disabled:
@@ -176,6 +181,7 @@ def render_search_results_as_json
def search_facets_as_json
facets_from_request.as_json.each do |f|
+ f.delete "options"
f["label"] = facet_configuration_for_field(f["name"]).label
f["items"] = f["items"].as_json.each do |i|
i['label'] ||= i['value']
diff --git a/lib/blacklight/facet.rb b/lib/blacklight/facet.rb
index bbd83c0391..f390b9356a 100644
--- a/lib/blacklight/facet.rb
+++ b/lib/blacklight/facet.rb
@@ -3,6 +3,14 @@
#
module Blacklight
module Facet
+
+ def facet_paginator field_config, display_facet
+ Blacklight::Solr::FacetPaginator.new(display_facet.items,
+ sort: display_facet.sort,
+ offset: display_facet.offset,
+ limit: facet_limit_for(field_config.field))
+ end
+
def facets_from_request(fields = facet_field_names)
fields.map { |solr_field| facet_by_field_name(solr_field) }.compact
end
diff --git a/lib/blacklight/solr_helper.rb b/lib/blacklight/solr_helper.rb
index a049827423..71c31d6f57 100644
--- a/lib/blacklight/solr_helper.rb
+++ b/lib/blacklight/solr_helper.rb
@@ -487,13 +487,14 @@ def get_solr_response_for_field_values(field, values, extra_solr_params = {})
# method facet_list_limit, otherwise 20.
def solr_facet_params(facet_field, user_params=params || {}, extra_controller_params={})
input = user_params.deep_merge(extra_controller_params)
+ facet_config = blacklight_config.facet_fields[facet_field]
# First start with a standard solr search params calculations,
# for any search context in our request params.
solr_params = solr_search_params(user_params).merge(extra_controller_params)
# Now override with our specific things for fetching facet values
- solr_params[:"facet.field"] = facet_field
+ solr_params[:"facet.field"] = with_ex_local_param((facet_config.ex if facet_config.respond_to?(:ex)), facet_field)
limit =
@@ -515,26 +516,31 @@ def solr_facet_params(facet_field, user_params=params || {}, extra_controller_pa
return solr_params
end
+ ##
+ # Get the solr response when retrieving only a single facet field
+ def get_facet_field_response(facet_field, user_params = params || {}, extra_controller_params = {})
+ solr_params = solr_facet_params(facet_field, user_params, extra_controller_params)
+ # Make the solr call
+ find(blacklight_config.qt, solr_params)
+ end
+
# a solr query method
# used to paginate through a single facet field's values
# /catalog/facet/language_facet
def get_facet_pagination(facet_field, user_params=params || {}, extra_controller_params={})
-
- solr_params = solr_facet_params(facet_field, user_params, extra_controller_params)
-
# Make the solr call
- response =find(blacklight_config.qt, solr_params)
+ response = get_facet_field_response(facet_field, user_params, extra_controller_params)
+
+ limit = response.params[:"f.#{facet_field}.facet.limit"].to_s.to_i - 1
- limit = solr_params[:"f.#{facet_field}.facet.limit"] -1
-
# Actually create the paginator!
# NOTE: The sniffing of the proper sort from the solr response is not
# currently tested for, tricky to figure out how to test, since the
# default setup we test against doesn't use this feature.
return Blacklight::Solr::FacetPaginator.new(response.facets.first.items,
- :offset => solr_params[:"f.#{facet_field}.facet.offset"],
+ :offset => response.params[:"f.#{facet_field}.facet.offset"],
:limit => limit,
- :sort => response["responseHeader"]["params"][:"f.#{facet_field}.facet.sort"] || response["responseHeader"]["params"]["facet.sort"]
+ :sort => response.params[:"f.#{facet_field}.facet.sort"] || response.params["facet.sort"]
)
end
@@ -612,23 +618,23 @@ def get_opensearch_response(field=nil, extra_controller_params={})
# a facet paginator with the right limit.
def facet_limit_for(facet_field)
facet = blacklight_config.facet_fields[facet_field]
- return nil if facet.blank?
-
- limit = facet.limit
-
- if ( limit == true && @response &&
- @response["responseHeader"] &&
- @response["responseHeader"]["params"])
- limit =
- @response["responseHeader"]["params"]["f.#{facet_field}.facet.limit"] ||
- @response["responseHeader"]["params"]["facet.limit"]
- limit = (limit.to_i() -1) if limit
- limit = nil if limit == -2 # -1-1==-2, unlimited.
- elsif limit == true
- limit = nil
- end
- return limit
+ return if facet.blank?
+
+ if facet.limit and @response
+ limit = @response.params["f.#{facet_field}.facet.limit"] ||
+ @response.params["facet.limit"]
+
+ if limit.blank? # we didn't get or a set a limit, so infer one.
+ facet.limit if facet.limit != true
+ elsif limit == -1 # limit -1 is solr-speak for unlimited
+ nil
+ else
+ limit.to_i - 1 # we added 1 to find out if we needed to paginate
+ end
+ elsif (facet.limit and facet.limit != true)
+ facet.limit
+ end
end
##
diff --git a/lib/blacklight/solr_response/facets.rb b/lib/blacklight/solr_response/facets.rb
index 39dc8fda1c..70dade7b21 100644
--- a/lib/blacklight/solr_response/facets.rb
+++ b/lib/blacklight/solr_response/facets.rb
@@ -29,8 +29,21 @@ def as_json(props = nil)
# represents a facet; which is a field and its values
class FacetField
attr_reader :name, :items
- def initialize name, items
+ def initialize name, items, options = {}
@name, @items = name, items
+ @options = options
+ end
+
+ def limit
+ @options[:limit]
+ end
+
+ def sort
+ @options[:sort] || 'index'
+ end
+
+ def offset
+ @options[:offset] || 0
end
end
@@ -40,15 +53,20 @@ def initialize name, items
# end
# "caches" the result in the @facets instance var
def facets
- @facets ||= (
+ @facets ||= begin
facet_fields.map do |(facet_field_name,values_and_hits)|
items = []
+ options = {}
values_and_hits.each_slice(2) do |k,v|
items << FacetItem.new(:value => k, :hits => v)
end
- FacetField.new(facet_field_name, items)
+
+ options[:sort] = params[:"f.#{facet_field_name}.facet.sort"] || params['facet.sort']
+ options[:offset] = params[:"f.#{facet_field_name}.facet.offset"].to_i
+
+ FacetField.new(facet_field_name, items, options)
end
- )
+ end
end
# pass in a facet field name and get back a Facet instance
diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb
index 35f7d7f49d..ce63bafc35 100644
--- a/spec/controllers/catalog_controller_spec.rb
+++ b/spec/controllers/catalog_controller_spec.rb
@@ -537,6 +537,9 @@ def export_as_mock
it "should be successful" do
get :facet, id: 'format'
expect(response).to be_successful
+ expect(assigns[:response]).to be_kind_of Blacklight::SolrResponse
+ expect(assigns[:facet]).to be_kind_of Blacklight::Configuration::FacetField
+ expect(assigns[:display_facet]).to be_kind_of Blacklight::SolrResponse::Facets::FacetField
expect(assigns[:pagination]).to be_kind_of Blacklight::Solr::FacetPaginator
end
end
diff --git a/spec/lib/blacklight/solr_helper_spec.rb b/spec/lib/blacklight/solr_helper_spec.rb
index 4971da2b56..d90a56531f 100644
--- a/spec/lib/blacklight/solr_helper_spec.rb
+++ b/spec/lib/blacklight/solr_helper_spec.rb
@@ -1061,11 +1061,11 @@ def blacklight_config
# Okay, this is cheesy, since we included SolrHelper directly
# into our example groups, we need to set an iVar here, so it will
# use it.
- @response = {"responseHeader" => {"params" => {"facet.limit" => 11}}}
+ @response = double(params:{"facet.limit" => 11})
expect(facet_limit_for("language_facet")).to eq 10
end
it "should get from specific field in @response if available" do
- @response = {"responseHeader" => {"params" => {"facet.limit" => 11,"f.language_facet.facet.limit" => 16}}}
+ @response = double(params: {"facet.limit" => 11,"f.language_facet.facet.limit" => 16})
expect(facet_limit_for("language_facet")).to eq 15
end
end
diff --git a/spec/views/catalog/_facets.html.erb_spec.rb b/spec/views/catalog/_facets.html.erb_spec.rb
index 69c9a5e6a9..a3b2023528 100644
--- a/spec/views/catalog/_facets.html.erb_spec.rb
+++ b/spec/views/catalog/_facets.html.erb_spec.rb
@@ -26,7 +26,7 @@
before do
blacklight_config.facet_fields['facet_field_1'] = facet_field
- @mock_display_facet_1 = double(:name => 'facet_field_1', :items => [Blacklight::SolrResponse::Facets::FacetItem.new(:value => 'Value', :hits => 1234)])
+ @mock_display_facet_1 = double(:name => 'facet_field_1', sort: nil, offset: nil, :items => [Blacklight::SolrResponse::Facets::FacetItem.new(:value => 'Value', :hits => 1234)])
view.stub(:facet_field_names => [:facet_field_1],
:facet_limit_for => 10 )
diff --git a/spec/views/catalog/facet.html.erb_spec.rb b/spec/views/catalog/facet.html.erb_spec.rb
new file mode 100644
index 0000000000..54cd5dd11a
--- /dev/null
+++ b/spec/views/catalog/facet.html.erb_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe 'catalog/facet.html.erb' do
+ let(:display_facet) { double }
+ let(:blacklight_config) { Blacklight::Configuration.new }
+ before :each do
+ blacklight_config.add_facet_field 'xyz', label: "Facet title"
+ view.stub(:blacklight_config).and_return(blacklight_config)
+ stub_template 'catalog/_facet_pagination.html.erb' => 'pagination'
+ assign :facet, blacklight_config.facet_fields['xyz']
+ assign :display_facet, display_facet
+ end
+
+ it "should have the facet title" do
+ view.stub(:render_facet_limit)
+ render
+ expect(rendered).to have_selector 'h3', text: "Facet title"
+ end
+
+ it "should render facet pagination" do
+ view.stub(:render_facet_limit)
+ render
+ expect(rendered).to have_content 'pagination'
+ end
+
+ it "should render the facet limit" do
+ view.should_receive(:render_facet_limit).with(display_facet, layout: false)
+ render
+ end
+end
\ No newline at end of file