Sha256: 589b53d819d8ff39ca4b5bb479282d994c3f2b3ca3e0008dda83b890edd74fb0

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'initializer'

class Rails::Initializer

=begin rdoc
Searches for models that use <tt>has_many_polymorphs</tt> or <tt>acts_as_double_polymorphic_join</tt> 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.
=end

  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  
  
  alias_method_chain :after_initialize, :autoload
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_many_polymorphs-2.9 lib/has_many_polymorphs/autoload.rb