Class: Gitlab::Template::BaseTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/template/base_template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, project = nil, category: nil) ⇒ BaseTemplate

Returns a new instance of BaseTemplate.


8
9
10
11
12
# File 'lib/gitlab/template/base_template.rb', line 8

def initialize(path, project = nil, category: nil)
  @path = path
  @category = category
  @finder = self.class.finder(project)
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category


6
7
8
# File 'lib/gitlab/template/base_template.rb', line 6

def category
  @category
end

Class Method Details

.all(project = nil) ⇒ Object


45
46
47
48
49
50
51
# File 'lib/gitlab/template/base_template.rb', line 45

def all(project = nil)
  if categories.any?
    categories.keys.flat_map { |cat| by_category(cat, project) }
  else
    by_category("", project)
  end
end

.base_dirObject

Raises:

  • (NotImplementedError)

69
70
71
# File 'lib/gitlab/template/base_template.rb', line 69

def base_dir
  raise NotImplementedError
end

.by_category(category, project = nil) ⇒ Object


80
81
82
83
84
85
# File 'lib/gitlab/template/base_template.rb', line 80

def by_category(category, project = nil)
  directory = category_directory(category)
  files = finder(project).list_files_for(directory)

  files.map { |f| new(f, project, category: category) }.sort
end

.categoriesObject

Set categories as sub directories Example: { “category_name_1” => “directory_path_1”, “category_name_2” => “directory_name_2” } Default is no category with all files in base dir of each class


61
62
63
# File 'lib/gitlab/template/base_template.rb', line 61

def categories
  {}
end

.category_directory(category) ⇒ Object


87
88
89
90
91
# File 'lib/gitlab/template/base_template.rb', line 87

def category_directory(category)
  return base_dir unless category.present?

  File.join(base_dir, categories[category])
end

If template is organized by category it returns { category_name: [{ name: template_name }, { name: template2_name }] } If no category is present returns [{ name: template_name }, { name: template2_name}]


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gitlab/template/base_template.rb', line 95

def dropdown_names(project = nil)
  return [] if project && !project.repository.exists?

  if categories.any?
    categories.keys.map do |category|
      files = self.by_category(category, project)
      [category, files.map { |t| { name: t.name } }]
    end.to_h
  else
    files = self.all(project)
    files.map { |t| { name: t.name } }
  end
end

.extensionObject

Raises:

  • (NotImplementedError)

65
66
67
# File 'lib/gitlab/template/base_template.rb', line 65

def extension
  raise NotImplementedError
end

.find(key, project = nil) ⇒ Object


53
54
55
56
# File 'lib/gitlab/template/base_template.rb', line 53

def find(key, project = nil)
  path = self.finder(project).find(key)
  path.present? ? new(path, project) : nil
end

.finder(project = nil) ⇒ Object

Defines which strategy will be used to get templates files RepoTemplateFinder - Finds templates on project repository, templates are filtered perproject GlobalTemplateFinder - Finds templates on gitlab installation source, templates can be used in all projects

Raises:

  • (NotImplementedError)

76
77
78
# File 'lib/gitlab/template/base_template.rb', line 76

def finder(project = nil)
  raise NotImplementedError
end

Instance Method Details

#<=>(other) ⇒ Object


40
41
42
# File 'lib/gitlab/template/base_template.rb', line 40

def <=>(other)
  name <=> other.name
end

#contentObject


25
26
27
# File 'lib/gitlab/template/base_template.rb', line 25

def content
  @finder.read(@path)
end

#full_nameObject


19
20
21
22
23
# File 'lib/gitlab/template/base_template.rb', line 19

def full_name
  Pathname.new(@path)
    .relative_path_from(self.class.base_dir)
    .to_s
end

#nameObject Also known as: key


14
15
16
# File 'lib/gitlab/template/base_template.rb', line 14

def name
  File.basename(@path, self.class.extension)
end

#resolve!(_placeholders = {}) ⇒ Object

Present for compatibility with license templates, which can replace text like `[fullname]` with a user-specified string. This is a no-op for other templates


32
33
34
# File 'lib/gitlab/template/base_template.rb', line 32

def resolve!(_placeholders = {})
  self
end

#to_jsonObject


36
37
38
# File 'lib/gitlab/template/base_template.rb', line 36

def to_json(*)
  { key: key, name: name, content: content }
end