Skip to content
Browse files

Merge pull request #1948 from ahoward/master

rake db:create_indexes sometimes misses models - this can help
  • Loading branch information...
2 parents c6fc5fa + f7ca50e commit 4e2ed6de184e60b0fd48344263add618f19ed0b5 @ahoward ahoward committed Apr 29, 2012
Showing with 29 additions and 4 deletions.
  1. +29 −4 lib/mongoid/railties/database.rake
View
33 lib/mongoid/railties/database.rake
@@ -63,19 +63,44 @@ namespace :db do
namespace :mongoid do
# gets a list of the mongoid models defined in the app/models directory
def get_mongoid_models
- documents = []
+ # try to agressively load the app (including models in engines, etc)
+ #
+ if defined?(Rails) && Rails.respond_to?(:application)
+ Rails.application.eager_load!
+ end
+
+ # look hard for document classes in object_space and/or app/models/
+ #
+ klasses = []
+
+ Object.constants.each do |const|
+ object = const.to_s.constantize rescue next
+ next unless object.is_a?(Class)
+ klasses << klass
+ end
+
Dir.glob("app/models/**/*.rb").sort.each do |file|
model_path = file[0..-4].split('/')[2..-1]
+ klass = model_path.map { |path| path.camelize }.join('::').constantize
+ klasses << klass
+ end
+
+ klasses.uniq!
+
+ # select the klasses that descend from mongoid
+ #
+ documents = []
+
+ klasses.each do |klass|
begin
- klass = model_path.map { |path| path.camelize }.join('::').constantize
if klass.ancestors.include?(Mongoid::Document) && !klass.embedded
documents << klass
end
rescue
- # Just for non-mongoid objects that dont have the embedded
- # attribute at the class level.
+ next
end
end
+
documents
end

0 comments on commit 4e2ed6d

Please sign in to comment.
Something went wrong with that request. Please try again.