Class: Gitlab::Git::BlamePagination

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/git/blame_pagination.rb

Constant Summary collapse

PAGINATION_PER_PAGE =
1000
STREAMING_FIRST_PAGE_SIZE =
200
STREAMING_PER_PAGE =
2000

Instance Method Summary collapse

Constructor Details

#initialize(blob, blame_mode, params) ⇒ BlamePagination

Returns a new instance of BlamePagination.



12
13
14
15
16
# File 'lib/gitlab/git/blame_pagination.rb', line 12

def initialize(blob, blame_mode, params)
  @blob = blob
  @blame_mode = blame_mode
  @params = params
end

Instance Method Details

#blame_rangeObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitlab/git/blame_pagination.rb', line 53

def blame_range
  return if blame_mode.full?

  first_line = ((page - 1) * per_page) + 1

  if blame_mode.streaming?
    return 1..STREAMING_FIRST_PAGE_SIZE if page == 1

    first_line = STREAMING_FIRST_PAGE_SIZE + ((page - 2) * per_page) + 1
  end

  last_line = (first_line + per_page).to_i - 1

  first_line..last_line
end

#pageObject



18
19
20
21
22
23
24
# File 'lib/gitlab/git/blame_pagination.rb', line 18

def page
  page = params.fetch(:page, 1).to_i

  return 1 if page < 1

  page
end

#paginatorObject



45
46
47
48
49
50
51
# File 'lib/gitlab/git/blame_pagination.rb', line 45

def paginator
  return if blame_mode.streaming? || blame_mode.full?

  Kaminari.paginate_array([], total_count: blob_lines_count, limit: per_page)
    .tap { |pagination| pagination.max_paginates_per(per_page) }
    .page(page)
end

#per_pageObject



27
28
29
# File 'lib/gitlab/git/blame_pagination.rb', line 27

def per_page
  blame_mode.streaming? ? STREAMING_PER_PAGE : PAGINATION_PER_PAGE
end

#total_extra_pagesObject



40
41
42
# File 'lib/gitlab/git/blame_pagination.rb', line 40

def total_extra_pages
  [total_pages - 1, 0].max
end

#total_pagesObject



32
33
34
35
36
37
# File 'lib/gitlab/git/blame_pagination.rb', line 32

def total_pages
  total = (blob_lines_count.to_f / per_page).ceil
  return total unless blame_mode.streaming?

  ([blob_lines_count - STREAMING_FIRST_PAGE_SIZE, 0].max.to_f / per_page).ceil + 1
end