Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #387 from brianhempel/delegate_more_to_plucky

Delegate more methods to plucky [Fixes #379, #266]
  • Loading branch information...
commit 2d807c058d8c25cc92878aba6237af4f5db8806a 2 parents 029dd45 + 9633972
@bkeepers bkeepers authored
View
15 lib/mongo_mapper/plugins/querying.rb
@@ -8,8 +8,11 @@ module Querying
extend ActiveSupport::Concern
module ClassMethods
+ extend Forwardable
include PluckyMethods
+ def_delegators :query, :to_a, :size, :empty?
+
def find_each(opts={})
super(opts).each { |doc| yield(doc) }
end
@@ -81,7 +84,7 @@ def transformer
def find_some(ids, options={})
query = query(options).amend(:_id => ids.flatten.compact.uniq)
- find_many(query.to_hash).compact
+ query.all
end
def find_some!(ids, options={})
@@ -95,16 +98,6 @@ def find_some!(ids, options={})
docs
end
- # All query methods that load documents pass through find_one or find_many
- def find_one(options={})
- query(options).first
- end
-
- # All query methods that load documents pass through find_one or find_many
- def find_many(options)
- query(options).all
- end
-
def initialize_each(*docs)
instances = []
docs = [{}] if docs.blank?
View
12 lib/mongo_mapper/plugins/querying/plucky_methods.rb
@@ -6,9 +6,15 @@ module Plugins
module Querying
module PluckyMethods
extend Forwardable
- def_delegators :query, :where, :fields, :limit, :skip, :sort,
- :count, :last, :first, :all, :paginate,
- :find, :find!, :exists?, :exist?, :find_each
+ def_delegators :query, :where, :filter,
+ :fields, :ignore, :only,
+ :limit, :paginate, :per_page, :skip, :offset,
+ :sort, :order, :reverse,
+ :count,
+ :distinct,
+ :last, :first, :find_one, :all, :find_each,
+ :find, :find!,
+ :exists?, :exist?
end
end
end
View
43 test/functional/associations/test_many_documents_proxy.rb
@@ -393,6 +393,49 @@ def pets
project.statuses.count(:name => 'Foo').should == 1
end
+
+ should "ignore unpersisted documents" do
+ project = Project.create
+ project.statuses.build(:name => 'Foo')
+ project.statuses.count.should == 0
+ end
+ end
+
+ context "size" do
+ should "reflect both persisted and new documents" do
+ project = Project.create
+ 3.times { project.statuses.create(:name => 'Foo!') }
+ 2.times { project.statuses.build(:name => 'Foo!') }
+ project.statuses.size.should == 5
+ end
+ end
+
+ context "empty?" do
+ should "be true with no associated docs" do
+ project = Project.create
+ project.statuses.empty?.should be_true
+ end
+
+ should "be false if a document is built" do
+ project = Project.create
+ project.statuses.build(:name => 'Foo!')
+ project.statuses.empty?.should be_false
+ end
+
+ should "be false if a document is created" do
+ project = Project.create
+ project.statuses.create(:name => 'Foo!')
+ project.statuses.empty?.should be_false
+ end
+ end
+
+ context "to_a" do
+ should "include persisted and new documents" do
+ project = Project.create
+ 3.times { project.statuses.create(:name => 'Foo!') }
+ 2.times { project.statuses.build(:name => 'Foo!') }
+ project.statuses.to_a.size.should == 5
+ end
end
context "to_json" do
View
37 test/functional/test_querying.rb
@@ -482,6 +482,30 @@ def setup
end
end
+ context ".size" do
+ should "return 0 if no documents" do
+ @document.count.should == 0
+ end
+
+ should "return the number of documents" do
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
+ @document.count.should == 3
+ end
+ end
+
+ context ".empty?" do
+ should "be true if no documents" do
+ @document.empty?.should be_true
+ end
+
+ should "be false if documents present" do
+ @doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
+ @document.empty?.should be_false
+ end
+ end
+
context ".exists?" do
setup do
@doc = @document.create(:first_name => "James", :age => 27)
@@ -505,6 +529,19 @@ def setup
end
end
+ context "to_a" do
+ should "return an array" do
+ @document.to_a.class.should == Array
+ end
+
+ should "return everything" do
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
+ @document.to_a.size.should == 3
+ end
+ end
+
context ".where" do
setup do
@doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
Please sign in to comment.
Something went wrong with that request. Please try again.