From d8b5822463e574417c0f760e8d7fff9c49970024 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Thu, 27 Apr 2017 09:53:18 -0500 Subject: [PATCH] Deprecate default_per_page helper It was doing the same thing the configuation was already doing. --- .../blacklight/catalog_helper_behavior.rb | 2 +- .../configuration_helper_behavior.rb | 5 +- app/helpers/blacklight/url_helper_behavior.rb | 4 +- .../configuration_helper_behavior_spec.rb | 59 +++++++++++-------- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/app/helpers/blacklight/catalog_helper_behavior.rb b/app/helpers/blacklight/catalog_helper_behavior.rb index b20b445294..bdf7659117 100644 --- a/app/helpers/blacklight/catalog_helper_behavior.rb +++ b/app/helpers/blacklight/catalog_helper_behavior.rb @@ -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 ## diff --git a/app/helpers/blacklight/configuration_helper_behavior.rb b/app/helpers/blacklight/configuration_helper_behavior.rb index 38d4fecfe9..7c13ed20c5 100644 --- a/app/helpers/blacklight/configuration_helper_behavior.rb +++ b/app/helpers/blacklight/configuration_helper_behavior.rb @@ -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 diff --git a/app/helpers/blacklight/url_helper_behavior.rb b/app/helpers/blacklight/url_helper_behavior.rb index db0772f887..102aaa1f0b 100644 --- a/app/helpers/blacklight/url_helper_behavior.rb +++ b/app/helpers/blacklight/url_helper_behavior.rb @@ -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 diff --git a/spec/helpers/blacklight/configuration_helper_behavior_spec.rb b/spec/helpers/blacklight/configuration_helper_behavior_spec.rb index 09f91ec1bb..4569a8a02f 100644 --- a/spec/helpers/blacklight/configuration_helper_behavior_spec.rb +++ b/spec/helpers/blacklight/configuration_helper_behavior_spec.rb @@ -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 @@ -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 @@ -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])) @@ -178,10 +191,10 @@ expect(helper.per_page_options_for_select).to include ["33 per page", 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 @@ -189,12 +202,12 @@ 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 @@ -202,12 +215,12 @@ 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' @@ -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 @@ -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