Skip to content

Commit

Permalink
Set update consumers to the collection name, not the class. Fixes #1640
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jan 25, 2012
1 parent a27cb77 commit 0a6cd67
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#1640 Update consumers should be tied to the name of the collection
they persist to, not the name of the class.

* \#1636 Scopes no longer modify parent class scopes when subclassing.
(Hans Hasselberg)

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/collection.rb
Expand Up @@ -142,7 +142,7 @@ def master(options = {})
#
# @since 2.0.0
def update(selector, document, options = {})
updater = Threaded.update_consumer(klass)
updater = Threaded.update_consumer(name)
if updater
updater.consume(selector, document, options)
else
Expand Down
24 changes: 24 additions & 0 deletions lib/mongoid/contexts/enumerable.rb
Expand Up @@ -210,6 +210,18 @@ def update_all(attributes = nil)

protected

# Get the root class collection name.
#
# @example Get the root class collection name.
# context.collection_name
#
# @return [ String ] The name of the collection.
#
# @since 2.4.3
def collection_name
root ? root.collection_name : nil
end

# Filters the documents against the criteria's selector
#
# @example Filter the documents.
Expand Down Expand Up @@ -251,10 +263,22 @@ def limit(documents)
documents
end

# Get the root document for the enumerable.
#
# @example Get the root document.
# context.root
#
# @return [ Document ] The root.
def root
@root ||= documents.first.try(:_root)
end

# Get the root class for the enumerable.
#
# @example Get the root class.
# context.root_class
#
# @return [ Class ] The root class.
def root_class
@root_class ||= root ? root.class : nil
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/relations/embedded/atomic.rb
Expand Up @@ -55,13 +55,13 @@ module Atomic
#
# @since 2.0.0
def atomically(modifier, &block)
updater = Threaded.update_consumer(root_class) ||
Threaded.set_update_consumer(root_class, MODIFIERS[modifier].new)
updater = Threaded.update_consumer(collection_name) ||
Threaded.set_update_consumer(collection_name, MODIFIERS[modifier].new)
count_executions do
block.call if block
end.tap do
if @executions.zero?
Threaded.set_update_consumer(root_class, nil)
Threaded.set_update_consumer(collection_name, nil)
updater.execute(collection)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/relations/proxy.rb
Expand Up @@ -18,6 +18,7 @@ class Proxy
# Backwards compatibility with Mongoid beta releases.
delegate :klass, :to => :metadata
delegate :bind_one, :unbind_one, :to => :binding
delegate :collection_name, :to => :base

# Convenience for setting the target and the metadata properties since
# all proxies will need to do this.
Expand Down
54 changes: 54 additions & 0 deletions spec/functional/mongoid/relations/embedded/many_spec.rb
Expand Up @@ -317,6 +317,60 @@
end
end

context "when setting for inherited docs" do

context "when the parent collection is already accessed" do

before do
Person.collection
end

context "when setting via the subclass" do

let(:doctor) do
Doctor.new
end

let(:address_one) do
Address.new(:street => "tauentzien")
end

before do
doctor.addresses = [ address_one ]
doctor.save
end

it "sets the documents" do
doctor.addresses.should eq([ address_one ])
end

it "persists the document" do
doctor.reload.addresses.should eq([ address_one ])
end

context "when setting the relation multiple times" do

let(:address_two) do
Address.new(:street => "kudamm")
end

before do
doctor.addresses = [ address_two ]
doctor.save
end

it "sets the new documents" do
doctor.addresses.should eq([ address_two ])
end

it "persits only the new documents" do
doctor.reload.addresses.should eq([ address_two ])
end
end
end
end
end

context "when replacing an existing relation" do

let(:person) do
Expand Down
8 changes: 6 additions & 2 deletions spec/unit/mongoid/relations/embedded/atomic_spec.rb
Expand Up @@ -12,6 +12,10 @@ def collection
def root_class
Person
end

def collection_name
"people"
end
end

describe "#atomically" do
Expand All @@ -35,7 +39,7 @@ def root_class

it "puts the updater on the current thread" do
klass.send(:atomically, :$set) do
Mongoid::Threaded.update_consumer(Person).should eq(set)
Mongoid::Threaded.update_consumer("people").should eq(set)
end
end
end
Expand All @@ -44,7 +48,7 @@ def root_class

it "removes the updater from the current thread" do
klass.send(:atomically, :$set)
Mongoid::Threaded.update_consumer(Person).should be_nil
Mongoid::Threaded.update_consumer("people").should be_nil
end
end
end
Expand Down

0 comments on commit 0a6cd67

Please sign in to comment.