Skip to content

Commit

Permalink
Updating belongs_to to point at target
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jan 24, 2010
1 parent 4ac474b commit cad6597
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
14 changes: 7 additions & 7 deletions lib/mongoid/associations/belongs_to.rb
Expand Up @@ -4,7 +4,7 @@ module Associations #:nodoc:
class BelongsTo #:nodoc:
include Proxy

attr_reader :document, :options
attr_reader :options, :target

# Creates the new association by setting the internal
# document as the passed in Document. This should be the
Expand All @@ -17,20 +17,20 @@ class BelongsTo #:nodoc:
#
# document: The parent +Document+
# options: The association options
def initialize(document, options)
@document, @options = document, options
def initialize(target, options)
@target, @options = target, options
end

# Returns the parent document. The id param is present for
# compatibility with rails, however this could be overwritten
# in the future.
def find(id)
@document
@target
end

# Delegate all missing methods over to the parent +Document+.
def method_missing(name, *args, &block)
@document.send(name, *args, &block)
@target.send(name, *args, &block)
end

class << self
Expand All @@ -43,8 +43,8 @@ class << self
# document: The parent +Document+
# options: The association options
def instantiate(document, options)
parent = document._parent
parent.nil? ? nil : new(parent, options)
target = document._parent
target.nil? ? nil : new(target, options)
end

# Returns the macro used to create the association.
Expand Down
87 changes: 49 additions & 38 deletions spec/unit/mongoid/associations/belongs_to_spec.rb
Expand Up @@ -2,88 +2,99 @@

describe Mongoid::Associations::BelongsTo do

let(:child) { Name.new(:first_name => "Drexel", :last_name => "Spivey") }
let(:target) { Person.new(:title => "Pimp") }

let(:options) do
Mongoid::Associations::Options.new(:name => :person)
end

describe "#find" do

before do
@parent = Name.new(:first_name => "Drexel")
@options = Mongoid::Associations::Options.new(:name => :person)
@association = Mongoid::Associations::BelongsTo.new(@parent, @options)
@association = Mongoid::Associations::BelongsTo.new(target, options)
end

context "when finding by id" do

it "returns the document in the array with that id" do
name = @association.find(Mongo::ObjectID.new.to_s)
name.should == @parent
it "always returns the target document" do
@association.find("").should == target
end

end

end

context "when decorating" do
describe ".instantiate" do

before do
@parent = Name.new(:first_name => "Drexel")
@options = Mongoid::Associations::Options.new(:name => :person)
@association = Mongoid::Associations::BelongsTo.new(@parent, @options)
end
context "when parent exists" do

context "when getting values" do
before do
@parent = stub
@target = stub(:_parent => @parent)
@association = Mongoid::Associations::BelongsTo.instantiate(@target, options)
end

it "delegates to the document" do
@association.first_name.should == "Drexel"
it "sets the parent to the target" do
@association.target.should == @parent
end

end

context "when setting values" do
context "when parent is nil" do

it "delegates to the document" do
@association.first_name = "Test"
@association.first_name.should == "Test"
before do
@document = stub(:_parent => nil)
end

it "returns nil" do
Mongoid::Associations::BelongsTo.instantiate(@document, options).should be_nil
end

end

end

describe ".instantiate" do
describe ".macro" do

context "when parent exists" do
it "returns :belongs_to" do
Mongoid::Associations::BelongsTo.macro.should == :belongs_to
end

before do
@parent = Name.new(:first_name => "Drexel")
@document = stub(:_parent => @parent)
@options = Mongoid::Associations::Options.new(:name => :person)
end
end

describe "#method_missing" do

before do
@association = Mongoid::Associations::BelongsTo.new(target, options)
end

it "delegates to new" do
Mongoid::Associations::BelongsTo.expects(:new).with(@parent, @options)
Mongoid::Associations::BelongsTo.instantiate(@document, @options)
context "when method is a getter" do

it "delegates to the target" do
@association.title.should == "Pimp"
end

end

context "when parent is nil" do
context "when method is a setter" do

before do
@document = stub(:_parent => nil)
@options = Mongoid::Associations::Options.new(:name => :person)
@association.title = "Dealer"
end

it "returns nil" do
Mongoid::Associations::BelongsTo.instantiate(@document, @options).should be_nil
it "delegates to the target" do
@association.title.should == "Dealer"
end

end

end
context "when method does not exist" do

describe ".macro" do
it "raises an error" do
lambda { @association.nothing }.should raise_error(NoMethodError)
end

it "returns :belongs_to" do
Mongoid::Associations::BelongsTo.macro.should == :belongs_to
end

end
Expand Down

0 comments on commit cad6597

Please sign in to comment.