I am trying to write a lib plugin/extension to perform an action where I need to know which Models have been marked for use with this plugin.
Currently, I am marking the models in the fashion of acts_as_something method which is added to each Model intended to be used with the plugin.
The main file of the plugin looks like this
# lib/foo.rb
module Foo
class << self
attr_accessor :models
end
self.models = []
module Model
def acts_as_foo
Foo.models << self
end
end
ActiveSupport.on_load(:active_record) do
extend Foo::Model
end
The intended use is to then call in a controller Foo.perform, which needs to know the marked models in order to carry out the intended action, the idea being getting the list of models from Foo.models.
It works as intended if when config.eager_load is set to true in development.rb , otherwise the files of the models have not been used/loaded yet and therefore Foo.models is an empty Array.
My goal is to be able to add more models to Foo without having to change Foo's code like this.
#app/models/bar.rb
class Bar < ApplicationRecord
acts_as_foo
end
Any ideas on the best way to implement this?
I had a similar problem before in my gem.
I ended up loading ONLY model files (which is the minimum of my requirement same as yours because the DSL code is there like your
acts_as_foo), and not immediately eager loading all Rails-related files usingRails.application.eager_load!.