Reduce mongoid_collection_names complexity

This commit is contained in:
mrb 2015-04-20 15:53:04 -04:00
parent bb608dd3ac
commit 30e5d7f0aa
2 changed files with 10 additions and 5 deletions

View file

@ -7,7 +7,7 @@ Each strategy is a small amount of code but is code that is usually needed in an
ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported. ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported.
[![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner) [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner) [![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner)
Here is an overview of the strategies supported for each library: Here is an overview of the strategies supported for each library:

View file

@ -20,10 +20,7 @@ module DatabaseCleaner
def mongoid_collection_names def mongoid_collection_names
@@mongoid_collection_names ||= Hash.new{|h,k| h[k]=[]}.tap do |names| @@mongoid_collection_names ||= Hash.new{|h,k| h[k]=[]}.tap do |names|
ObjectSpace.each_object(Class) do |klass| ObjectSpace.each_object(Class) do |klass|
next unless klass.ancestors.map(&:to_s).include?('Mongoid::Document') (names[klass.db.name] << klass.collection_name) if valid_collection_name?(klass)
next if klass.embedded
next if klass.collection_name.empty?
names[klass.db.name] << klass.collection_name
end end
end end
end end
@ -52,6 +49,14 @@ module DatabaseCleaner
db_collections db_collections
end end
private
def valid_collection_name?(klass)
klass.ancestors.map(&:to_s).include?('Mongoid::Document') &&
!klass.embedded &&
!klass.collection_name.empty?
end
end end
end end
end end