Merge pull request #356 from mrb/reduce_method_complexity

Reduce mongoid_collection_names complexity
This commit is contained in:
Ernesto Tagwerker 2015-04-20 17:30:22 -03:00
commit 4a06819db5
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.
[![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:

View file

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