Permalink
Browse files

add support for autoloading name-spaced models

Signed-off-by: Evan <evan+fauna@cloudbur.st>
  • Loading branch information...
1 parent d226794 commit 421dd0bd60b246652bbbafd64724ebf1efa27607 @stepheneb stepheneb committed with Evan Aug 14, 2009
Showing with 8 additions and 3 deletions.
  1. +8 −3 lib/has_many_polymorphs/autoload.rb
@@ -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
@@ -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

0 comments on commit 421dd0b

Please sign in to comment.