Permalink
Browse files

Adding clear specs on has many

  • Loading branch information...
1 parent 12ef03f commit 43213d68feb317314be5f3428506edfb63689057 @durran durran committed Dec 28, 2009
Showing with 28 additions and 5 deletions.
  1. +6 −4 lib/mongoid/associations/has_many.rb
  2. +22 −1 spec/unit/mongoid/associations/has_many_spec.rb
@@ -18,10 +18,12 @@ def <<(*objects)
# Clears the association, and notifies the parents of the removal.
def clear
- object = @documents.first
- object.changed(true)
- object.notify_observers(object, true)
- @documents.clear
+ unless @documents.empty?
+ object = @documents.first
+ object.changed(true)
+ object.notify_observers(object, true)
+ @documents.clear
+ end
end
# Appends the object to the +Array+, setting its parent in
@@ -104,7 +104,10 @@
describe "#concat" do
before do
- @association = Mongoid::Associations::HasMany.new(@document, Mongoid::Associations::Options.new(:name => :addresses))
+ @association = Mongoid::Associations::HasMany.new(
+ @document,
+ Mongoid::Associations::Options.new(:name => :addresses)
+ )
@address = Address.new
end
@@ -116,6 +119,24 @@
end
+ describe "#clear" do
+
+ before do
+ @association = Mongoid::Associations::HasMany.new(
+ @document,
+ Mongoid::Associations::Options.new(:name => :addresses)
+ )
+ @address = Address.new
+ @association << @address
+ end
+
+ it "clears out the association" do
+ @association.clear
+ @association.size.should == 0
+ end
+
+ end
+
describe "#find" do
before do

0 comments on commit 43213d6

Please sign in to comment.