Skip to content

Commit b05f688

Browse files
committed
Merge pull request #777 from projectblacklight/facet-rendering
Use common facet rendering behavior on facet sidebar and facet action re...
2 parents c19d334 + 48a9bb6 commit b05f688

File tree

12 files changed

+109
-51
lines changed

12 files changed

+109
-51
lines changed

app/assets/stylesheets/blacklight/_facets.css.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383

8484
}
8585

86-
ul.facet_extended_list
86+
/* deprecated in Blacklight 5.x */
87+
ul.facet_extended_list, .facet_extended_list ul
8788
{
8889
@extend .list-unstyled;
8990

app/views/catalog/_facet_limit.html.erb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<ul class="facet-values list-unstyled">
2-
<% paginator =
3-
Blacklight::Solr::FacetPaginator.new(display_facet.items,
4-
:limit => facet_limit_for(solr_field))
2+
<% paginator = facet_paginator(facet_field, display_facet)
53
%>
64
<% paginator.items.each do |item| -%>
75
<li>
@@ -13,7 +11,7 @@
1311
</li>
1412
<% end %>
1513

16-
<% if(paginator.has_next?) %>
14+
<% if paginator.has_next? and params[:action] != "facet" %>
1715
<li class="more_facets_link"><%= link_to(t('blacklight.search.facets.more'), params.merge(:id => solr_field, :action=>"facet", :page => nil), :class => "more_facets_link") %></li>
1816
<% end %>
1917

app/views/catalog/_facet_pagination.html.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<div class="prev_next_links btn-group pull-left">
32
<%= 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 %>
43
<span class="disabled btn btn-disabled"><%= raw(t('views.pagination.previous')) %></span>

app/views/catalog/facet.html.erb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@
88
</div>
99
<div class="modal-body">
1010
<div class="facet_extended_list">
11-
12-
<ul class="facet_extended_list facet-values">
13-
<% @pagination.items.each do |item| %>
14-
<li>
15-
<% if facet_in_params?( params[:id], item.value ) %>
16-
<%= render_selected_facet_value(params[:id], item) %>
17-
<% else %>
18-
<%= render_facet_value(params[:id], item) %>
19-
<% end -%>
20-
</li>
21-
<% end %>
22-
</ul>
11+
<%= render_facet_limit(@display_facet, layout: false) %>
2312
</div>
2413
</div>
2514

lib/blacklight/catalog.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ def update
6464
# displays values and pagination links for a single facet field
6565
def facet
6666
@facet = blacklight_config.facet_fields[params[:id]]
67-
@pagination = get_facet_pagination(@facet.field, params)
67+
@response = get_facet_field_response(@facet.field, params)
68+
@display_facet = @response.facets.first
69+
70+
# @pagination was deprecated in Blacklight 5.1
71+
@pagination = facet_paginator(@facet, @display_facet)
72+
6873

6974
respond_to do |format|
7075
# Draw the facet selector for users who have javascript disabled:
@@ -176,6 +181,7 @@ def render_search_results_as_json
176181

177182
def search_facets_as_json
178183
facets_from_request.as_json.each do |f|
184+
f.delete "options"
179185
f["label"] = facet_configuration_for_field(f["name"]).label
180186
f["items"] = f["items"].as_json.each do |i|
181187
i['label'] ||= i['value']

lib/blacklight/facet.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
#
44
module Blacklight
55
module Facet
6+
7+
def facet_paginator field_config, display_facet
8+
Blacklight::Solr::FacetPaginator.new(display_facet.items,
9+
sort: display_facet.sort,
10+
offset: display_facet.offset,
11+
limit: facet_limit_for(field_config.field))
12+
end
13+
614
def facets_from_request(fields = facet_field_names)
715
fields.map { |solr_field| facet_by_field_name(solr_field) }.compact
816
end

lib/blacklight/solr_helper.rb

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,14 @@ def get_solr_response_for_field_values(field, values, extra_solr_params = {})
487487
# method facet_list_limit, otherwise 20.
488488
def solr_facet_params(facet_field, user_params=params || {}, extra_controller_params={})
489489
input = user_params.deep_merge(extra_controller_params)
490+
facet_config = blacklight_config.facet_fields[facet_field]
490491

491492
# First start with a standard solr search params calculations,
492493
# for any search context in our request params.
493494
solr_params = solr_search_params(user_params).merge(extra_controller_params)
494495

495496
# Now override with our specific things for fetching facet values
496-
solr_params[:"facet.field"] = facet_field
497+
solr_params[:"facet.field"] = with_ex_local_param((facet_config.ex if facet_config.respond_to?(:ex)), facet_field)
497498

498499

499500
limit =
@@ -515,26 +516,31 @@ def solr_facet_params(facet_field, user_params=params || {}, extra_controller_pa
515516
return solr_params
516517
end
517518

519+
##
520+
# Get the solr response when retrieving only a single facet field
521+
def get_facet_field_response(facet_field, user_params = params || {}, extra_controller_params = {})
522+
solr_params = solr_facet_params(facet_field, user_params, extra_controller_params)
523+
# Make the solr call
524+
find(blacklight_config.qt, solr_params)
525+
end
526+
518527
# a solr query method
519528
# used to paginate through a single facet field's values
520529
# /catalog/facet/language_facet
521530
def get_facet_pagination(facet_field, user_params=params || {}, extra_controller_params={})
522-
523-
solr_params = solr_facet_params(facet_field, user_params, extra_controller_params)
524-
525531
# Make the solr call
526-
response =find(blacklight_config.qt, solr_params)
532+
response = get_facet_field_response(facet_field, user_params, extra_controller_params)
533+
534+
limit = response.params[:"f.#{facet_field}.facet.limit"].to_s.to_i - 1
527535

528-
limit = solr_params[:"f.#{facet_field}.facet.limit"] -1
529-
530536
# Actually create the paginator!
531537
# NOTE: The sniffing of the proper sort from the solr response is not
532538
# currently tested for, tricky to figure out how to test, since the
533539
# default setup we test against doesn't use this feature.
534540
return Blacklight::Solr::FacetPaginator.new(response.facets.first.items,
535-
:offset => solr_params[:"f.#{facet_field}.facet.offset"],
541+
:offset => response.params[:"f.#{facet_field}.facet.offset"],
536542
:limit => limit,
537-
:sort => response["responseHeader"]["params"][:"f.#{facet_field}.facet.sort"] || response["responseHeader"]["params"]["facet.sort"]
543+
:sort => response.params[:"f.#{facet_field}.facet.sort"] || response.params["facet.sort"]
538544
)
539545
end
540546

@@ -612,23 +618,23 @@ def get_opensearch_response(field=nil, extra_controller_params={})
612618
# a facet paginator with the right limit.
613619
def facet_limit_for(facet_field)
614620
facet = blacklight_config.facet_fields[facet_field]
615-
return nil if facet.blank?
616-
617-
limit = facet.limit
618-
619-
if ( limit == true && @response &&
620-
@response["responseHeader"] &&
621-
@response["responseHeader"]["params"])
622-
limit =
623-
@response["responseHeader"]["params"]["f.#{facet_field}.facet.limit"] ||
624-
@response["responseHeader"]["params"]["facet.limit"]
625-
limit = (limit.to_i() -1) if limit
626-
limit = nil if limit == -2 # -1-1==-2, unlimited.
627-
elsif limit == true
628-
limit = nil
629-
end
630621

631-
return limit
622+
return if facet.blank?
623+
624+
if facet.limit and @response
625+
limit = @response.params["f.#{facet_field}.facet.limit"] ||
626+
@response.params["facet.limit"]
627+
628+
if limit.blank? # we didn't get or a set a limit, so infer one.
629+
facet.limit if facet.limit != true
630+
elsif limit == -1 # limit -1 is solr-speak for unlimited
631+
nil
632+
else
633+
limit.to_i - 1 # we added 1 to find out if we needed to paginate
634+
end
635+
elsif (facet.limit and facet.limit != true)
636+
facet.limit
637+
end
632638
end
633639

634640
##

lib/blacklight/solr_response/facets.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,21 @@ def as_json(props = nil)
2929
# represents a facet; which is a field and its values
3030
class FacetField
3131
attr_reader :name, :items
32-
def initialize name, items
32+
def initialize name, items, options = {}
3333
@name, @items = name, items
34+
@options = options
35+
end
36+
37+
def limit
38+
@options[:limit]
39+
end
40+
41+
def sort
42+
@options[:sort] || 'index'
43+
end
44+
45+
def offset
46+
@options[:offset] || 0
3447
end
3548
end
3649

@@ -40,15 +53,20 @@ def initialize name, items
4053
# end
4154
# "caches" the result in the @facets instance var
4255
def facets
43-
@facets ||= (
56+
@facets ||= begin
4457
facet_fields.map do |(facet_field_name,values_and_hits)|
4558
items = []
59+
options = {}
4660
values_and_hits.each_slice(2) do |k,v|
4761
items << FacetItem.new(:value => k, :hits => v)
4862
end
49-
FacetField.new(facet_field_name, items)
63+
64+
options[:sort] = params[:"f.#{facet_field_name}.facet.sort"] || params['facet.sort']
65+
options[:offset] = params[:"f.#{facet_field_name}.facet.offset"].to_i
66+
67+
FacetField.new(facet_field_name, items, options)
5068
end
51-
)
69+
end
5270
end
5371

5472
# pass in a facet field name and get back a Facet instance

spec/controllers/catalog_controller_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ def export_as_mock
537537
it "should be successful" do
538538
get :facet, id: 'format'
539539
expect(response).to be_successful
540+
expect(assigns[:response]).to be_kind_of Blacklight::SolrResponse
541+
expect(assigns[:facet]).to be_kind_of Blacklight::Configuration::FacetField
542+
expect(assigns[:display_facet]).to be_kind_of Blacklight::SolrResponse::Facets::FacetField
540543
expect(assigns[:pagination]).to be_kind_of Blacklight::Solr::FacetPaginator
541544
end
542545
end

spec/lib/blacklight/solr_helper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,11 +1061,11 @@ def blacklight_config
10611061
# Okay, this is cheesy, since we included SolrHelper directly
10621062
# into our example groups, we need to set an iVar here, so it will
10631063
# use it.
1064-
@response = {"responseHeader" => {"params" => {"facet.limit" => 11}}}
1064+
@response = double(params:{"facet.limit" => 11})
10651065
expect(facet_limit_for("language_facet")).to eq 10
10661066
end
10671067
it "should get from specific field in @response if available" do
1068-
@response = {"responseHeader" => {"params" => {"facet.limit" => 11,"f.language_facet.facet.limit" => 16}}}
1068+
@response = double(params: {"facet.limit" => 11,"f.language_facet.facet.limit" => 16})
10691069
expect(facet_limit_for("language_facet")).to eq 15
10701070
end
10711071
end

0 commit comments

Comments
 (0)