From 30e5d7f0aa66095a264877b9811c23b565e7dd4f Mon Sep 17 00:00:00 2001 From: mrb Date: Mon, 20 Apr 2015 15:53:04 -0400 Subject: [PATCH] Reduce mongoid_collection_names complexity --- README.markdown | 2 +- lib/database_cleaner/mongo/truncation.rb | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.markdown b/README.markdown index 6e37fb2..29188f6 100644 --- a/README.markdown +++ b/README.markdown @@ -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: diff --git a/lib/database_cleaner/mongo/truncation.rb b/lib/database_cleaner/mongo/truncation.rb index e5938dc..a09d1ff 100644 --- a/lib/database_cleaner/mongo/truncation.rb +++ b/lib/database_cleaner/mongo/truncation.rb @@ -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