add pagination for getting log entries from wikis
This commit is contained in:
@@ -3,6 +3,7 @@ require 'grit'
|
||||
require 'github/markup'
|
||||
|
||||
# internal
|
||||
require 'gollum/pagination'
|
||||
require 'gollum/wiki'
|
||||
require 'gollum/page'
|
||||
require 'gollum/file'
|
||||
|
||||
+8
-2
@@ -1,5 +1,7 @@
|
||||
module Gollum
|
||||
class Page
|
||||
include Pagination
|
||||
|
||||
VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i
|
||||
|
||||
# Public: Initialize a page.
|
||||
@@ -73,9 +75,13 @@ module Gollum
|
||||
|
||||
# Public: All of the versions that have touched the Page.
|
||||
#
|
||||
# options - The options Hash:
|
||||
# :page - The Integer page number (default: 1).
|
||||
# :per_page - The Integer max count of items to return.
|
||||
#
|
||||
# Returns an Array of Grit::Commit.
|
||||
def versions
|
||||
@wiki.repo.log('master', @path)
|
||||
def versions(options = {})
|
||||
@wiki.repo.log('master', @path, log_pagination_options(options))
|
||||
end
|
||||
|
||||
#########################################################################
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
module Gollum
|
||||
module Pagination
|
||||
def self.included(klass)
|
||||
klass.extend ClassMethods
|
||||
class << klass
|
||||
attr_accessor :per_page
|
||||
end
|
||||
klass.per_page = 30
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def page_to_skip(page)
|
||||
([1, page.to_i].max - 1) * per_page
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def page_to_skip(page)
|
||||
self.class.page_to_skip(page)
|
||||
end
|
||||
|
||||
def log_pagination_options(options = {})
|
||||
self.class.log_pagination_options(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,7 @@
|
||||
module Gollum
|
||||
class Wiki
|
||||
include Pagination
|
||||
|
||||
# Public: Initialize a new Gollum Repo.
|
||||
#
|
||||
# repo - The String path to the Git repository that holds the Gollum site.
|
||||
@@ -121,6 +123,17 @@ module Gollum
|
||||
tree_list(@repo.commit(treeish).tree)
|
||||
end
|
||||
|
||||
# Public: All of the versions that have touched the Page.
|
||||
#
|
||||
# options - The options Hash:
|
||||
# :page - The Integer page number (default: 1).
|
||||
# :per_page - The Integer max count of items to return.
|
||||
#
|
||||
# Returns an Array of Grit::Commit.
|
||||
def log(options = {})
|
||||
@repo.log('master', nil, log_pagination_options(options))
|
||||
end
|
||||
|
||||
#########################################################################
|
||||
#
|
||||
# Internal Methods
|
||||
|
||||
@@ -13,6 +13,31 @@ context "Wiki" do
|
||||
assert_equal Grit::Repo, @wiki.repo.class
|
||||
assert @wiki.exist?
|
||||
end
|
||||
|
||||
test "shows paginated log with no page" do
|
||||
Gollum::Wiki.per_page = 3
|
||||
assert_equal %w(
|
||||
f01428b3138994aab19d5f880b6f37336ddf1f24
|
||||
fbabba862dfa7ac35b39042dd4ad780c9f67b8cb
|
||||
df26e61e707116f81ebc6b935ec6d1676b7e96c4),
|
||||
@wiki.log.map { |c| c.id }
|
||||
end
|
||||
|
||||
test "shows paginated log with 1st page" do
|
||||
Gollum::Wiki.per_page = 3
|
||||
assert_equal %w(
|
||||
f01428b3138994aab19d5f880b6f37336ddf1f24
|
||||
fbabba862dfa7ac35b39042dd4ad780c9f67b8cb
|
||||
df26e61e707116f81ebc6b935ec6d1676b7e96c4),
|
||||
@wiki.log(:page => 1).map { |c| c.id }
|
||||
end
|
||||
|
||||
test "shows paginated log with next page" do
|
||||
Gollum::Wiki.per_page = 3
|
||||
assert_equal %w(
|
||||
5bc1aaec6149e854078f1d0f8b71933bbc6c2e43),
|
||||
@wiki.log(:page => 2).map { |c| c.id }
|
||||
end
|
||||
end
|
||||
|
||||
context "Wiki page writing" do
|
||||
|
||||
Reference in New Issue
Block a user