2010-06-20 22:01:11 +02:00
|
|
|
module CouchRest
|
|
|
|
module Model
|
|
|
|
module Persistence
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2011-02-09 14:22:54 +01:00
|
|
|
# Create the document. Validation is enabled by default and will return
|
2010-06-20 22:01:11 +02:00
|
|
|
# false if the document is not valid. If all goes well, the document will
|
|
|
|
# be returned.
|
|
|
|
def create(options = {})
|
|
|
|
return false unless perform_validations(options)
|
|
|
|
_run_create_callbacks do
|
|
|
|
_run_save_callbacks do
|
|
|
|
set_unique_id if new? && self.respond_to?(:set_unique_id)
|
|
|
|
result = database.save_doc(self)
|
2011-02-28 16:00:41 +01:00
|
|
|
ret = (result["ok"] == true) ? self : false
|
|
|
|
@changed_attributes.clear if ret && @changed_attributes
|
|
|
|
ret
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-02-09 14:22:54 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Creates the document in the db. Raises an exception
|
|
|
|
# if the document is not created properly.
|
2011-06-01 00:31:37 +02:00
|
|
|
def create!(options = {})
|
|
|
|
self.class.fail_validate!(self) unless self.create(options)
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
2011-02-09 14:22:54 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Trigger the callbacks (before, after, around)
|
|
|
|
# only if the document isn't new
|
|
|
|
def update(options = {})
|
2011-06-25 00:58:50 +02:00
|
|
|
raise "Cannot save a destroyed document!" if destroyed?
|
|
|
|
raise "Calling #{self.class.name}#update on document that has not been created!" if new?
|
2010-06-20 22:01:11 +02:00
|
|
|
return false unless perform_validations(options)
|
2011-04-20 16:44:49 +02:00
|
|
|
return true if !self.disable_dirty && !self.changed?
|
2010-06-20 22:01:11 +02:00
|
|
|
_run_update_callbacks do
|
|
|
|
_run_save_callbacks do
|
|
|
|
result = database.save_doc(self)
|
2011-02-28 16:00:41 +01:00
|
|
|
ret = result["ok"] == true
|
|
|
|
@changed_attributes.clear if ret && @changed_attributes
|
|
|
|
ret
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-02-09 14:22:54 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Trigger the callbacks (before, after, around) and save the document
|
|
|
|
def save(options = {})
|
|
|
|
self.new? ? create(options) : update(options)
|
|
|
|
end
|
2011-02-09 14:22:54 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Saves the document to the db using save. Raises an exception
|
|
|
|
# if the document is not saved properly.
|
|
|
|
def save!
|
|
|
|
self.class.fail_validate!(self) unless self.save
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Deletes the document from the database. Runs the :destroy callbacks.
|
|
|
|
def destroy
|
|
|
|
_run_destroy_callbacks do
|
|
|
|
result = database.delete_doc(self)
|
|
|
|
if result['ok']
|
2011-06-05 11:21:01 +02:00
|
|
|
@_destroyed = true
|
|
|
|
self.freeze
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
|
|
|
result['ok']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-05 11:21:01 +02:00
|
|
|
def destroyed?
|
|
|
|
!!@_destroyed
|
|
|
|
end
|
|
|
|
|
2011-06-25 00:58:50 +02:00
|
|
|
def persisted?
|
|
|
|
!new? && !destroyed?
|
|
|
|
end
|
|
|
|
|
2010-08-24 19:27:35 +02:00
|
|
|
# Update the document's attributes and save. For example:
|
|
|
|
#
|
|
|
|
# doc.update_attributes :name => "Fred"
|
|
|
|
# Is the equivilent of doing the following:
|
|
|
|
#
|
|
|
|
# doc.attributes = { :name => "Fred" }
|
|
|
|
# doc.save
|
|
|
|
#
|
|
|
|
def update_attributes(hash)
|
|
|
|
update_attributes_without_saving hash
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2011-02-09 14:31:52 +01:00
|
|
|
# Reloads the attributes of this object from the database.
|
|
|
|
# It doesn't override custom instance variables.
|
|
|
|
#
|
|
|
|
# Returns self.
|
|
|
|
def reload
|
2011-06-06 01:53:36 +02:00
|
|
|
prepare_all_attributes(database.get(id), :directly_set_attributes => true)
|
2011-02-09 14:31:52 +01:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2010-06-20 22:01:11 +02:00
|
|
|
|
|
|
|
def perform_validations(options = {})
|
|
|
|
perform_validation = case options
|
|
|
|
when Hash
|
|
|
|
options[:validate] != false
|
|
|
|
else
|
|
|
|
options
|
|
|
|
end
|
|
|
|
perform_validation ? valid? : true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
2011-06-08 18:22:35 +02:00
|
|
|
# Creates a new instance, bypassing attribute protection and
|
|
|
|
# uses the type field to determine which model to use to instanatiate
|
|
|
|
# the new object.
|
2010-06-20 22:01:11 +02:00
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# a document instance
|
2011-02-09 21:21:03 +01:00
|
|
|
#
|
2011-06-08 18:22:35 +02:00
|
|
|
def build_from_database(doc = {}, options = {}, &block)
|
|
|
|
src = doc[model_type_key]
|
|
|
|
base = (src.blank? || src == self.to_s) ? self : src.constantize
|
|
|
|
base.new(doc, options.merge(:directly_set_attributes => true), &block)
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
|
|
|
|
2011-02-09 14:22:54 +01:00
|
|
|
# Defines an instance and save it directly to the database
|
|
|
|
#
|
2010-06-20 22:01:11 +02:00
|
|
|
# ==== Returns
|
|
|
|
# returns the reloaded document
|
2011-06-01 00:03:08 +02:00
|
|
|
def create(attributes = {}, &block)
|
|
|
|
instance = new(attributes, &block)
|
2010-06-20 22:01:11 +02:00
|
|
|
instance.create
|
|
|
|
instance
|
|
|
|
end
|
2011-02-09 14:22:54 +01:00
|
|
|
|
|
|
|
# Defines an instance and save it directly to the database
|
|
|
|
#
|
2010-06-20 22:01:11 +02:00
|
|
|
# ==== Returns
|
|
|
|
# returns the reloaded document or raises an exception
|
2011-06-01 00:03:08 +02:00
|
|
|
def create!(attributes = {}, &block)
|
|
|
|
instance = new(attributes, &block)
|
2010-06-20 22:01:11 +02:00
|
|
|
instance.create!
|
|
|
|
instance
|
|
|
|
end
|
|
|
|
|
|
|
|
# Name a method that will be called before the document is first saved,
|
|
|
|
# which returns a string to be used for the document's <tt>_id</tt>.
|
|
|
|
#
|
|
|
|
# Because CouchDB enforces a constraint that each id must be unique,
|
|
|
|
# this can be used to enforce eg: uniq usernames. Note that this id
|
|
|
|
# must be globally unique across all document types which share a
|
|
|
|
# database, so if you'd like to scope uniqueness to this class, you
|
|
|
|
# should use the class name as part of the unique id.
|
2011-04-20 16:44:49 +02:00
|
|
|
def unique_id(method = nil, &block)
|
2010-06-20 22:01:11 +02:00
|
|
|
if method
|
|
|
|
define_method :set_unique_id do
|
2011-04-20 16:44:49 +02:00
|
|
|
self['_id'] ||= self.send(method)
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
|
|
|
elsif block
|
|
|
|
define_method :set_unique_id do
|
2011-04-20 16:44:49 +02:00
|
|
|
uniqid = block.call(self)
|
2010-06-20 22:01:11 +02:00
|
|
|
raise ArgumentError, "unique_id block must not return nil" if uniqid.nil?
|
|
|
|
self['_id'] ||= uniqid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Raise an error if validation failed.
|
|
|
|
def fail_validate!(document)
|
|
|
|
raise Errors::Validations.new(document)
|
|
|
|
end
|
|
|
|
end
|
2011-02-09 14:22:54 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|