This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
forked from gollum/gollum
/
pagination.rb
61 lines (57 loc) · 1.87 KB
/
pagination.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Gollum
module Pagination
def self.included(klass)
klass.extend ClassMethods
class << klass
# Default Integer max count of items to return in git commands.
attr_accessor :per_page
end
klass.per_page = 30
end
module ClassMethods
# Turns a page number into an offset number for the git skip option.
#
# page - Integer page number.
#
# Returns an Integer.
def page_to_skip(page)
([1, page.to_i].max - 1) * per_page
end
# Fills in git-specific options for the log command using simple
# pagination options.
#
# options - Hash of options:
# page - Optional Integer page number (default: 1)
# per_page - Optional Integer max count of items to return.
# Defaults to #per_class class method.
#
# Returns Hash with :max_count and :skip keys.
def log_pagination_options(options = {})
skip = page_to_skip(options.delete(:page))
options[:max_count] = [options.delete(:per_page).to_i, per_page].max
options[:skip] = skip if skip > 0
options
end
end
# Turns a page number into an offset number for the git skip option.
#
# page - Integer page number.
#
# Returns an Integer.
def page_to_skip(page)
self.class.page_to_skip(page)
end
# Fills in git-specific options for the log command using simple
# pagination options.
#
# options - Hash of options:
# page - Optional Integer page number (default: 1)
# per_page - Optional Integer max count of items to return.
# Defaults to #per_class class method.
#
# Returns Hash with :max_count and :skip keys.
def log_pagination_options(options = {})
self.class.log_pagination_options(options)
end
end
end