add pagination for getting log entries from wikis
This commit is contained in:
@@ -3,6 +3,7 @@ require 'grit'
|
|||||||
require 'github/markup'
|
require 'github/markup'
|
||||||
|
|
||||||
# internal
|
# internal
|
||||||
|
require 'gollum/pagination'
|
||||||
require 'gollum/wiki'
|
require 'gollum/wiki'
|
||||||
require 'gollum/page'
|
require 'gollum/page'
|
||||||
require 'gollum/file'
|
require 'gollum/file'
|
||||||
|
|||||||
+8
-2
@@ -1,5 +1,7 @@
|
|||||||
module Gollum
|
module Gollum
|
||||||
class Page
|
class Page
|
||||||
|
include Pagination
|
||||||
|
|
||||||
VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i
|
VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i
|
||||||
|
|
||||||
# Public: Initialize a page.
|
# Public: Initialize a page.
|
||||||
@@ -73,9 +75,13 @@ module Gollum
|
|||||||
|
|
||||||
# Public: All of the versions that have touched the Page.
|
# 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.
|
# Returns an Array of Grit::Commit.
|
||||||
def versions
|
def versions(options = {})
|
||||||
@wiki.repo.log('master', @path)
|
@wiki.repo.log('master', @path, log_pagination_options(options))
|
||||||
end
|
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
|
module Gollum
|
||||||
class Wiki
|
class Wiki
|
||||||
|
include Pagination
|
||||||
|
|
||||||
# Public: Initialize a new Gollum Repo.
|
# Public: Initialize a new Gollum Repo.
|
||||||
#
|
#
|
||||||
# repo - The String path to the Git repository that holds the Gollum site.
|
# 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)
|
tree_list(@repo.commit(treeish).tree)
|
||||||
end
|
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
|
# Internal Methods
|
||||||
|
|||||||
@@ -13,6 +13,31 @@ context "Wiki" do
|
|||||||
assert_equal Grit::Repo, @wiki.repo.class
|
assert_equal Grit::Repo, @wiki.repo.class
|
||||||
assert @wiki.exist?
|
assert @wiki.exist?
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "Wiki page writing" do
|
context "Wiki page writing" do
|
||||||
|
|||||||
Reference in New Issue
Block a user