diff --git a/app/views/catalog/_facet_group.html.erb b/app/views/catalog/_facet_group.html.erb new file mode 100644 index 0000000000..6588109b33 --- /dev/null +++ b/app/views/catalog/_facet_group.html.erb @@ -0,0 +1,22 @@ +<% # main container for facets/limits menu -%> +<% if has_facet_values? facet_field_names(groupname) %> +
" class="facets sidenav"> + +
+ + +

+ <%= groupname.blank? ? t('blacklight.search.facets.title') : t("blacklight.search.facets-#{groupname}.title") %> +

+
+ +
-collapse" class="collapse panel-group"> + <%= render_facet_partials facet_field_names(groupname) %> +
+
+<% end %> diff --git a/app/views/catalog/_facets.html.erb b/app/views/catalog/_facets.html.erb index b986b6ca14..66beb757d9 100644 --- a/app/views/catalog/_facets.html.erb +++ b/app/views/catalog/_facets.html.erb @@ -1,22 +1,3 @@ -<% # main container for facets/limits menu -%> -<% if has_facet_values? %> -
- -
- - -

- <%= t('blacklight.search.facets.title') %> -

-
- -
- <%= render_facet_partials %> -
-
-<% end %> \ No newline at end of file +<% facet_group_names.each do |groupname| %> + <%= render partial: "facet_group", locals: {groupname: groupname} %> +<% end %> diff --git a/lib/blacklight/facet.rb b/lib/blacklight/facet.rb index bbd83c0391..e769f136bb 100644 --- a/lib/blacklight/facet.rb +++ b/lib/blacklight/facet.rb @@ -7,8 +7,12 @@ def facets_from_request(fields = facet_field_names) fields.map { |solr_field| facet_by_field_name(solr_field) }.compact end - def facet_field_names - blacklight_config.facet_fields.keys + def facet_field_names group=nil + blacklight_config.facet_fields.select {|facet,opts| group == opts[:group]}.keys + end + + def facet_group_names + blacklight_config.facet_fields.map {|facet,opts| opts[:group]}.uniq end def facet_configuration_for_field(field) diff --git a/spec/views/catalog/_facets.html.erb_spec.rb b/spec/views/catalog/_facets.html.erb_spec.rb index bccad28fd4..4545e86d28 100644 --- a/spec/views/catalog/_facets.html.erb_spec.rb +++ b/spec/views/catalog/_facets.html.erb_spec.rb @@ -5,34 +5,67 @@ @mock_config = Blacklight::Configuration.new view.stub(:blacklight_config => @mock_config) end - it "should not have a header if no facets are displayed" do - view.stub(:render_facet_partials => '') - render - expect(rendered).to_not have_selector('h4') - end - it "should have a header" do + describe "facet groups" do + describe "default facet group" do + it "should not have a header if no facets are displayed" do + view.stub(:render_facet_partials => '') + render + expect(rendered).to_not have_selector('h4') + end - @mock_field_1 = double(:field => 'facet_field_1', - :label => 'label') - @mock_display_facet_1 = double(:name => 'facet_field_1', :items => [Blacklight::SolrResponse::Facets::FacetItem.new(:value => 'Value', :hits => 1234)]) - view.stub(:facet_field_names => [:facet_field_1], + it "should have a header" do + + @mock_field_1 = double(:field => 'facet_field_1', + :label => 'label', :group => nil) + @mock_display_facet_1 = double(:name => 'facet_field_1', :items => [Blacklight::SolrResponse::Facets::FacetItem.new(:value => 'Value', :hits => 1234)]) + view.stub(:facet_group_names => [nil], :facet_field_names => [:facet_field_1], :facet_limit_for => 10 ) - @response = double() - @response.stub(:facet_by_field_name).with(:facet_field_1) { @mock_display_facet_1 } + @response = double() + @response.stub(:facet_by_field_name).with(:facet_field_1) { @mock_display_facet_1 } + + view.stub(:render_facet_partials => '') + render + expect(rendered).to have_selector('h4') + end + end - view.stub(:render_facet_partials => '') - render - expect(rendered).to have_selector('h4') + describe "named facet group" do + it "should not have a header if no facets are displayed" do + view.stub(:render_facet_partials => '') + render + expect(rendered).to_not have_selector('h4') + expect(rendered).to_not have_selector('#facets-group_1') + end + + it "should have a header" do + + @mock_field_1 = double(:field => 'facet_field_1', + :label => 'label', :group => 'group_1') + @mock_display_facet_1 = double(:name => 'facet_field_1', :items => [Blacklight::SolrResponse::Facets::FacetItem.new(:value => 'Value', :hits => 1234)]) + view.stub(:facet_group_names => [nil, 'group_1'], + :facet_limit_for => 10 ) + view.stub(:facet_field_names).with('group_1').and_return([:facet_field_1]) + + @response = double() + @response.stub(:facet_by_field_name).with(:facet_field_1) { @mock_display_facet_1 } + + view.stub(:render_facet_partials => '') + render + expect(rendered).to have_selector('h4') + expect(rendered).to have_selector('#facets-group_1') + end + + end end describe "facet display" do before do @mock_field_1 = double(:field => 'facet_field_1', - :label => 'label') + :label => 'label', :group => nil) @mock_display_facet_1 = double(:name => 'facet_field_1', :items => [Blacklight::SolrResponse::Facets::FacetItem.new(:value => 'Value', :hits => 1234)]) - view.stub(:facet_field_names => [:facet_field_1], + view.stub(:facet_group_names => [nil], :facet_field_names => [:facet_field_1], :facet_limit_for => 10 ) @response = double()