2018-08-30 07:05:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-30 15:28:59 -04:00
|
|
|
# We're patching `ActiveSupport::Concern` in
|
|
|
|
# config/initializers/0_as_concern.rb
|
|
|
|
#
|
|
|
|
# We want to patch `ActiveSupport::Concern` for two reasons:
|
|
|
|
# 1. Allow defining class methods via: `class_methods` method
|
|
|
|
# 2. Allow `prepended do; end` work like `included do; end`
|
|
|
|
# If we don't need anything above, we don't need this patch nor the concern!
|
|
|
|
|
2018-08-30 07:05:00 -04:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
module Gitlab
|
|
|
|
module Patch
|
|
|
|
module Prependable
|
|
|
|
class MultiplePrependedBlocks < StandardError
|
|
|
|
def initialize
|
|
|
|
super "Cannot define multiple 'prepended' blocks for a Concern"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
def prepend_features(base)
|
|
|
|
return false if prepended?(base)
|
2018-08-30 07:05:00 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
if const_defined?(:ClassMethods)
|
2019-10-15 23:06:12 -04:00
|
|
|
klass_methods = const_get(:ClassMethods, false)
|
2018-08-30 12:25:17 -04:00
|
|
|
base.singleton_class.prepend klass_methods
|
|
|
|
base.instance_variable_set(:@_prepended_class_methods, klass_methods)
|
|
|
|
end
|
2018-08-30 07:05:00 -04:00
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
if instance_variable_defined?(:@_prepended_block)
|
|
|
|
base.class_eval(&@_prepended_block)
|
|
|
|
end
|
2018-08-30 07:05:00 -04:00
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
true
|
|
|
|
end
|
2018-08-30 07:05:00 -04:00
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
def class_methods
|
|
|
|
super
|
2018-08-30 07:05:00 -04:00
|
|
|
|
2021-01-29 13:09:17 -05:00
|
|
|
class_methods_module = const_get(:ClassMethods, false)
|
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
if instance_variable_defined?(:@_prepended_class_methods)
|
2021-01-29 13:09:17 -05:00
|
|
|
class_methods_module.prepend @_prepended_class_methods
|
2018-08-30 07:05:00 -04:00
|
|
|
end
|
2021-01-29 13:09:17 -05:00
|
|
|
|
|
|
|
# Hack to resolve https://gitlab.com/gitlab-org/gitlab/-/issues/23932
|
|
|
|
extend class_methods_module if ENV['STATIC_VERIFICATION']
|
2018-08-30 07:05:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepended(base = nil, &block)
|
|
|
|
if base.nil?
|
|
|
|
raise MultiplePrependedBlocks if
|
|
|
|
instance_variable_defined?(:@_prepended_block)
|
|
|
|
|
|
|
|
@_prepended_block = block
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepended?(base)
|
|
|
|
index = base.ancestors.index(base)
|
|
|
|
|
2018-08-30 12:25:17 -04:00
|
|
|
base.ancestors[0...index].index(self)
|
2018-08-30 07:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|