diff --git a/app/controllers/lti/lti_apps_controller.rb b/app/controllers/lti/lti_apps_controller.rb index 645365a91b5..5cdffc8d541 100644 --- a/app/controllers/lti/lti_apps_controller.rb +++ b/app/controllers/lti/lti_apps_controller.rb @@ -35,8 +35,9 @@ module Lti if authorized_action(@context, @current_user, :update) placements = params['placements'] || [] collection = AppLaunchCollator.bookmarked_collection(@context, placements) + pagination_args = {max_per_page: 100} respond_to do |format| - launch_defs = Api.paginate(collection, self, named_context_url(@context, :api_v1_context_launch_definitions_url, include_host: true)) + launch_defs = Api.paginate(collection, self, named_context_url(@context, :api_v1_context_launch_definitions_url, include_host: true), pagination_args) format.json { render :json => AppLaunchCollator.launch_definitions(launch_defs, placements) } end end diff --git a/app/views/shared/_select_content_dialog.html.erb b/app/views/shared/_select_content_dialog.html.erb index 1811f98552d..db2dfef4962 100644 --- a/app/views/shared/_select_content_dialog.html.erb +++ b/app/views/shared/_select_content_dialog.html.erb @@ -83,7 +83,7 @@ <%= t :external_tool_notice, "Select a tool from the list below, or enter a URL for an external tool you already know is configured with Basic LTI to add a link to it to this *module*.", :wrapper => '\1' %> - +
diff --git a/lib/api.rb b/lib/api.rb index 080703ad0cb..a94aff635dc 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -257,10 +257,14 @@ module Api Setting.get('api_max_per_page', '50').to_i end + def self.per_page + Setting.get('api_per_page', '10').to_i + end + def self.per_page_for(controller, options={}) - per_page = controller.params[:per_page] || options[:default] || Setting.get('api_per_page', '10') + per_page_requested = controller.params[:per_page] || options[:default] || per_page max = options[:max] || max_per_page - [[per_page.to_i, 1].max, max.to_i].min + [[per_page_requested.to_i, 1].max, max.to_i].min end # Add [link HTTP Headers](http://www.w3.org/Protocols/9707-link-header.html) for pagination diff --git a/spec/lib/api_spec.rb b/spec/lib/api_spec.rb index ce2d3a42bd9..120709db1d9 100644 --- a/spec/lib/api_spec.rb +++ b/spec/lib/api_spec.rb @@ -716,7 +716,8 @@ describe Api do let(:collection) { [1, 2, 3] } it "should not raise Folio::InvalidPage for pages past the end" do - expect(Api.paginate(collection, controller, 'example.com', page: collection.size + 1, per_page: 1)). + controller = stub('controller', request: request, response: response, params: {per_page: 1}) + expect(Api.paginate(collection, controller, 'example.com', page: collection.size + 1)). to eq [] end @@ -738,6 +739,42 @@ describe Api do to raise_error(Folio::InvalidPage) end end + + describe "page size limits" do + let(:collection) { (1..101).to_a } + + context "with no max_per_page argument" do + it "should limit to the default max_per_page" do + controller = stub('controller', request: request, response: response, params: {per_page: Api.max_per_page + 5}) + expect(Api.paginate(collection, controller, 'example.com').size). + to eq Api.max_per_page + end + end + + context "with no per_page parameter" do + it "should limit to the default per_page" do + controller = stub('controller', request: request, response: response, params: {}) + expect(Api.paginate(collection, controller, 'example.com').size). + to eq Api.per_page + end + end + + context "with per_page parameter > max_per_page argument" do + let(:controller) { stub('controller', request: request, response: response, params: {per_page: 100}) } + it "should take the smaller of the max_per_page arugment and the per_page param" do + expect(Api.paginate(collection, controller, 'example.com', {max_per_page: 75}).size). + to eq 75 + end + end + + context "with per_page parameter < max_per_page argument" do + let(:controller) { stub('controller', request: request, response: response, params: {per_page: 75}) } + it "should take the smaller of the max_per_page arugment and the per_page param" do + expect(Api.paginate(collection, controller, 'example.com', {max_per_page: 100}).size). + to eq 75 + end + end + end end context ".build_links" do |