Skip to content

Commit

Permalink
add support for autoloading name-spaced models
Browse files Browse the repository at this point in the history
  • Loading branch information
stepheneb committed Aug 14, 2009
1 parent fd531f3 commit b563f4d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/has_many_polymorphs/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ module HasManyPolymorphs
end
=end


MODELS_ROOT = "#{RAILS_ROOT}/app/models/"

DEFAULT_OPTIONS = {
:file_pattern => "#{RAILS_ROOT}/app/models/**/*.rb",
:file_pattern => "#{MODELS_ROOT}**/*.rb",
:file_exclusions => ['svn', 'CVS', 'bzr'],
:methods => ['has_many_polymorphs', 'acts_as_double_polymorphic_join'],
:requirements => []}

mattr_accessor :options
@@options = HashWithIndifferentAccess.new(DEFAULT_OPTIONS)


# Dispatcher callback to load polymorphic relationships from the top down.
def self.autoload

Expand All @@ -41,7 +44,9 @@ def self.autoload
open(filename) do |file|
if file.grep(/#{options[:methods].join("|")}/).any?
begin
model = File.basename(filename)[0..-4].camelize
modelname = filename[0..-4]
modelname.slice!(MODELS_ROOT)
model = modelname.camelize
_logger_warn "preloading parent model #{model}"
model.constantize
rescue Object => e
Expand Down

0 comments on commit b563f4d

Please sign in to comment.