Skip to content

Move `prepend` calls outside of classes to the last line in the file

In https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8153/ and https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8047 we moved the prepend line of the affected models to the last line, outside of the class block. This was necessary to ensure all code was defined that the prepended modules may want to override, as otherwise various specs would fail.

This approach has two main benefits:

  1. When we prepend the module, the target class has been fully defined, instead of only partially. This means we don't have to place the prepend in a specific place if we want to depend on say constants in the class.
  2. It reduces the chance of merge conflicts, because the prepend now comes after all the CE code

This was discussed in https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8153#note_113239380, and I believe we should start applying this to all existing cases to ensure the codebase is consistently using this pattern.

TODO

To do this, you can use the following script: https://gitlab.com/gitlab-org/release/framework/snippets/1773219

To use:

  1. Change Dir['app/models/**/*.rb'].each do |path| to use the appropriate path (e.g. app/services/**/*.rb)
  2. Run it using ruby move_prepend.rb in the EE source directory

cc @godfat @rspeicher @rymai

Edited by Yorick Peterse