Class: Gitlab::Template::BaseTemplate
- Inherits:
-
Object
- Object
- Gitlab::Template::BaseTemplate
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
13
|
# File 'lib/gitlab/template/base_template.rb', line 8
def initialize(path, project = nil, category: nil)
@path = path
@category = category
@project = project
@finder = self.class.finder(project)
end
|
Instance Attribute Details
#category ⇒ Object
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
57
58
59
60
61
62
63
|
# File 'lib/gitlab/template/base_template.rb', line 57
def all(project = nil)
if categories.any?
categories.keys.flat_map { |cat| by_category(cat, project) }
else
by_category("", project)
end
end
|
.base_dir ⇒ Object
81
82
83
|
# File 'lib/gitlab/template/base_template.rb', line 81
def base_dir
raise NotImplementedError
end
|
.by_category(category, project = nil, empty_category_title: nil) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/gitlab/template/base_template.rb', line 92
def by_category(category, project = nil, empty_category_title: nil)
directory = category_directory(category)
files = finder(project).list_files_for(directory)
files.map { |f| new(f, project, category: category.presence || empty_category_title) }.sort
end
|
.categories ⇒ Object
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
73
74
75
|
# File 'lib/gitlab/template/base_template.rb', line 73
def categories
{}
end
|
.category_directory(category) ⇒ Object
99
100
101
102
103
|
# File 'lib/gitlab/template/base_template.rb', line 99
def category_directory(category)
return base_dir unless category.present?
File.join(base_dir, categories[category])
end
|
.extension ⇒ Object
77
78
79
|
# File 'lib/gitlab/template/base_template.rb', line 77
def extension
raise NotImplementedError
end
|
.find(key, project = nil) ⇒ Object
65
66
67
68
|
# File 'lib/gitlab/template/base_template.rb', line 65
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 per project GlobalTemplateFinder - Finds templates on gitlab installation source, templates can be used in all projects
88
89
90
|
# File 'lib/gitlab/template/base_template.rb', line 88
def finder(project = nil)
raise NotImplementedError
end
|
.repository_template_names(project) ⇒ Object
Also known as:
template_names
‘repository_template_names` - reads through Gitaly the actual templates names within a given project’s repository. This is only used by issue and merge request templates, that need to call this once and then cache the returned value.
‘template_names` - is an alias to `repository_template_names`. It would read through Gitaly the actual template names within a given project’s repository for all file templates other than ‘issue` and `merge request` description templates, which would instead overwrite the `template_names` method to return a redis cached version, by reading cached values from `repository.issue_template_names_hash` and `repository.merge_request_template_names_hash` methods.
115
116
117
|
# File 'lib/gitlab/template/base_template.rb', line 115
def repository_template_names(project)
template_names_by_category(self.all(project))
end
|
.template_names_by_category(items) ⇒ Object
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/gitlab/template/base_template.rb', line 120
def template_names_by_category(items)
grouped = items.group_by(&:category)
categories = grouped.keys
categories.index_with do |category|
grouped[category].map do |item|
{ name: item.name, id: item.key, key: item.key, project_id: item.try(:project_id) }
end
end
end
|
.template_subsets(project = nil) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/gitlab/template/base_template.rb', line 131
def template_subsets(project = nil)
return [] if project && !project.repository.exists?
if categories.any?
categories.keys.to_h do |category|
files = self.by_category(category, project)
[category, files.map { |t| { key: t.key, name: t.name, content: t.content } }]
end
else
files = self.all(project)
files.map { |t| { key: t.key, name: t.name, content: t.content } }
end
end
|
Instance Method Details
#<=>(other) ⇒ Object
52
53
54
|
# File 'lib/gitlab/template/base_template.rb', line 52
def <=>(other)
name <=> other.name
end
|
#content ⇒ Object
26
27
28
29
|
# File 'lib/gitlab/template/base_template.rb', line 26
def content
blob = @finder.read(@path)
[description, blob].compact.join("\n")
end
|
#description ⇒ Object
31
32
33
|
# File 'lib/gitlab/template/base_template.rb', line 31
def description
end
|
#full_name ⇒ Object
20
21
22
23
24
|
# File 'lib/gitlab/template/base_template.rb', line 20
def full_name
Pathname.new(@path)
.relative_path_from(self.class.base_dir)
.to_s
end
|
#name ⇒ Object
Also known as:
key
15
16
17
|
# File 'lib/gitlab/template/base_template.rb', line 15
def name
File.basename(@path, self.class.extension)
end
|
#project_id ⇒ Object
35
36
37
|
# File 'lib/gitlab/template/base_template.rb', line 35
def project_id
@project&.id
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
42
43
44
|
# File 'lib/gitlab/template/base_template.rb', line 42
def resolve!(_placeholders = {})
self
end
|
#to_json ⇒ Object
Also known as:
as_json
46
47
48
|
# File 'lib/gitlab/template/base_template.rb', line 46
def to_json(*)
{ key: key, name: name, content: content }
end
|