diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-02-13 17:04:15 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-02-13 17:04:15 +0000 |
commit | 018a7c6a550494a7dd386450b464d5692413f319 (patch) | |
tree | 65c10ca3f5e627b25fa4a2b10e76a7504628d69c /app/models/wiki_page.rb | |
parent | 79e8e6134f89c4a09a422ca128a7579d844f040c (diff) | |
parent | fe5a753be903344a6cc1dc8f1e023b62887e920b (diff) | |
download | gitlab-ce-018a7c6a550494a7dd386450b464d5692413f319.tar.gz |
Merge branch '23535-folders-in-wiki-repository' into 'master'
Show directory hierarchy when listing wiki pages
Closes #23535
See merge request !8133
Diffstat (limited to 'app/models/wiki_page.rb')
-rw-r--r-- | app/models/wiki_page.rb | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index e93d0eab6d8..6347b274341 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -12,6 +12,32 @@ class WikiPage ActiveModel::Name.new(self, nil, 'wiki') end + # Sorts and groups pages by directory. + # + # pages - an array of WikiPage objects. + # + # Returns an array of WikiPage and WikiDirectory objects. The entries are + # sorted by alphabetical order (directories and pages inside each directory). + # Pages at the root level come before everything. + 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 + end + + def self.unhyphenize(name) + name.gsub(/-+/, ' ') + end + def to_key [:slug] end @@ -56,7 +82,7 @@ class WikiPage # The formatted title of this page. def title if @attributes[:title] - @attributes[:title].gsub(/-+/, ' ') + self.class.unhyphenize(@attributes[:title]) else "" end @@ -72,6 +98,11 @@ class WikiPage @attributes[:content] ||= @page&.text_data end + # The hierarchy of the directory this page is contained in. + def directory + wiki.page_title_and_dir(slug).last + end + # The processed/formatted content of this page. def formatted_content @attributes[:formatted_content] ||= @page&.formatted_data @@ -170,6 +201,12 @@ class WikiPage end end + # Relative path to the partial to be used when rendering collections + # of this object. + def to_partial_path + 'projects/wikis/wiki_page' + end + private def set_attributes |