base_doc should be nil for unassociated casted models
This commit is contained in:
parent
027dd9a3ee
commit
91cd1d9c7b
|
@ -30,7 +30,7 @@ module CouchRest
|
||||||
# Gets a reference to the top level extended
|
# Gets a reference to the top level extended
|
||||||
# document that a model is saved inside of
|
# document that a model is saved inside of
|
||||||
def base_doc
|
def base_doc
|
||||||
raise "Cannot call base_doc on a model that is not yet casted by a document" unless @casted_by
|
return nil unless @casted_by
|
||||||
@casted_by.base_doc
|
@casted_by.base_doc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ CouchRest::CastedModel.class_eval do
|
||||||
# The to_param method is needed for rails to generate resourceful routes.
|
# The to_param method is needed for rails to generate resourceful routes.
|
||||||
# In your controller, remember that it's actually the id of the document.
|
# In your controller, remember that it's actually the id of the document.
|
||||||
def id
|
def id
|
||||||
|
return nil if base_doc.nil?
|
||||||
base_doc.id
|
base_doc.id
|
||||||
end
|
end
|
||||||
alias_method :to_param, :id
|
alias_method :to_param, :id
|
||||||
|
|
|
@ -326,6 +326,11 @@ describe CouchRest::CastedModel do
|
||||||
@toy1.base_doc.title = 'Tom Foolery'
|
@toy1.base_doc.title = 'Tom Foolery'
|
||||||
@course.title.should == 'Tom Foolery'
|
@course.title.should == 'Tom Foolery'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return nil if not yet casted" do
|
||||||
|
person = Person.new
|
||||||
|
person.base_doc.should == nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "calling base_doc.save from a nested casted model" do
|
describe "calling base_doc.save from a nested casted model" do
|
||||||
|
|
Loading…
Reference in a new issue