diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-23 03:06:01 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-23 03:06:01 +0000 |
commit | 8c7eab92cd0009f55cb999bbade43e0f969c137e (patch) | |
tree | 180cac6632448a211ddbe555191574c98e8dc385 /app/models/wiki_directory.rb | |
parent | dffeff5520e861dc6e7319b690c573186bbbd22e (diff) | |
download | gitlab-ce-8c7eab92cd0009f55cb999bbade43e0f969c137e.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/wiki_directory.rb')
-rw-r--r-- | app/models/wiki_directory.rb | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/app/models/wiki_directory.rb b/app/models/wiki_directory.rb index 712ba79bbd2..6c86c11e7fb 100644 --- a/app/models/wiki_directory.rb +++ b/app/models/wiki_directory.rb @@ -1,20 +1,75 @@ # frozen_string_literal: true class WikiDirectory + include StaticModel include ActiveModel::Validations attr_accessor :slug, :pages validates :slug, presence: true + # StaticModel overrides and configuration: + + def self.primary_key + 'slug' + end + + def id + "#{slug}@#{last_version&.sha}" + end + + def self.model_name + ActiveModel::Name.new(self, nil, 'wiki_dir') + end + + alias_method :to_param, :slug + alias_method :title, :slug + + # Sorts and groups pages by directory. + # + # pages - an array of WikiPage objects. + # + # Returns an array of WikiPage and WikiDirectory objects. + # The entries are sorted in the order of the input array, where + # directories appear in the position of their first member. + def self.group_by_directory(pages) + grouped = [] + dirs = Hash.new do |h, k| + new(k).tap { |dir| grouped << (h[k] = dir) } + end + + Array.wrap(pages).each_with_object(grouped) do |page, top_level| + group = page.directory.present? ? dirs[page.directory] : top_level + + group << page + end + end + def initialize(slug, pages = []) @slug = slug @pages = pages end - # Relative path to the partial to be used when rendering collections - # of this object. - def to_partial_path - 'projects/wikis/wiki_directory' + def <<(page) + @pages << page + @last_version = nil + end + + def last_version + @last_version ||= @pages.map(&:last_version).max_by(&:authored_date) + end + + def page_count + @pages.size + end + + def empty? + page_count.zero? + end + + def to_partial_path(context = nil) + name = [context, 'wiki_directory'].compact.join('_') + + "projects/wiki_directories/#{name}" end end |