Skip to content

Commit

Permalink
Builders now take in the base document
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 15, 2011
1 parent 8a26806 commit af49b6d
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/relations/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def build(name, object, metadata)
# @since 2.0.0.rc.1
def create_relation(object, metadata)
type = @attributes[metadata.inverse_type]
target = metadata.builder(object).build(type)
target = metadata.builder(self, object).build(type)
target ? metadata.relation.new(self, target, metadata) : nil
end

Expand Down
7 changes: 4 additions & 3 deletions lib/mongoid/relations/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ module Relations #:nodoc:
class Builder
include Threaded::Lifecycle

attr_reader :metadata, :object
attr_reader :base, :metadata, :object

# Instantiate the new builder for a relation.
#
# @example Create the builder.
# Builder.new(metadata, { :field => "value })
#
# @param [ Document ] base The base document.
# @param [ Metdata ] metadata The metadata for the relation.
# @param [ Hash, BSON::ObjectId ] object The attributes to build from or
# id to query with.
#
# @since 2.0.0.rc.1
def initialize(metadata, object)
@metadata, @object = metadata, object
def initialize(base, metadata, object)
@base, @metadata, @object = base, metadata, object
end

protected
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/embedded/in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ class << self
# @example Get the builder.
# Embedded::In.builder(meta, object, person)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build with.
#
# @return [ Builder ] A newly instantiated builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Embedded::In.new(meta, object)
def builder(base, meta, object)
Builders::Embedded::In.new(base, meta, object)
end

# Returns true if the relation is an embedded one. In this case
Expand Down
7 changes: 4 additions & 3 deletions lib/mongoid/relations/embedded/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def substitute(replacement)
else
atomically(:$set) do
if replacement.first.is_a?(Hash)
replacement = Many.builder(metadata, replacement).build
replacement = Many.builder(base, metadata, replacement).build
end
proxy.target = replacement.compact
if _assigning?
Expand Down Expand Up @@ -437,15 +437,16 @@ class << self
# @example Get the builder.
# Embedded::Many.builder(meta, object)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build
# with.
#
# @return [ Builder ] A newly instantiated builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Embedded::Many.new(meta, object)
def builder(base, meta, object)
Builders::Embedded::Many.new(base, meta, object)
end

# Returns true if the relation is an embedded one. In this case
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/embedded/one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ class << self
# @example Get the builder.
# Embedded::One.builder(meta, object, person)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build with.
#
# @return [ Builder ] A newly instantiated builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Embedded::One.new(meta, object)
def builder(base, meta, object)
Builders::Embedded::One.new(base, meta, object)
end

# Returns true if the relation is an embedded one. In this case
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ def autosave?
# @example Get the builder.
# metadata.builder(document)
#
# @param [ Document ] base The base document.
# @param [ Object ] object A document or attributes to give the builder.
#
# @return [ Builder ] The builder for the relation.
#
# @since 2.0.0.rc.1
def builder(object)
relation.builder(self, object)
def builder(base, object)
relation.builder(base, self, object)
end

# Returns the name of the strategy used for handling dependent relations.
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/referenced/in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ class << self
# @example Get the builder.
# Referenced::In.builder(meta, object)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build
# with.
#
# @return [ Builder ] A new builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Referenced::In.new(meta, object)
def builder(base, meta, object)
Builders::Referenced::In.new(base, meta, object)
end

# Get the standard criteria used for querying this relation.
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/referenced/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,16 @@ class << self
# @example Get the builder.
# Referenced::Many.builder(meta, object)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build
# with.
#
# @return [ Builder ] A new builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Referenced::Many.new(meta, object || [])
def builder(base, meta, object)
Builders::Referenced::Many.new(base, meta, object || [])
end

# Get the standard criteria used for querying this relation.
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/referenced/many_to_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,16 @@ class << self
# @example Get the builder.
# Referenced::ManyToMany.builder(meta, object)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build
# with.
#
# @return [ Builder ] A new builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Referenced::ManyToMany.new(meta, object)
def builder(base, meta, object)
Builders::Referenced::ManyToMany.new(base, meta, object)
end

# Create the standard criteria for this relation given the supplied
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/relations/referenced/one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ class << self
# @example Get the builder.
# Referenced::One.builder(meta, object)
#
# @param [ Document ] base The base document.
# @param [ Metadata ] meta The metadata of the relation.
# @param [ Document, Hash ] object A document or attributes to build
# with.
#
# @return [ Builder ] A new builder object.
#
# @since 2.0.0.rc.1
def builder(meta, object)
Builders::Referenced::One.new(meta, object)
def builder(base, meta, object)
Builders::Referenced::One.new(base, meta, object)
end

# Get the standard criteria used for querying this relation.
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/mongoid/relations/builders/embedded/in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
stub
end

let(:base) do
stub
end

let(:metadata) do
stub(:klass => Person, :name => :person)
end

context "when a document is provided" do

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

let(:document) do
Expand Down
8 changes: 6 additions & 2 deletions spec/unit/mongoid/relations/builders/embedded/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

describe Mongoid::Relations::Builders::Embedded::Many do

let(:base) do
stub
end

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

describe "#build" do
Expand Down Expand Up @@ -53,7 +57,7 @@
end

let(:builder) do
described_class.new(metadata, nil)
described_class.new(nil, metadata, nil)
end

before do
Expand Down
8 changes: 6 additions & 2 deletions spec/unit/mongoid/relations/builders/embedded/one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

describe Mongoid::Relations::Builders::Embedded::One do

let(:base) do
stub
end

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

describe "#build" do
Expand All @@ -15,7 +19,7 @@
end

let(:builder) do
described_class.new(metadata, nil)
described_class.new(nil, metadata, nil)
end

before do
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/mongoid/relations/builders/referenced/in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe Mongoid::Relations::Builders::Referenced::In do

let(:base) do
stub
end

describe "#build" do

let(:criteria) do
Expand All @@ -18,7 +22,7 @@
end

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

context "when provided an id" do
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/mongoid/relations/builders/referenced/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe Mongoid::Relations::Builders::Referenced::Many do

let(:base) do
stub
end

describe "#build" do

let(:criteria) do
Expand All @@ -19,7 +23,7 @@
end

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

context "when provided an id" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe Mongoid::Relations::Builders::Referenced::ManyToMany do

let(:base) do
stub
end

describe "#build" do

let(:metadata) do
Expand All @@ -14,7 +18,7 @@
end

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

context "when provided ids" do
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/mongoid/relations/builders/referenced/one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe Mongoid::Relations::Builders::Referenced::One do

let(:base) do
stub
end

describe "#build" do

let(:criteria) do
Expand All @@ -19,7 +23,7 @@
end

let(:builder) do
described_class.new(metadata, object)
described_class.new(base, metadata, object)
end

context "when provided an id" do
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/relations/embedded/in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
describe ".builder" do

it "returns the embedded one builder" do
described_class.builder(metadata, target).should be_a(builder_klass)
described_class.builder(base, metadata, target).should be_a(builder_klass)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/relations/embedded/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
end

it "returns the many builder" do
described_class.builder(metadata, document).should
described_class.builder(base, metadata, document).should
be_a(Mongoid::Relations::Builders::Embedded::Many)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/relations/embedded/one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
describe ".builder" do

it "returns the embedded one builder" do
described_class.builder(metadata, target).should be_a(builder_klass)
described_class.builder(base, metadata, target).should be_a(builder_klass)
end
end

Expand Down
6 changes: 5 additions & 1 deletion spec/unit/mongoid/relations/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
stub
end

let(:base) do
stub
end

it "returns the builder from the relation" do
metadata.builder(object).should
metadata.builder(base, object).should
be_a_kind_of(Mongoid::Relations::Builders::Embedded::One)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/relations/referenced/in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

it "returns the embedded in builder" do
described_class.builder(metadata, document).should
described_class.builder(nil, metadata, document).should
be_a_kind_of(builder_klass)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/relations/referenced/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end

it "returns the embedded in builder" do
described_class.builder(metadata, document).should
described_class.builder(nil, metadata, document).should
be_a_kind_of(builder_klass)
end
end
Expand Down
Loading

0 comments on commit af49b6d

Please sign in to comment.