2010-05-10 21:19:24 +02:00
|
|
|
require File.expand_path('../../spec_helper', __FILE__)
|
2010-03-30 22:50:47 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
|
|
|
require File.join(FIXTURE_PATH, 'more', 'person')
|
2009-03-03 06:15:02 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
class Driver < CouchRest::Model::Base
|
2009-03-03 06:15:02 +01:00
|
|
|
use_database TEST_SERVER.default_database
|
2010-03-30 22:50:47 +02:00
|
|
|
# You have to add a casted_by accessor if you want to reach a casted extended doc parent
|
|
|
|
attr_accessor :casted_by
|
2009-03-03 06:15:02 +01:00
|
|
|
|
|
|
|
property :name
|
|
|
|
end
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
class Car < CouchRest::Model::Base
|
2009-03-03 06:15:02 +01:00
|
|
|
use_database TEST_SERVER.default_database
|
|
|
|
|
|
|
|
property :name
|
2010-06-20 22:01:11 +02:00
|
|
|
property :driver, Driver
|
2009-03-03 06:15:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "casting an extended document" do
|
|
|
|
|
2009-05-25 18:16:48 +02:00
|
|
|
before(:each) do
|
|
|
|
@driver = Driver.new(:name => 'Matt')
|
|
|
|
@car = Car.new(:name => 'Renault 306', :driver => @driver)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should retain all properties of the casted attribute" do
|
|
|
|
@car.driver.should == @driver
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should let the casted document know who casted it" do
|
|
|
|
@car.driver.casted_by.should == @car
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "assigning a value to casted attribute after initializing an object" do
|
|
|
|
|
2009-03-03 06:15:02 +01:00
|
|
|
before(:each) do
|
|
|
|
@car = Car.new(:name => 'Renault 306')
|
|
|
|
@driver = Driver.new(:name => 'Matt')
|
|
|
|
end
|
|
|
|
|
2009-05-25 18:16:48 +02:00
|
|
|
it "should not create an empty casted object" do
|
|
|
|
@car.driver.should be_nil
|
|
|
|
end
|
2009-03-03 06:15:02 +01:00
|
|
|
|
2009-05-25 18:16:48 +02:00
|
|
|
it "should let you assign the value" do
|
2009-03-03 06:15:02 +01:00
|
|
|
@car.driver = @driver
|
|
|
|
@car.driver.name.should == 'Matt'
|
|
|
|
end
|
|
|
|
|
2009-06-10 06:02:04 +02:00
|
|
|
it "should cast attribute" do
|
2010-02-27 01:39:09 +01:00
|
|
|
@car.driver = JSON.parse(@driver.to_json)
|
2009-06-10 06:02:04 +02:00
|
|
|
@car.driver.should be_instance_of(Driver)
|
2009-05-25 18:16:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
describe "casting a model from parsed JSON" do
|
2009-05-25 18:16:48 +02:00
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@driver = Driver.new(:name => 'Matt')
|
|
|
|
@car = Car.new(:name => 'Renault 306', :driver => @driver)
|
2010-02-27 01:39:09 +01:00
|
|
|
@new_car = Car.new(JSON.parse(@car.to_json))
|
2009-05-25 18:16:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should cast casted attribute" do
|
|
|
|
@new_car.driver.should be_instance_of(Driver)
|
2009-03-03 06:15:02 +01:00
|
|
|
end
|
|
|
|
|
2009-05-25 18:16:48 +02:00
|
|
|
it "should retain all properties of the casted attribute" do
|
|
|
|
@new_car.driver.should == @driver
|
|
|
|
end
|
2010-03-30 22:50:47 +02:00
|
|
|
end
|