Module: HasManyPolymorphs
- Defined in:
- lib/has_many_polymorphs/railtie.rb,
lib/has_many_polymorphs/autoload.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- MODELS_ROOT =
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.['requirements'] << 'lib/my_extension' end end Rails.root.join('app', 'models')
- DEFAULT_OPTIONS =
{ :file_pattern => "#{MODELS_ROOT}/**/*.rb", :file_exclusions => ['svn', 'CVS', 'bzr'], :methods => ['has_many_polymorphs', 'acts_as_double_polymorphic_join'], :requirements => []}
- @@options =
HashWithIndifferentAccess.new(DEFAULT_OPTIONS)
Class Method Summary collapse
-
.setup ⇒ Object
Dispatcher callback to load polymorphic relationships from the top down.
Class Method Details
.setup ⇒ Object
Dispatcher callback to load polymorphic relationships from the top down.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/has_many_polymorphs/autoload.rb', line 31 def self.setup _logger_debug "autoload hook invoked" [:requirements].each do |requirement| _logger_warn "forcing requirement load of #{requirement}" require requirement end Dir.glob([:file_pattern]).each do |filename| next if filename =~ /#{options[:file_exclusions].join("|")}/ open(filename) do |file| if file.grep(/#{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 |