2009-05-14 05:44:57 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2010-05-10 21:19:24 +02:00
|
|
|
require File.expand_path('../../spec_helper', __FILE__)
|
2010-06-20 22:01:11 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'cat')
|
2010-04-16 02:51:59 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'person')
|
2009-02-09 20:20:23 +01:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'card')
|
2009-05-27 22:24:25 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'question')
|
2009-05-29 07:42:30 +02:00
|
|
|
require File.join(FIXTURE_PATH, 'more', 'course')
|
2009-05-14 07:50:29 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
class WithCastedModelMixin < Hash
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::CastedModel
|
2009-02-10 11:15:39 +01:00
|
|
|
property :name
|
2009-04-02 16:00:28 +02:00
|
|
|
property :no_value
|
2010-06-20 22:01:11 +02:00
|
|
|
property :details, Object, :default => {}
|
|
|
|
property :casted_attribute, WithCastedModelMixin
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
class DummyModel < CouchRest::Model::Base
|
2009-02-10 11:15:39 +01:00
|
|
|
use_database TEST_SERVER.default_database
|
|
|
|
raise "Default DB not set" if TEST_SERVER.default_database.nil?
|
2010-06-16 21:04:53 +02:00
|
|
|
property :casted_attribute, WithCastedModelMixin
|
2010-06-20 22:01:11 +02:00
|
|
|
property :keywords, [String]
|
2010-06-16 21:04:53 +02:00
|
|
|
property :sub_models do |child|
|
|
|
|
child.property :title
|
|
|
|
end
|
2011-02-05 22:38:22 +01:00
|
|
|
property :param_free_sub_models do
|
|
|
|
property :title
|
|
|
|
end
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
|
|
|
|
2009-06-05 04:49:10 +02:00
|
|
|
class WithCastedCallBackModel < Hash
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::CastedModel
|
2009-06-05 04:49:10 +02:00
|
|
|
property :name
|
2011-02-01 20:32:38 +01:00
|
|
|
property :run_before_validation
|
|
|
|
property :run_after_validation
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2011-02-01 20:32:38 +01:00
|
|
|
validates_presence_of :run_before_validation
|
|
|
|
|
|
|
|
before_validation do |object|
|
|
|
|
object.run_before_validation = true
|
2009-06-05 04:49:10 +02:00
|
|
|
end
|
2011-02-01 20:32:38 +01:00
|
|
|
after_validation do |object|
|
|
|
|
object.run_after_validation = true
|
2009-06-05 04:49:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
class CastedCallbackDoc < CouchRest::Model::Base
|
|
|
|
use_database TEST_SERVER.default_database
|
|
|
|
raise "Default DB not set" if TEST_SERVER.default_database.nil?
|
|
|
|
property :callback_model, WithCastedCallBackModel
|
|
|
|
end
|
|
|
|
|
|
|
|
describe CouchRest::Model::CastedModel do
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
describe "A non hash class including CastedModel" do
|
|
|
|
it "should fail raising and include error" do
|
|
|
|
lambda do
|
|
|
|
class NotAHashButWithCastedModelMixin
|
|
|
|
include CouchRest::CastedModel
|
|
|
|
property :name
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
end.should raise_error
|
|
|
|
end
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
describe "isolated" do
|
|
|
|
before(:each) do
|
|
|
|
@obj = WithCastedModelMixin.new
|
|
|
|
end
|
|
|
|
it "should automatically include the property mixin and define getters and setters" do
|
|
|
|
@obj.name = 'Matt'
|
2010-09-01 23:13:52 +02:00
|
|
|
@obj.name.should == 'Matt'
|
2009-02-09 20:20:23 +01:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-25 21:12:56 +02:00
|
|
|
it "should allow override of default" do
|
|
|
|
@obj = WithCastedModelMixin.new(:name => 'Eric', :details => {'color' => 'orange'})
|
|
|
|
@obj.name.should == 'Eric'
|
|
|
|
@obj.details['color'].should == 'orange'
|
|
|
|
end
|
2011-04-20 16:44:49 +02:00
|
|
|
it "should always return base_doc? as false" do
|
|
|
|
@obj.base_doc?.should be_false
|
|
|
|
end
|
|
|
|
|
2009-05-25 21:12:56 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-25 21:12:56 +02:00
|
|
|
describe "casted as an attribute, but without a value" do
|
|
|
|
before(:each) do
|
|
|
|
@obj = DummyModel.new
|
|
|
|
@casted_obj = @obj.casted_attribute
|
|
|
|
end
|
|
|
|
it "should be nil" do
|
|
|
|
@casted_obj.should == nil
|
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
end
|
2010-06-16 21:04:53 +02:00
|
|
|
|
|
|
|
describe "anonymous sub casted models" do
|
|
|
|
before :each do
|
|
|
|
@obj = DummyModel.new
|
|
|
|
end
|
|
|
|
it "should be empty initially" do
|
|
|
|
@obj.sub_models.should_not be_nil
|
|
|
|
@obj.sub_models.should be_empty
|
|
|
|
end
|
|
|
|
it "should be updatable using a hash" do
|
|
|
|
@obj.sub_models << {:title => 'test'}
|
|
|
|
@obj.sub_models.first.title.should eql('test')
|
|
|
|
end
|
2011-02-05 22:38:22 +01:00
|
|
|
it "should be empty intitally (without params)" do
|
|
|
|
@obj.param_free_sub_models.should_not be_nil
|
|
|
|
@obj.param_free_sub_models.should be_empty
|
|
|
|
end
|
|
|
|
it "should be updatable using a hash (without params)" do
|
|
|
|
@obj.param_free_sub_models << {:title => 'test'}
|
|
|
|
@obj.param_free_sub_models.first.title.should eql('test')
|
|
|
|
end
|
2010-06-16 21:04:53 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
describe "casted as attribute" do
|
|
|
|
before(:each) do
|
2009-05-25 21:12:56 +02:00
|
|
|
casted = {:name => 'not whatever'}
|
|
|
|
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever', :casted_attribute => casted})
|
2009-02-09 20:20:23 +01:00
|
|
|
@casted_obj = @obj.casted_attribute
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
it "should be available from its parent" do
|
|
|
|
@casted_obj.should be_an_instance_of(WithCastedModelMixin)
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
it "should have the getters defined" do
|
|
|
|
@casted_obj.name.should == 'whatever'
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
it "should know who casted it" do
|
|
|
|
@casted_obj.casted_by.should == @obj
|
|
|
|
end
|
2009-04-02 16:00:28 +02:00
|
|
|
|
2011-04-20 16:44:49 +02:00
|
|
|
it "should know which property casted it" do
|
|
|
|
@casted_obj.casted_by_property.should == @obj.properties.detect{|p| p.to_s == 'casted_attribute'}
|
|
|
|
end
|
|
|
|
|
2009-04-02 16:00:28 +02:00
|
|
|
it "should return nil for the 'no_value' attribute" do
|
|
|
|
@casted_obj.no_value.should be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return nil for the unknown attribute" do
|
|
|
|
@casted_obj["unknown"].should be_nil
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-25 18:40:01 +02:00
|
|
|
it "should return {} for the hash attribute" do
|
2009-05-25 21:12:56 +02:00
|
|
|
@casted_obj.details.should == {}
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-25 21:12:56 +02:00
|
|
|
it "should cast its own attributes" do
|
|
|
|
@casted_obj.casted_attribute.should be_instance_of(WithCastedModelMixin)
|
2009-05-25 18:40:01 +02:00
|
|
|
end
|
2010-08-24 19:30:41 +02:00
|
|
|
|
|
|
|
it "should raise an error if save or update_attributes called" do
|
|
|
|
expect { @casted_obj.casted_attribute.save }.to raise_error(NoMethodError)
|
|
|
|
expect { @casted_obj.casted_attribute.update_attributes(:name => "Fubar") }.to raise_error(NoMethodError)
|
|
|
|
end
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-13 05:28:07 +01:00
|
|
|
describe "casted as an array of a different type" do
|
|
|
|
before(:each) do
|
2009-05-14 05:44:57 +02:00
|
|
|
@obj = DummyModel.new(:keywords => ['couch', 'sofa', 'relax', 'canapé'])
|
2009-02-13 05:28:07 +01:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2010-03-30 22:50:47 +02:00
|
|
|
it "should cast the array properly" do
|
2011-03-03 13:52:19 +01:00
|
|
|
@obj.keywords.should be_kind_of(Array)
|
2009-02-13 05:28:07 +01:00
|
|
|
@obj.keywords.first.should == 'couch'
|
|
|
|
end
|
2009-05-27 22:24:25 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-27 22:24:25 +02:00
|
|
|
describe "update attributes without saving" do
|
|
|
|
before(:each) do
|
|
|
|
@question = Question.new(:q => "What is your quest?", :a => "To seek the Holy Grail")
|
|
|
|
end
|
|
|
|
it "should work for attribute= methods" do
|
|
|
|
@question.q.should == "What is your quest?"
|
|
|
|
@question['a'].should == "To seek the Holy Grail"
|
|
|
|
@question.update_attributes_without_saving(:q => "What is your favorite color?", 'a' => "Blue")
|
|
|
|
@question['q'].should == "What is your favorite color?"
|
|
|
|
@question.a.should == "Blue"
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-27 22:24:25 +02:00
|
|
|
it "should also work for attributes= alias" do
|
|
|
|
@question.respond_to?(:attributes=).should be_true
|
|
|
|
@question.attributes = {:q => "What is your favorite color?", 'a' => "Blue"}
|
|
|
|
@question['q'].should == "What is your favorite color?"
|
|
|
|
@question.a.should == "Blue"
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-27 22:24:25 +02:00
|
|
|
it "should flip out if an attribute= method is missing" do
|
|
|
|
lambda {
|
|
|
|
@q.update_attributes_without_saving('foo' => "something", :a => "No green")
|
|
|
|
}.should raise_error(NoMethodError)
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-27 22:24:25 +02:00
|
|
|
it "should not change any attributes if there is an error" do
|
|
|
|
lambda {
|
|
|
|
@q.update_attributes_without_saving('foo' => "something", :a => "No green")
|
|
|
|
}.should raise_error(NoMethodError)
|
|
|
|
@question.q.should == "What is your quest?"
|
|
|
|
@question.a.should == "To seek the Holy Grail"
|
|
|
|
end
|
2010-08-24 19:27:35 +02:00
|
|
|
|
2009-02-13 05:28:07 +01:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
describe "saved document with casted models" do
|
|
|
|
before(:each) do
|
2009-03-08 15:27:30 +01:00
|
|
|
reset_test_db!
|
2009-02-10 11:15:39 +01:00
|
|
|
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever'})
|
2009-02-11 01:10:35 +01:00
|
|
|
@obj.save.should be_true
|
2009-02-17 09:36:11 +01:00
|
|
|
@obj = DummyModel.get(@obj.id)
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
it "should be able to load with the casted models" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.should_not be_nil
|
|
|
|
casted_obj.should be_an_instance_of(WithCastedModelMixin)
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
it "should have defined getters for the casted model" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.name.should == "whatever"
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
it "should have defined setters for the casted model" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.name = "test"
|
|
|
|
casted_obj.name.should == "test"
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-25 21:12:56 +02:00
|
|
|
it "should retain an override of a casted model attribute's default" do
|
|
|
|
casted_obj = @obj.casted_attribute
|
|
|
|
casted_obj.details['color'] = 'orange'
|
|
|
|
@obj.save
|
|
|
|
casted_obj = DummyModel.get(@obj.id).casted_attribute
|
|
|
|
casted_obj.details['color'].should == 'orange'
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
end
|
2009-05-12 00:17:53 +02:00
|
|
|
|
|
|
|
describe "saving document with array of casted models and validation" do
|
|
|
|
before :each do
|
2010-05-10 21:19:24 +02:00
|
|
|
@cat = Cat.new :name => "felix"
|
2009-05-12 00:17:53 +02:00
|
|
|
@cat.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should save" do
|
|
|
|
toy = CatToy.new :name => "Mouse"
|
|
|
|
@cat.toys.push(toy)
|
|
|
|
@cat.save.should be_true
|
2009-06-12 08:34:02 +02:00
|
|
|
@cat = Cat.get @cat.id
|
2010-06-20 22:01:11 +02:00
|
|
|
@cat.toys.class.should == CouchRest::Model::CastedArray
|
2009-06-12 08:34:02 +02:00
|
|
|
@cat.toys.first.class.should == CatToy
|
|
|
|
@cat.toys.first.should === toy
|
2009-05-12 00:17:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail because name is not present" do
|
|
|
|
toy = CatToy.new
|
|
|
|
@cat.toys.push(toy)
|
2009-05-14 07:50:29 +02:00
|
|
|
@cat.should_not be_valid
|
2009-05-12 00:17:53 +02:00
|
|
|
@cat.save.should be_false
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-14 07:50:29 +02:00
|
|
|
it "should not fail if the casted model doesn't have validation" do
|
2010-06-20 22:01:11 +02:00
|
|
|
Cat.property :masters, [Person], :default => []
|
2009-10-30 19:07:59 +01:00
|
|
|
Cat.validates_presence_of :name
|
2009-05-14 07:50:29 +02:00
|
|
|
cat = Cat.new(:name => 'kitty')
|
|
|
|
cat.should be_valid
|
|
|
|
cat.masters.push Person.new
|
|
|
|
cat.should be_valid
|
|
|
|
end
|
2009-05-12 00:17:53 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-28 21:18:23 +02:00
|
|
|
describe "calling valid?" do
|
|
|
|
before :each do
|
|
|
|
@cat = Cat.new
|
|
|
|
@toy1 = CatToy.new
|
|
|
|
@toy2 = CatToy.new
|
|
|
|
@toy3 = CatToy.new
|
|
|
|
@cat.favorite_toy = @toy1
|
|
|
|
@cat.toys << @toy2
|
|
|
|
@cat.toys << @toy3
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-28 21:18:23 +02:00
|
|
|
describe "on the top document" do
|
|
|
|
it "should put errors on all invalid casted models" do
|
|
|
|
@cat.should_not be_valid
|
|
|
|
@cat.errors.should_not be_empty
|
|
|
|
@toy1.errors.should_not be_empty
|
|
|
|
@toy2.errors.should_not be_empty
|
|
|
|
@toy3.errors.should_not be_empty
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-28 21:18:23 +02:00
|
|
|
it "should not put errors on valid casted models" do
|
|
|
|
@toy1.name = "Feather"
|
2010-09-01 23:13:52 +02:00
|
|
|
@toy2.name = "Twine"
|
2009-05-28 21:18:23 +02:00
|
|
|
@cat.should_not be_valid
|
|
|
|
@cat.errors.should_not be_empty
|
|
|
|
@toy1.errors.should be_empty
|
|
|
|
@toy2.errors.should be_empty
|
|
|
|
@toy3.errors.should_not be_empty
|
|
|
|
end
|
2010-12-28 21:22:29 +01:00
|
|
|
|
|
|
|
it "should not use dperecated ActiveModel options" do
|
|
|
|
ActiveSupport::Deprecation.should_not_receive(:warn)
|
|
|
|
@cat.should_not be_valid
|
|
|
|
end
|
2009-05-28 21:18:23 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-28 21:18:23 +02:00
|
|
|
describe "on a casted model property" do
|
|
|
|
it "should only validate itself" do
|
|
|
|
@toy1.should_not be_valid
|
|
|
|
@toy1.errors.should_not be_empty
|
|
|
|
@cat.errors.should be_empty
|
|
|
|
@toy2.errors.should be_empty
|
|
|
|
@toy3.errors.should be_empty
|
|
|
|
end
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-28 21:18:23 +02:00
|
|
|
describe "on a casted model inside a casted collection" do
|
|
|
|
it "should only validate itself" do
|
|
|
|
@toy2.should_not be_valid
|
|
|
|
@toy2.errors.should_not be_empty
|
|
|
|
@cat.errors.should be_empty
|
|
|
|
@toy1.errors.should be_empty
|
|
|
|
@toy3.errors.should be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-06-05 05:44:44 +02:00
|
|
|
describe "calling new? on a casted model" do
|
2009-05-29 01:09:53 +02:00
|
|
|
before :each do
|
|
|
|
reset_test_db!
|
|
|
|
@cat = Cat.new(:name => 'Sockington')
|
2009-06-10 06:02:04 +02:00
|
|
|
@favorite_toy = CatToy.new(:name => 'Catnip Ball')
|
|
|
|
@cat.favorite_toy = @favorite_toy
|
2009-05-29 01:09:53 +02:00
|
|
|
@cat.toys << CatToy.new(:name => 'Fuzzy Stick')
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 01:09:53 +02:00
|
|
|
it "should be true on new" do
|
2009-06-05 05:44:44 +02:00
|
|
|
CatToy.new.should be_new
|
2009-05-29 01:09:53 +02:00
|
|
|
CatToy.new.new_record?.should be_true
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 01:09:53 +02:00
|
|
|
it "should be true after assignment" do
|
2009-06-10 06:02:04 +02:00
|
|
|
@cat.should be_new
|
2009-06-05 05:44:44 +02:00
|
|
|
@cat.favorite_toy.should be_new
|
|
|
|
@cat.toys.first.should be_new
|
2009-05-29 01:09:53 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 01:09:53 +02:00
|
|
|
it "should not be true after create or save" do
|
|
|
|
@cat.create
|
|
|
|
@cat.save
|
2009-06-05 05:44:44 +02:00
|
|
|
@cat.favorite_toy.should_not be_new
|
2010-06-16 21:04:53 +02:00
|
|
|
@cat.toys.first.casted_by.should eql(@cat)
|
2009-06-05 05:44:44 +02:00
|
|
|
@cat.toys.first.should_not be_new
|
2009-05-29 01:09:53 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 01:09:53 +02:00
|
|
|
it "should not be true after get from the database" do
|
|
|
|
@cat.save
|
|
|
|
@cat = Cat.get(@cat.id)
|
2009-06-05 05:44:44 +02:00
|
|
|
@cat.favorite_toy.should_not be_new
|
|
|
|
@cat.toys.first.should_not be_new
|
2009-05-29 01:09:53 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 01:09:53 +02:00
|
|
|
it "should still be true after a failed create or save" do
|
|
|
|
@cat.name = nil
|
|
|
|
@cat.create.should be_false
|
|
|
|
@cat.save.should be_false
|
2009-06-05 05:44:44 +02:00
|
|
|
@cat.favorite_toy.should be_new
|
|
|
|
@cat.toys.first.should be_new
|
2009-05-29 01:09:53 +02:00
|
|
|
end
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
describe "calling base_doc from a nested casted model" do
|
|
|
|
before :each do
|
|
|
|
@course = Course.new(:title => 'Science 101')
|
2010-06-18 01:24:49 +02:00
|
|
|
@professor = Person.new(:name => ['Professor', 'Plum'])
|
2009-05-29 07:42:30 +02:00
|
|
|
@cat = Cat.new(:name => 'Scratchy')
|
|
|
|
@toy1 = CatToy.new
|
|
|
|
@toy2 = CatToy.new
|
|
|
|
@course.professor = @professor
|
|
|
|
@professor.pet = @cat
|
|
|
|
@cat.favorite_toy = @toy1
|
|
|
|
@cat.toys << @toy2
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
|
|
|
it 'should let you copy over casted arrays' do
|
|
|
|
question = Question.new
|
|
|
|
@course.questions << question
|
|
|
|
new_course = Course.new
|
|
|
|
new_course.questions = @course.questions
|
|
|
|
new_course.questions.should include(question)
|
|
|
|
end
|
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
it "should reference the top document for" do
|
|
|
|
@course.base_doc.should === @course
|
2009-06-10 06:02:04 +02:00
|
|
|
@professor.casted_by.should === @course
|
2009-05-29 07:42:30 +02:00
|
|
|
@professor.base_doc.should === @course
|
|
|
|
@cat.base_doc.should === @course
|
|
|
|
@toy1.base_doc.should === @course
|
|
|
|
@toy2.base_doc.should === @course
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
it "should call setter on top document" do
|
2009-06-10 06:02:04 +02:00
|
|
|
@toy1.base_doc.should_not be_nil
|
2009-05-29 07:42:30 +02:00
|
|
|
@toy1.base_doc.title = 'Tom Foolery'
|
|
|
|
@course.title.should == 'Tom Foolery'
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-31 08:20:39 +02:00
|
|
|
it "should return nil if not yet casted" do
|
|
|
|
person = Person.new
|
|
|
|
person.base_doc.should == nil
|
|
|
|
end
|
2009-05-29 07:42:30 +02:00
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
describe "calling base_doc.save from a nested casted model" do
|
|
|
|
before :each do
|
|
|
|
reset_test_db!
|
|
|
|
@cat = Cat.new(:name => 'Snowball')
|
|
|
|
@toy = CatToy.new
|
|
|
|
@cat.favorite_toy = @toy
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
it "should not save parent document when casted model is invalid" do
|
|
|
|
@toy.should_not be_valid
|
|
|
|
@toy.base_doc.save.should be_false
|
|
|
|
lambda{@toy.base_doc.save!}.should raise_error
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
it "should save parent document when nested casted model is valid" do
|
|
|
|
@toy.name = "Mr Squeaks"
|
|
|
|
@toy.should be_valid
|
|
|
|
@toy.base_doc.save.should be_true
|
|
|
|
lambda{@toy.base_doc.save!}.should_not raise_error
|
|
|
|
end
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-06-05 04:49:10 +02:00
|
|
|
describe "callbacks" do
|
|
|
|
before(:each) do
|
|
|
|
@doc = CastedCallbackDoc.new
|
|
|
|
@model = WithCastedCallBackModel.new
|
|
|
|
@doc.callback_model = @model
|
|
|
|
end
|
2010-09-01 23:13:52 +02:00
|
|
|
|
2009-06-05 04:49:10 +02:00
|
|
|
describe "validate" do
|
2011-02-01 20:32:38 +01:00
|
|
|
it "should run before_validation before validating" do
|
|
|
|
@model.run_before_validation.should be_nil
|
2009-06-05 04:49:10 +02:00
|
|
|
@model.should be_valid
|
2011-02-01 20:32:38 +01:00
|
|
|
@model.run_before_validation.should be_true
|
2009-06-05 04:49:10 +02:00
|
|
|
end
|
2011-02-01 20:32:38 +01:00
|
|
|
it "should run after_validation after validating" do
|
|
|
|
@model.run_after_validation.should be_nil
|
2009-06-05 04:49:10 +02:00
|
|
|
@model.should be_valid
|
2011-02-01 20:32:38 +01:00
|
|
|
@model.run_after_validation.should be_true
|
2010-09-01 23:13:52 +02:00
|
|
|
end
|
2009-06-05 04:49:10 +02:00
|
|
|
end
|
|
|
|
end
|
2009-03-08 15:27:30 +01:00
|
|
|
end
|