Class: Gitlab::MultiCollectionPaginator
- Inherits:
-
Object
- Object
- Gitlab::MultiCollectionPaginator
- Defined in:
- lib/gitlab/multi_collection_paginator.rb
Instance Attribute Summary collapse
-
#first_collection ⇒ Object
readonly
Returns the value of attribute first_collection.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#second_collection ⇒ Object
readonly
Returns the value of attribute second_collection.
Instance Method Summary collapse
-
#initialize(*collections, per_page: nil) ⇒ MultiCollectionPaginator
constructor
A new instance of MultiCollectionPaginator.
- #paginate(page) ⇒ Object
- #paginated_first_collection(page) ⇒ Object
- #paginated_second_collection(page) ⇒ Object
- #total_count ⇒ Object
Constructor Details
#initialize(*collections, per_page: nil) ⇒ MultiCollectionPaginator
Returns a new instance of MultiCollectionPaginator.
7 8 9 10 11 12 13 14 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 7 def initialize(*collections, per_page: nil) raise ArgumentError, 'Only 2 collections are supported' if collections.size != 2 @per_page = (per_page || Kaminari.config.default_per_page).to_i @first_collection, @second_collection = collections raise ArgumentError, 'Page size must be at least 1' if @per_page < 1 end |
Instance Attribute Details
#first_collection ⇒ Object (readonly)
Returns the value of attribute first_collection.
5 6 7 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 5 def first_collection @first_collection end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
5 6 7 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 5 def per_page @per_page end |
#second_collection ⇒ Object (readonly)
Returns the value of attribute second_collection.
5 6 7 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 5 def second_collection @second_collection end |
Instance Method Details
#paginate(page) ⇒ Object
16 17 18 19 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 16 def paginate(page) page = page.to_i paginated_first_collection(page) + paginated_second_collection(page) end |
#paginated_first_collection(page) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 25 def paginated_first_collection(page) @first_collection_pages ||= Hash.new do |hash, page| hash[page] = first_collection.page(page).per(per_page) end @first_collection_pages[page] end |
#paginated_second_collection(page) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 33 def paginated_second_collection(page) @second_collection_pages ||= Hash.new do |hash, page| second_collection_page = page - first_collection_page_count offset = if second_collection_page < 1 || first_collection_page_count == 0 0 else per_page - first_collection_last_page_size end hash[page] = second_collection.page(second_collection_page) .per(per_page - paginated_first_collection(page).size) .padding(offset) end @second_collection_pages[page] end |
#total_count ⇒ Object
21 22 23 |
# File 'lib/gitlab/multi_collection_paginator.rb', line 21 def total_count @total_count ||= first_collection.size + second_collection.size end |