lograge icon indicating copy to clipboard operation
lograge copied to clipboard

Subclasses of Lograge::Railtie aren't registered with Rails application

Open dmolesUC opened this issue 5 years ago • 2 comments

Rails::Railtie depends on an inherited hook to build the list of Railtie classes (Rails::Railties, Rails::Engine.railties) that should be initialized and/or invoked at runtime (see this section in Rails::Application).

Because this hook is only called for classes that extend Rails::Railtie directly, extending Lograge::Railtie will result in Lograge::Railtie itself getting registered, but not its subclass. Some Railties that are designed for extension (e.g. Rails::Engine itself) deal with this by adding their own inherited hook which calls super.

I've worked this by modifying Railtie.subclasses directly, but it feels a bit hacky.

module MyModule
  class Railtie < Lograge::Railtie
    Rails::Railtie.subclasses.delete(Lograge::Railtie)
    Rails::Railtie.subclasses << self

    # ...rest of class

At any rate, IDK if this is something Lograge is interested in supporting, but if not, it might be nice to add a note to the documentation.

System info

  • Lograge version: 0.11.2
  • Rails version: 6.0.3.4
  • RVM version: 1.29.10
  • Ruby version: 2.7.1p83
  • OS: macOS Catalina (10.15.7)

dmolesUC avatar Dec 11 '20 20:12 dmolesUC

(It's probably not strictly necessary for me to extend Lograge::Railtie in any case, but it seems a clearer way of documenting "this Railtie also initializes Lograge" than just calling require 'lograge' at the top without explicitly calling any Lograge code.)

dmolesUC avatar Dec 11 '20 20:12 dmolesUC

FWIW, the workaround above no longer works in Rails 6.1, which removes the subclasses array and instead changes Railtie to extend ActiveSupport::DescendantsTracker.

dmolesUC avatar Mar 17 '21 18:03 dmolesUC