Commit f69c6a72 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'improve_the_performance_of_prepend_if_ee' into 'master'

[RUN-AS-IF-FOSS] Extend the interface of (prepend|extend|include)_if_ee helper methods

See merge request gitlab-org/gitlab!51677
parents 2fc8048c aeaf4e52
......@@ -556,4 +556,4 @@ class ApplicationController < ActionController::Base
end
end
ApplicationController.prepend_if_ee('EE::ApplicationController')
ApplicationController.prepend_ee_mod
......@@ -25,4 +25,4 @@ class Vulnerability < ApplicationRecord
end
end
Vulnerability.prepend_if_ee('EE::Vulnerability')
Vulnerability.prepend_ee_mod
......@@ -6,12 +6,7 @@ module InjectEnterpriseEditionModule
def prepend_if_ee(constant, with_descendants: false)
return unless Gitlab.ee?
ee_module = constant.constantize
prepend(ee_module)
if with_descendants
descendants.each { |descendant| descendant.prepend(ee_module) }
end
prepend_module(constant.constantize, with_descendants)
end
def extend_if_ee(constant)
......@@ -21,6 +16,34 @@ module InjectEnterpriseEditionModule
def include_if_ee(constant)
include(constant.constantize) if Gitlab.ee?
end
def prepend_ee_mod(with_descendants: false)
return unless Gitlab.ee?
prepend_module(ee_module, with_descendants)
end
def extend_ee_mod
extend(ee_module) if Gitlab.ee?
end
def include_ee_mod
include(ee_module) if Gitlab.ee?
end
private
def prepend_module(mod, with_descendants)
prepend(mod)
if with_descendants
descendants.each { |descendant| descendant.prepend(mod) }
end
end
def ee_module
::EE.const_get(name, false)
end
end
Module.prepend(InjectEnterpriseEditionModule)
......@@ -92,12 +92,36 @@ class User < ActiveRecord::Base
# ... lots of code here ...
end
User.prepend_if_ee('EE::User')
User.prepend_ee_mod
```
Do not use methods such as `prepend`, `extend`, and `include`. Instead, use
`prepend_if_ee`, `extend_if_ee`, or `include_if_ee`. These methods take a
_String_ containing the full module name as the argument, not the module itself.
`prepend_ee_mod`, `extend_ee_mod`, or `include_ee_mod`. These methods will try to
find the relevant EE module by the name of the receiver module, for example;
```ruby
module Vulnerabilities
class Finding
#...
end
end
Vulnerabilities::Finding.prepend_ee_mod
```
will prepend the module named `::EE::Vulnerabilities::Finding`.
If the extending module does not follow this naming convention, you can also provide the module name
by using `prepend_if_ee`, `extend_if_ee`, or `include_if_ee`. These methods take a
_String_ containing the full module name as the argument, not the module itself, like so;
```ruby
class User
#...
end
User.prepend_if_ee('::EE::UserExtension')
```
Since the module would require an `EE` namespace, the file should also be
put in an `ee/` sub-directory. For example, we want to extend the user model
......
......@@ -313,4 +313,4 @@ module API
end
end
API::API.prepend_if_ee('::EE::API::API')
API::API.prepend_ee_mod
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment