diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-11-30 17:39:50 +0100 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-12-01 18:41:07 +0100 |
commit | e7990a16ded06f495e0c4aa40f8519ab76e00c5b (patch) | |
tree | 717f0472cf95c4ae5e24edc631c3c3d3e6425ea5 | |
parent | c594659fea15c6dd17b9ea4c6b88c5a418f10ab9 (diff) | |
download | gitlab-ce-pagination-without-count-flag.tar.gz |
Allow disabling of page numbers using Flipperpagination-without-count-flag
This allows page numbers to be disabled globally by enabling the
feature "paginate_without_count". This allows us to test the performance
impact of not having to run COUNT(*) queries, while still being able to
revert easily if necessary.
See https://gitlab.com/gitlab-org/gitlab-ce/issues/36077 for more
information.
-rw-r--r-- | app/helpers/pagination_helper.rb | 21 | ||||
-rw-r--r-- | app/views/dashboard/todos/index.html.haml | 2 | ||||
-rw-r--r-- | app/views/shared/projects/_list.html.haml | 2 | ||||
-rw-r--r-- | config/initializers/kaminari_config.rb | 54 | ||||
-rw-r--r-- | spec/helpers/pagination_helper_spec.rb | 23 |
5 files changed, 56 insertions, 46 deletions
diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb deleted file mode 100644 index 83dd76a01dd..00000000000 --- a/app/helpers/pagination_helper.rb +++ /dev/null @@ -1,21 +0,0 @@ -module PaginationHelper - def paginate_collection(collection, remote: nil) - if collection.is_a?(Kaminari::PaginatableWithoutCount) - paginate_without_count(collection) - elsif collection.respond_to?(:total_pages) - paginate_with_count(collection, remote: remote) - end - end - - def paginate_without_count(collection) - render( - 'kaminari/gitlab/without_count', - previous_path: path_to_prev_page(collection), - next_path: path_to_next_page(collection) - ) - end - - def paginate_with_count(collection, remote: nil) - paginate(collection, remote: remote, theme: 'gitlab') - end -end diff --git a/app/views/dashboard/todos/index.html.haml b/app/views/dashboard/todos/index.html.haml index a5686002328..4c4e9dabdaa 100644 --- a/app/views/dashboard/todos/index.html.haml +++ b/app/views/dashboard/todos/index.html.haml @@ -72,7 +72,7 @@ .js-todos-all - if @todos.any? .js-todos-list-container - .js-todos-options{ data: { per_page: @todos.limit_value, current_page: @todos.current_page, total_pages: @todos.total_pages } } + .js-todos-options{ data: { per_page: @todos.limit_value, current_page: @todos.current_page } } .panel.panel-default.panel-without-border.panel-without-margin %ul.content-list.todos-list = render @todos diff --git a/app/views/shared/projects/_list.html.haml b/app/views/shared/projects/_list.html.haml index 0bedfea3502..109c99b0dda 100644 --- a/app/views/shared/projects/_list.html.haml +++ b/app/views/shared/projects/_list.html.haml @@ -23,6 +23,6 @@ = icon('lock fw', base: 'circle', class: 'fa-lg private-fork-icon') %strong= pluralize(@private_forks_count, 'private fork') %span you have no access to. - = paginate_collection(projects, remote: remote) + = paginate(projects, remote: remote) - else .nothing-here-block No projects found diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb index 3cbe9a058d7..b6e7d07ea2e 100644 --- a/config/initializers/kaminari_config.rb +++ b/config/initializers/kaminari_config.rb @@ -1,6 +1,7 @@ Kaminari.configure do |config| config.default_per_page = 20 config.max_per_page = 100 + config.page_method_name = :page_with_count # config.window = 4 # config.outer_window = 0 # config.left = 0 @@ -8,3 +9,56 @@ Kaminari.configure do |config| # config.page_method_name = :page # config.param_name = :page end + +ar_module = Module.new do + def page(*args) + relation = page_with_count(*args) + + if Feature.enabled?(:paginate_without_count) + relation.without_count + else + relation + end + end +end + +av_module = Module.new do + def paginate(collection, options = {}) + options[:theme] ||= 'gitlab' + + if collection.is_a?(Kaminari::PaginatableWithoutCount) + paginate_without_count(collection, param_name: options[:param_name]) + elsif collection.respond_to?(:total_pages) + super(collection, options) + end + end + + def paginate_without_count(collection, param_name: nil) + render( + 'kaminari/gitlab/without_count', + previous_path: path_to_prev_page(collection, param_name: param_name), + next_path: path_to_next_page(collection, param_name: param_name) + ) + end +end + +ActiveSupport.on_load :active_record do + ActiveRecord::Base.singleton_class.prepend(ar_module) + + # When paginating without a COUNT(*) we still want to allow one to explicitly + # request the total number of pages. + module Kaminari::PaginatableWithoutCount + remove_method :total_count + + total_count = + Kaminari::ActiveRecordRelationMethods.instance_method(:total_count) + + total_count.bind(self) + end +end + +ActiveSupport.on_load :action_view do + # We can not use regular helpers for this because Kaminari is loaded after + # them, thus it would overwrite our custom "paginate" method. + ActionView::Base.prepend(av_module) +end diff --git a/spec/helpers/pagination_helper_spec.rb b/spec/helpers/pagination_helper_spec.rb deleted file mode 100644 index e235475fb47..00000000000 --- a/spec/helpers/pagination_helper_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'spec_helper' - -describe PaginationHelper do - describe '#paginate_collection' do - let(:collection) { User.all.page(1) } - - it 'paginates a collection without using a COUNT' do - without_count = collection.without_count - - expect(helper).to receive(:paginate_without_count) - .with(without_count) - .and_call_original - - helper.paginate_collection(without_count) - end - - it 'paginates a collection using a COUNT' do - expect(helper).to receive(:paginate_with_count).and_call_original - - helper.paginate_collection(collection) - end - end -end |