Skip to content

Commit

Permalink
add default_per_page configuration option, and add a #per_page_option…
Browse files Browse the repository at this point in the history
…s_for_select helper to format the per page options for the template; fixes #818
  • Loading branch information
cbeer committed Mar 9, 2014
1 parent 60b6da3 commit 5240310
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
12 changes: 10 additions & 2 deletions app/helpers/blacklight/configuration_helper_behavior.rb
Expand Up @@ -116,6 +116,14 @@ def default_sort_field
##
# The default value for search results per page
def default_per_page
blacklight_config.per_page.first unless blacklight_config.per_page.blank?
blacklight_config.default_per_page || blacklight_config.per_page.first
end
end

##
# The available options for results per page, in the style of #options_for_select
def per_page_options_for_select
blacklight_config.per_page.map do |count|
[t(:'blacklight.search.per_page.label', :count => count).html_safe, count]
end
end
end
8 changes: 4 additions & 4 deletions app/views/catalog/_per_page_widget.html.erb
Expand Up @@ -4,9 +4,9 @@
<div id="per_page-dropdown" class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><%= link_to(t(:'blacklight.search.per_page.button_label', :count => current_per_page), "#") %> <span class="caret"></span></button>
<ul class="dropdown-menu">
<%- blacklight_config.per_page.each do |count| %>
<li><%= link_to(t(:'blacklight.search.per_page.label', :count => count).html_safe, url_for(params_for_search(:per_page => count))) %></li>
<%- end -%>
<%- per_page_options_for_select.each do |(label, count)| %>
<li><%= link_to(label, url_for(params_for_search(:per_page => count))) %></li>
<%- end -%>
</ul>
</div>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions lib/blacklight/configuration.rb
Expand Up @@ -70,6 +70,7 @@ def default_values
:max_per_page => 100,
# Options for the user for number of results to show per page
:per_page => [10,20,50,100],
:default_per_page => nil,
# how many searches to save in session history
# (TODO: move the value into the configuration?)
:search_history_window => Blacklight::Catalog::SearchHistoryWindow,
Expand Down
23 changes: 22 additions & 1 deletion spec/helpers/configuration_helper_spec.rb
Expand Up @@ -100,4 +100,25 @@
label = helper.solr_field_label "default text", :key_a, :key_b
end
end
end

describe "#default_per_page" do
it "should be the configured default per page" do
helper.stub(blacklight_config: double(default_per_page: 42))
expect(helper.default_per_page).to eq 42
end

it "should be the first per-page value if a default isn't set" do
helper.stub(blacklight_config: double(default_per_page: nil, per_page: [11, 22]))
expect(helper.default_per_page).to eq 11
end
end

describe "#per_page_options_for_select" do
it "should be the per-page values formatted as options_for_select" do
helper.stub(blacklight_config: double(per_page: [11, 22, 33]))
expect(helper.per_page_options_for_select).to include ["11<span class=\"sr-only\"> per page</span>", 11]
expect(helper.per_page_options_for_select).to include ["22<span class=\"sr-only\"> per page</span>", 22]
expect(helper.per_page_options_for_select).to include ["33<span class=\"sr-only\"> per page</span>", 33]
end
end
end

0 comments on commit 5240310

Please sign in to comment.