debian-mirror-gitlab/lib/gitlab/patch/prependable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
# 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!
# 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
def prepend_features(base)
return false if prepended?(base)
2021-06-08 01:23:25 +05:30
# Rails 6.1 allows prepending of the modules, but it doesn't
# work well when both modules extend ActiveSupport::Concern
# https://github.com/rails/rails/pull/42067
#
# Let's keep our own implementation, until the issue is fixed
2023-03-04 22:38:38 +05:30
Module.instance_method(:prepend_features).bind_call(self, base)
2018-12-05 23:21:45 +05:30
if const_defined?(:ClassMethods)
2019-12-21 20:55:43 +05:30
klass_methods = const_get(:ClassMethods, false)
2018-12-05 23:21:45 +05:30
base.singleton_class.prepend klass_methods
base.instance_variable_set(:@_prepended_class_methods, klass_methods)
end
if instance_variable_defined?(:@_prepended_block)
base.class_eval(&@_prepended_block)
end
true
end
def class_methods
super
2021-03-11 19:13:27 +05:30
class_methods_module = const_get(:ClassMethods, false)
2018-12-05 23:21:45 +05:30
if instance_variable_defined?(:@_prepended_class_methods)
2021-03-11 19:13:27 +05:30
class_methods_module.prepend @_prepended_class_methods
2018-12-05 23:21:45 +05:30
end
2021-03-11 19:13:27 +05:30
# Hack to resolve https://gitlab.com/gitlab-org/gitlab/-/issues/23932
extend class_methods_module if ENV['STATIC_VERIFICATION']
2018-12-05 23:21:45 +05:30
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)
base.ancestors[0...index].index(self)
end
end
end
end