Class: Rails::Initializer

Inherits:
Object show all
Defined in:
lib/has_many_polymorphs/autoload.rb

Instance Method Summary collapse

Instance Method Details

#after_initialize_with_autoloadObject

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.

Overrides Rails::Initializer#after_initialize.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/has_many_polymorphs/autoload.rb', line 12

def after_initialize_with_autoload
  after_initialize_without_autoload

  _logger_debug "has_many_polymorphs: autoload hook invoked"
  Dir["#{RAILS_ROOT}/app/models/**/*.rb"].each do |filename|
    next if filename =~ /svn|CVS|bzr/
    open filename do |file|
      if file.grep(/has_many_polymorphs|acts_as_double_polymorphic_join/).any?
        begin
          model = File.basename(filename)[0..-4].classify
          _logger_warn "has_many_polymorphs: preloading parent model #{model}"
          model.constantize
        rescue Object => e
          _logger_warn "has_many_polymorphs: WARNING; possibly critical error preloading #{model}: #{e.inspect}"
        end
      end
    end
  end
end