Skip to content

Commit

Permalink
Use common facet rendering behavior on facet sidebar and facet action…
Browse files Browse the repository at this point in the history
… rendering
  • Loading branch information
cbeer committed Feb 12, 2014
1 parent f2dad4d commit 6dd5c03
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 34 deletions.
3 changes: 2 additions & 1 deletion app/assets/stylesheets/blacklight/_facets.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@

}

ul.facet_extended_list
/* deprecated in Blacklight 5.x */
ul.facet_extended_list, .facet_extended_list ul
{
@extend .list-unstyled;

Expand Down
6 changes: 2 additions & 4 deletions app/views/catalog/_facet_limit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<ul class="facet-values list-unstyled">
<% paginator =
Blacklight::Solr::FacetPaginator.new(display_facet.items,
:limit => facet_limit_for(solr_field))
<% paginator = facet_paginator(facet_field, display_facet)
%>
<% paginator.items.each do |item| -%>
<li>
Expand All @@ -13,7 +11,7 @@
</li>
<% end %>
<% if(paginator.has_next?) %>
<% if paginator.has_next? and params[:action] != "facet" %>
<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>
<% end %>

Expand Down
1 change: 0 additions & 1 deletion app/views/catalog/_facet_pagination.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<div class="prev_next_links btn-group pull-left">
<%= 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 %>
<span class="disabled btn btn-disabled"><%= raw(t('views.pagination.previous')) %></span>
Expand Down
13 changes: 1 addition & 12 deletions app/views/catalog/facet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,7 @@
</div>
<div class="modal-body">
<div class="facet_extended_list">

<ul class="facet_extended_list facet-values">
<% @pagination.items.each do |item| %>
<li>
<% if facet_in_params?( params[:id], item.value ) %>
<%= render_selected_facet_value(params[:id], item) %>
<% else %>
<%= render_facet_value(params[:id], item) %>
<% end -%>
</li>
<% end %>
</ul>
<%= render_facet_limit(@display_facet, layout: false) %>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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']
Expand Down
8 changes: 8 additions & 0 deletions lib/blacklight/facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 16 additions & 10 deletions lib/blacklight/solr_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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

Expand Down Expand Up @@ -616,7 +622,7 @@ def facet_limit_for(facet_field)

limit = facet.limit

if ( limit == true && @response &&
if ( limit && @response &&
@response["responseHeader"] &&
@response["responseHeader"]["params"])
limit =
Expand Down
26 changes: 22 additions & 4 deletions lib/blacklight/solr_response/facets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/views/catalog/_facets.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 )

Expand Down
30 changes: 30 additions & 0 deletions spec/views/catalog/facet.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6dd5c03

Please sign in to comment.