2019-07-23 10:51:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-01 08:41:37 -04:00
|
|
|
require 'active_support/inflector'
|
|
|
|
|
2019-07-23 10:51:13 -04:00
|
|
|
module InjectEnterpriseEditionModule
|
2021-05-11 17:10:21 -04:00
|
|
|
def prepend_mod_with(constant_name, namespace: Object, with_descendants: false)
|
|
|
|
each_extension_for(constant_name, namespace) do |constant|
|
|
|
|
prepend_module(constant, with_descendants)
|
|
|
|
end
|
2019-07-23 10:51:13 -04:00
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
def extend_mod_with(constant_name, namespace: Object)
|
2021-05-05 08:10:33 -04:00
|
|
|
each_extension_for(
|
2021-05-11 17:10:21 -04:00
|
|
|
constant_name,
|
2021-05-05 08:10:33 -04:00
|
|
|
namespace,
|
|
|
|
&method(:extend))
|
2019-07-23 10:51:13 -04:00
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
def include_mod_with(constant_name, namespace: Object)
|
2021-05-05 08:10:33 -04:00
|
|
|
each_extension_for(
|
2021-05-11 17:10:21 -04:00
|
|
|
constant_name,
|
2021-05-05 08:10:33 -04:00
|
|
|
namespace,
|
|
|
|
&method(:include))
|
2019-07-23 10:51:13 -04:00
|
|
|
end
|
2021-01-26 07:09:27 -05:00
|
|
|
|
2021-05-05 08:10:33 -04:00
|
|
|
def prepend_mod(with_descendants: false)
|
2021-05-11 17:10:21 -04:00
|
|
|
prepend_mod_with(name, with_descendants: with_descendants) # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2021-01-26 07:09:27 -05:00
|
|
|
end
|
|
|
|
|
2021-05-05 08:10:33 -04:00
|
|
|
def extend_mod
|
2021-05-11 17:10:21 -04:00
|
|
|
extend_mod_with(name) # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2021-01-26 07:09:27 -05:00
|
|
|
end
|
|
|
|
|
2021-05-05 08:10:33 -04:00
|
|
|
def include_mod
|
2021-05-11 17:10:21 -04:00
|
|
|
include_mod_with(name) # rubocop: disable Cop/InjectEnterpriseEditionModule
|
2021-01-26 07:09:27 -05:00
|
|
|
end
|
|
|
|
|
2021-05-05 08:10:33 -04:00
|
|
|
private
|
2021-04-13 14:11:28 -04:00
|
|
|
|
2021-01-26 07:09:27 -05:00
|
|
|
def prepend_module(mod, with_descendants)
|
|
|
|
prepend(mod)
|
|
|
|
|
|
|
|
if with_descendants
|
|
|
|
descendants.each { |descendant| descendant.prepend(mod) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-05 08:10:33 -04:00
|
|
|
def each_extension_for(constant_name, namespace)
|
|
|
|
Gitlab.extensions.each do |extension_name|
|
|
|
|
extension_namespace =
|
|
|
|
const_get_maybe_false(namespace, extension_name.upcase)
|
|
|
|
|
|
|
|
extension_module =
|
|
|
|
const_get_maybe_false(extension_namespace, constant_name)
|
|
|
|
|
|
|
|
yield(extension_module) if extension_module
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def const_get_maybe_false(mod, name)
|
|
|
|
# We're still heavily relying on Rails autoloading instead of zeitwerk,
|
|
|
|
# therefore this check: `mod.const_defined?(name, false)`
|
|
|
|
# Is not reliable, which may return false while it's defined.
|
|
|
|
# After we moved everything over to zeitwerk we can avoid rescuing
|
|
|
|
# NameError and just check if const_defined?
|
|
|
|
# mod && mod.const_defined?(name, false) && mod.const_get(name, false)
|
|
|
|
mod && mod.const_get(name, false)
|
|
|
|
rescue NameError
|
|
|
|
false
|
|
|
|
end
|
2019-07-23 10:51:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Module.prepend(InjectEnterpriseEditionModule)
|