Skip to content

Commit

Permalink
Deprecate default_per_page helper
Browse files Browse the repository at this point in the history
It was doing the same thing the configuation was already doing.
  • Loading branch information
jcoyne committed Apr 27, 2017
1 parent 4b3e868 commit d8b5822
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/helpers/blacklight/catalog_helper_behavior.rb
Expand Up @@ -121,7 +121,7 @@ def current_sort_field
#
# @return [Integer]
def current_per_page
(@response.rows if @response && @response.rows > 0) || params.fetch(:per_page, default_per_page).to_i
(@response.rows if @response && @response.rows > 0) || params.fetch(:per_page, blacklight_config.default_per_page).to_i
end

##
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/blacklight/configuration_helper_behavior.rb
Expand Up @@ -172,9 +172,8 @@ def default_sort_field

##
# The default value for search results per page
def default_per_page
blacklight_config.default_per_page || blacklight_config.per_page.first
end
delegate :default_per_page, to: :blacklight_config
deprecation_deprecate default_per_page: "Use blacklight_config.default_per_page instead"

##
# The available options for results per page, in the style of #options_for_select
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/blacklight/url_helper_behavior.rb
Expand Up @@ -111,10 +111,10 @@ def link_back_to_catalog(opts = { :label => nil })
query_params = current_search_session.try(:query_params) || ActionController::Parameters.new

if search_session['counter']
per_page = (search_session['per_page'] || default_per_page).to_i
per_page = (search_session['per_page'] || blacklight_config.default_per_page).to_i
counter = search_session['counter'].to_i

query_params[:per_page] = per_page unless search_session['per_page'].to_i == default_per_page
query_params[:per_page] = per_page unless search_session['per_page'].to_i == blacklight_config.default_per_page
query_params[:page] = ((counter - 1) / per_page) + 1
end

Expand Down
59 changes: 36 additions & 23 deletions spec/helpers/blacklight/configuration_helper_behavior_spec.rb
Expand Up @@ -43,13 +43,13 @@
blacklight_config.view.b.default = true
expect(helper.default_document_index_view_type).to eq :b
end

it "defaults to the first configured index view" do
allow(blacklight_config).to receive_messages(view: { a: true, b: true})
expect(helper.default_document_index_view_type).to eq :a
end
end

describe "#document_index_views" do
before do
blacklight_config.view.abc = false
Expand Down Expand Up @@ -101,7 +101,7 @@
f = helper.document_show_link_field document
expect(f).to eq :a
end

it "retrieves the first field with data" do
blacklight_config.index.title_field = [:zzz, :b]
f = helper.document_show_link_field document
Expand Down Expand Up @@ -145,31 +145,44 @@

end
end

describe "#default_per_page" do
it "is the configured default per page" do
allow(helper).to receive_messages(blacklight_config: double(default_per_page: 42))
expect(helper.default_per_page).to eq 42
before do
expect(Deprecation).to receive(:warn)
end

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

context "when default_per_page is configured" do
before do
blacklight_config.default_per_page = 42
end

it "is the configured value" do
expect(helper.default_per_page).to eq 42
end
end

context "when default_per_page is not configured" do
before do
blacklight_config.per_page = [11, 22]
end
it "is the first per-page value if a default isn't set" do
expect(helper.default_per_page).to eq 11
end
end
end

describe "#default_sort_field" do
it "is the configured default field" do
allow(helper).to receive_messages(blacklight_config: double(sort_fields: { a: double(default: nil), b: double(key: 'b', default: true) }))
expect(helper.default_sort_field.key).to eq 'b'
end

it "is the first per-page value if a default isn't set" do
allow(helper).to receive_messages(blacklight_config: double(sort_fields: { a: double(key: 'a', default: nil), b: double(key: 'b', default: nil) }))
expect(helper.default_sort_field.key).to eq 'a'
end
end

describe "#per_page_options_for_select" do
it "is the per-page values formatted as options_for_select" do
allow(helper).to receive_messages(blacklight_config: double(per_page: [11, 22, 33]))
Expand All @@ -178,36 +191,36 @@
expect(helper.per_page_options_for_select).to include ["33<span class=\"sr-only\"> per page</span>", 33]
end
end

describe "#should_render_field?" do
let(:field_config) { double('field config', if: true, unless: false) }

before do
allow(helper).to receive_messages(document_has_value?: true)
end

it "is true" do
expect(helper.should_render_field?(field_config)).to be true
end

it "is false if the :if condition is false" do
allow(field_config).to receive_messages(if: false)
expect(helper.should_render_field?(field_config)).to be false
end

it "is false if the :unless condition is true" do
allow(field_config).to receive_messages(unless: true)
expect(helper.should_render_field?(field_config)).to be false
end
end

describe "#search_field_options_for_select" do

before do

@config = Blacklight::Configuration.new do |config|
config.default_solr_params = { :qt => 'search' }

config.add_search_field 'all_fields', :label => 'All Fields'
config.add_search_field 'title', :qt => 'title_search'
config.add_search_field 'author', :qt => 'author_search'
Expand All @@ -217,7 +230,7 @@

allow(helper).to receive_messages(blacklight_config: @config)
end

it "returns proper options_for_select arguments" do

select_arguments = helper.search_field_options_for_select
Expand All @@ -227,7 +240,7 @@

expect(label).to eq config_hash.label
expect(key).to eq config_hash.key
end
end
end

it "does not include fields in select if :display_in_simple_search=>false" do
Expand Down

0 comments on commit d8b5822

Please sign in to comment.