diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/project_wiki.rb | 16 | ||||
-rw-r--r-- | app/models/wiki_page.rb | 19 |
2 files changed, 23 insertions, 12 deletions
diff --git a/app/models/project_wiki.rb b/app/models/project_wiki.rb index 268706a6aea..23ddd708396 100644 --- a/app/models/project_wiki.rb +++ b/app/models/project_wiki.rb @@ -13,6 +13,11 @@ class ProjectWiki CouldNotCreateWikiError = Class.new(StandardError) SIDEBAR = '_sidebar' + TITLE_ORDER = 'title' + CREATED_AT_ORDER = 'created_at' + DIRECTION_DESC = 'desc' + DIRECTION_ASC = 'asc' + # Returns a string describing what went wrong after # an operation fails. attr_reader :error_message @@ -82,8 +87,15 @@ class ProjectWiki # Returns an Array of GitLab WikiPage instances or an # empty Array if this Wiki has no pages. - def pages(limit: 0) - wiki.pages(limit: limit).map { |page| WikiPage.new(self, page, true) } + def pages(limit: 0, sort: nil, direction: DIRECTION_ASC) + sort ||= TITLE_ORDER + direction_desc = direction == DIRECTION_DESC + + wiki.pages( + limit: limit, sort: sort, direction_desc: direction_desc + ).map do |page| + WikiPage.new(self, page, true) + end end # Finds a page within the repository based on a tile diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 64daba81dcf..909da4316d0 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -28,16 +28,15 @@ class WikiPage def self.group_by_directory(pages) return [] if pages.blank? - pages.sort_by { |page| [page.directory, page.slug] } - .group_by(&:directory) - .map do |dir, pages| - if dir.present? - WikiDirectory.new(dir, pages) - else - pages - end - end - .flatten + pages.each_with_object([]) do |page, grouped_pages| + next grouped_pages << page unless page.directory.present? + + directory = grouped_pages.find { |dir| dir.slug == page.directory } + + next directory.pages << page if directory + + grouped_pages << WikiDirectory.new(page.directory, [page]) + end end def self.unhyphenize(name) |