debian-mirror-gitlab/app/finders/license_template_finder.rb

60 lines
1.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-11-20 20:47:30 +05:30
# LicenseTemplateFinder
#
# Used to find license templates, which may come from a variety of external
# sources
#
2018-12-05 23:21:45 +05:30
# Params can be any of the following:
2018-11-20 20:47:30 +05:30
# popular: boolean. When set to true, only "popular" licenses are shown. When
# false, all licenses except popular ones are shown. When nil (the
# default), *all* licenses will be shown.
2018-12-05 23:21:45 +05:30
# name: string. If set, return a single license matching that name (or nil)
2018-11-20 20:47:30 +05:30
class LicenseTemplateFinder
2018-12-05 23:21:45 +05:30
include Gitlab::Utils::StrongMemoize
attr_reader :project, :params
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
def initialize(project, params = {})
@project = project
2018-11-20 20:47:30 +05:30
@params = params
end
def execute
2018-12-05 23:21:45 +05:30
if params[:name]
vendored_licenses.find { |template| template.key == params[:name] }
else
vendored_licenses
2018-11-20 20:47:30 +05:30
end
end
2021-03-11 19:13:27 +05:30
def template_names
::Gitlab::Template::BaseTemplate.template_names_by_category(vendored_licenses)
end
2018-11-20 20:47:30 +05:30
private
2018-12-05 23:21:45 +05:30
def vendored_licenses
strong_memoize(:vendored_licenses) do
Licensee::License.all(featured: popular_only?).map do |license|
LicenseTemplate.new(
key: license.key,
name: license.name,
2021-03-11 19:13:27 +05:30
project: project,
2018-12-05 23:21:45 +05:30
nickname: license.nickname,
category: (license.featured? ? :Popular : :Other),
content: license.content,
url: license.url,
meta: license.meta
)
end
end
end
2018-11-20 20:47:30 +05:30
def popular_only?
params.fetch(:popular, nil)
end
end
2020-04-22 19:07:51 +05:30
2021-06-08 01:23:25 +05:30
LicenseTemplateFinder.prepend_mod_with('LicenseTemplateFinder')