diff --git a/lib/gollum.rb b/lib/gollum.rb index 449f619e..77064832 100644 --- a/lib/gollum.rb +++ b/lib/gollum.rb @@ -3,6 +3,7 @@ require 'grit' require 'github/markup' # internal +require 'gollum/pagination' require 'gollum/wiki' require 'gollum/page' require 'gollum/file' diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 7694606c..295826f3 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -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 ######################################################################### diff --git a/lib/gollum/pagination.rb b/lib/gollum/pagination.rb new file mode 100644 index 00000000..8c274142 --- /dev/null +++ b/lib/gollum/pagination.rb @@ -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 \ No newline at end of file diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index fd3737c5..639d31a4 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -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 diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 17743f56..8d3ceded 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -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