Welcome to mirror list, hosted at ThFree Co, Russian Federation.

wiki_directory.rb « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c86c11e7fbea0d37b0988cf0e8c381141c5be26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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

  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