4 module HasManyPolymorphs
7 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.
9 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 <tt>after_initialize</tt> block in <tt>config/environment.rb</tt> that appends to the <tt>'requirements'</tt> key:
10 Rails::Initializer.run do |config|
11 # your other configuration here
13 config.after_initialize do
14 config.has_many_polymorphs_options['requirements'] << '/lib/my_extension'
23 :file_pattern => "#{RAILS_ROOT}/app/models/**/*.rb",
24 :file_exclusions => ['svn', 'CVS', 'bzr'],
25 :methods => ['has_many_polymorphs', 'acts_as_double_polymorphic_join'],
28 mattr_accessor :options
29 @@options = HashWithIndifferentAccess.new(DEFAULT_OPTIONS)
31 # Override for Rails::Initializer#after_initialize.
34 _logger_debug "autoload hook invoked"
36 options[:requirements].each do |requirement|
40 Dir[options[:file_pattern]].each do |filename|
41 next if filename =~ /#{options[:file_exclusions].join("|")}/
42 open filename do |file|
43 if file.grep(/#{options[:methods].join("|")}/).any?
45 model = File.basename(filename)[0..-4].camelize
46 _logger_warn "preloading parent model #{model}"
49 _logger_warn "WARNING; possibly critical error preloading #{model}: #{e.inspect}"
60 Dispatcher.to_prepare do
61 HasManyPolymorphs::Autoload.autoload