Searches for models that use has_many_polymorphs
or
acts_as_double_polymorphic_join
and makes sure that they get
loaded during app initialization. This ensures that helper methods are
injected into the target classes.
Note that you can override DEFAULT_OPTIONS via
Rails::Configuration#has_many_polymorphs_options. For example, if you need
an application extension to be required before has_many_polymorphs loads
your models, add an after_initialize
block in
config/environment.rb
that appends to the
'requirements'
key:
Rails::Initializer.run do |config| # your other configuration here config.after_initialize do config.has_many_polymorphs.options['requirements'] << 'lib/my_extension' end end
Dispatcher callback to load polymorphic relationships from the top down.
# File lib/has_many_polymorphs/autoload.rb, line 31 def self.setup _logger_debug "autoload hook invoked" options[:requirements].each do |requirement| _logger_warn "forcing requirement load of #{requirement}" require requirement end Dir.glob(options[:file_pattern]).each do |filename| next if filename =~ %r#{options[:file_exclusions].join("|")}/ open(filename) do |file| if file.grep(%r#{options[:methods].join("|")}/).any? begin # determines the modelname by the directory - this allows the autoload of namespaced models modelname = filename[0..-4].gsub("#{MODELS_ROOT.to_s}/", "") model = modelname.camelize _logger_warn "preloading parent model #{model}" model.constantize rescue Object => e _logger_warn "#{model} could not be preloaded: #{e.inspect} #{e.backtrace}" end end end end end