Added helper for accessing the top level document. And more rails compatibility.
This commit is contained in:
parent
936ce54449
commit
d012380b67
5 changed files with 80 additions and 0 deletions
|
@ -27,6 +27,13 @@ module CouchRest
|
|||
super(key.to_s)
|
||||
end
|
||||
|
||||
# Gets a reference to the top level extended
|
||||
# document that a model is saved inside of
|
||||
def base_doc
|
||||
raise "Cannot call base_doc on a model that is not yet casted by a document" unless @casted_by
|
||||
@casted_by.base_doc
|
||||
end
|
||||
|
||||
# False if the casted model has already
|
||||
# been saved in the containing document
|
||||
def new_model?
|
||||
|
|
|
@ -110,6 +110,19 @@ module CouchRest
|
|||
self.class.properties
|
||||
end
|
||||
|
||||
# Gets a reference to the actual document in the DB
|
||||
# Calls up to the next document if there is one,
|
||||
# Otherwise we're at the top and we return self
|
||||
def base_doc
|
||||
return self if base_doc?
|
||||
@casted_by.base_doc
|
||||
end
|
||||
|
||||
# Checks if we're the top document
|
||||
def base_doc?
|
||||
!@casted_by
|
||||
end
|
||||
|
||||
# Takes a hash as argument, and applies the values by using writer methods
|
||||
# for each key. It doesn't save the document at the end. Raises a NoMethodError if the corresponding methods are
|
||||
# missing. In case of error, no attributes are changed.
|
||||
|
|
|
@ -21,8 +21,17 @@ CouchRest::Document.class_eval do
|
|||
super
|
||||
end
|
||||
alias_method :kind_of?, :is_a?
|
||||
alias_method :to_param, :id
|
||||
end
|
||||
|
||||
CouchRest::CastedModel.class_eval do
|
||||
# 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.
|
||||
def id
|
||||
base_doc.id
|
||||
end
|
||||
alias_method :to_param, :id
|
||||
end
|
||||
|
||||
require Pathname.new(File.dirname(__FILE__)).join('..', 'validation', 'validation_errors')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue