2010-05-11 00:15:40 +02:00
|
|
|
|
2010-05-10 21:19:24 +02:00
|
|
|
require File.join(File.dirname(__FILE__), "property")
|
2010-05-11 16:27:06 +02:00
|
|
|
require File.join(File.dirname(__FILE__), "validation")
|
2010-05-10 21:19:24 +02:00
|
|
|
require File.join(File.dirname(__FILE__), 'mixins')
|
2010-05-10 22:28:19 +02:00
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
module CouchRest
|
|
|
|
|
|
|
|
# Same as CouchRest::Document but with properties and validations
|
|
|
|
class ExtendedDocument < Document
|
2010-05-10 21:19:24 +02:00
|
|
|
|
2010-06-11 02:02:22 +02:00
|
|
|
VERSION = "1.0.0.beta5"
|
2010-05-10 21:19:24 +02:00
|
|
|
|
|
|
|
include CouchRest::Mixins::Callbacks
|
2009-02-10 11:15:39 +01:00
|
|
|
include CouchRest::Mixins::DocumentQueries
|
2009-02-03 01:16:14 +01:00
|
|
|
include CouchRest::Mixins::Views
|
2009-01-30 03:25:45 +01:00
|
|
|
include CouchRest::Mixins::DesignDoc
|
2009-02-18 02:59:31 +01:00
|
|
|
include CouchRest::Mixins::ExtendedAttachments
|
2009-03-27 14:42:49 +01:00
|
|
|
include CouchRest::Mixins::ClassProxy
|
2009-06-04 20:43:14 +02:00
|
|
|
include CouchRest::Mixins::Collection
|
2010-03-05 15:40:51 +01:00
|
|
|
include CouchRest::Mixins::AttributeProtection
|
2010-06-16 21:04:53 +02:00
|
|
|
include CouchRest::Mixins::Attributes
|
2009-05-28 08:59:43 +02:00
|
|
|
|
2010-05-12 23:43:17 +02:00
|
|
|
# Including validation here does not work due to the way inheritance is handled.
|
|
|
|
#include CouchRest::Validation
|
2010-05-11 16:27:06 +02:00
|
|
|
|
2009-06-04 20:43:14 +02:00
|
|
|
def self.subclasses
|
|
|
|
@subclasses ||= []
|
|
|
|
end
|
2009-02-04 02:33:31 +01:00
|
|
|
|
2009-02-10 11:15:39 +01:00
|
|
|
def self.inherited(subklass)
|
2010-02-27 01:39:09 +01:00
|
|
|
super
|
2009-02-10 11:15:39 +01:00
|
|
|
subklass.send(:include, CouchRest::Mixins::Properties)
|
2009-07-19 10:23:51 +02:00
|
|
|
subklass.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
2009-03-20 02:53:17 +01:00
|
|
|
def self.inherited(subklass)
|
2010-02-27 01:39:09 +01:00
|
|
|
super
|
2009-03-20 02:53:17 +01:00
|
|
|
subklass.properties = self.properties.dup
|
|
|
|
end
|
|
|
|
EOS
|
2009-05-28 19:36:25 +02:00
|
|
|
subclasses << subklass
|
2009-02-10 11:15:39 +01:00
|
|
|
end
|
|
|
|
|
2009-03-03 06:15:02 +01:00
|
|
|
# Accessors
|
|
|
|
attr_accessor :casted_by
|
|
|
|
|
2009-02-04 02:33:31 +01:00
|
|
|
# Callbacks
|
2009-06-07 23:52:23 +02:00
|
|
|
define_callbacks :create, "result == :halt"
|
|
|
|
define_callbacks :save, "result == :halt"
|
|
|
|
define_callbacks :update, "result == :halt"
|
|
|
|
define_callbacks :destroy, "result == :halt"
|
2009-12-04 08:58:07 +01:00
|
|
|
|
|
|
|
# Creates a new instance, bypassing attribute protection
|
|
|
|
#
|
2010-05-12 23:43:17 +02:00
|
|
|
#
|
2009-12-04 08:58:07 +01:00
|
|
|
# ==== Returns
|
|
|
|
# a document instance
|
2010-05-12 23:43:17 +02:00
|
|
|
def self.create_from_database(doc = {})
|
|
|
|
base = (doc['couchrest-type'].blank? || doc['couchrest-type'] == self.to_s) ? self : doc['couchrest-type'].constantize
|
|
|
|
base.new(doc, :directly_set_attributes => true)
|
2009-12-04 08:58:07 +01:00
|
|
|
end
|
2009-01-30 03:25:45 +01:00
|
|
|
|
2010-06-16 21:04:53 +02:00
|
|
|
|
|
|
|
# Instantiate a new ExtendedDocument by preparing all properties
|
|
|
|
# using the provided document hash.
|
|
|
|
#
|
|
|
|
# Options supported:
|
|
|
|
#
|
|
|
|
# * :directly_set_attributes: true when data comes directly from database
|
|
|
|
#
|
2010-05-12 23:43:17 +02:00
|
|
|
def initialize(doc = {}, options = {})
|
2010-06-16 21:04:53 +02:00
|
|
|
prepare_all_attributes(doc, options) # defined in CouchRest::Mixins::Attributes
|
2010-05-12 23:43:17 +02:00
|
|
|
super(doc)
|
2009-02-06 03:57:11 +01:00
|
|
|
unless self['_id'] && self['_rev']
|
|
|
|
self['couchrest-type'] = self.class.to_s
|
|
|
|
end
|
2010-03-03 21:01:27 +01:00
|
|
|
after_initialize if respond_to?(:after_initialize)
|
2009-02-06 03:57:11 +01:00
|
|
|
end
|
|
|
|
|
2009-07-17 09:12:33 +02:00
|
|
|
# Defines an instance and save it directly to the database
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# returns the reloaded document
|
|
|
|
def self.create(options)
|
|
|
|
instance = new(options)
|
|
|
|
instance.create
|
|
|
|
instance
|
|
|
|
end
|
2009-05-28 03:16:50 +02:00
|
|
|
|
2009-07-17 09:12:33 +02:00
|
|
|
# Defines an instance and save it directly to the database
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# returns the reloaded document or raises an exception
|
|
|
|
def self.create!(options)
|
|
|
|
instance = new(options)
|
|
|
|
instance.create!
|
|
|
|
instance
|
|
|
|
end
|
2009-02-06 03:57:11 +01:00
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
# Automatically set <tt>updated_at</tt> and <tt>created_at</tt> fields
|
|
|
|
# on the document whenever saving occurs. CouchRest uses a pretty
|
|
|
|
# decent time format by default. See Time#to_json
|
|
|
|
def self.timestamps!
|
2009-02-04 02:33:31 +01:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
2010-06-16 21:04:53 +02:00
|
|
|
property(:updated_at, Time, :read_only => true, :protected => true, :auto_validation => false)
|
|
|
|
property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false)
|
2009-02-04 02:33:31 +01:00
|
|
|
|
2009-06-07 11:57:22 +02:00
|
|
|
set_callback :save, :before do |object|
|
2010-06-16 21:04:53 +02:00
|
|
|
write_attribute('updated_at', Time.now)
|
|
|
|
write_attribute('created_at', Time.now) if object.new?
|
2009-02-04 02:33:31 +01:00
|
|
|
end
|
|
|
|
EOS
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
2009-06-10 06:02:04 +02:00
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
# 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.
|
|
|
|
def self.unique_id method = nil, &block
|
|
|
|
if method
|
|
|
|
define_method :set_unique_id do
|
|
|
|
self['_id'] ||= self.send(method)
|
|
|
|
end
|
|
|
|
elsif block
|
|
|
|
define_method :set_unique_id do
|
|
|
|
uniqid = block.call(self)
|
|
|
|
raise ArgumentError, "unique_id block must not return nil" if uniqid.nil?
|
|
|
|
self['_id'] ||= uniqid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-17 09:36:11 +01:00
|
|
|
# Temp solution to make the view_by methods available
|
2009-03-27 12:27:37 +01:00
|
|
|
def self.method_missing(m, *args, &block)
|
2009-02-17 09:36:11 +01:00
|
|
|
if has_view?(m)
|
|
|
|
query = args.shift || {}
|
2010-06-15 01:15:30 +02:00
|
|
|
return view(m, query, *args, &block)
|
2010-06-15 01:35:14 +02:00
|
|
|
elsif m.to_s =~ /^find_(by_.+)/
|
2010-06-15 01:15:30 +02:00
|
|
|
view_name = $1
|
|
|
|
if has_view?(view_name)
|
|
|
|
query = {:key => args.first, :limit => 1}
|
|
|
|
return view(view_name, query).first
|
|
|
|
end
|
2009-02-17 09:36:11 +01:00
|
|
|
end
|
2010-06-15 01:15:30 +02:00
|
|
|
super
|
2009-02-17 09:36:11 +01:00
|
|
|
end
|
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
### instance methods
|
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
# 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
|
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
# for compatibility with old-school frameworks
|
2009-08-25 02:12:13 +02:00
|
|
|
alias :new_record? :new?
|
|
|
|
alias :new_document? :new?
|
2009-01-30 03:25:45 +01:00
|
|
|
|
2009-02-13 05:28:07 +01:00
|
|
|
# Trigger the callbacks (before, after, around)
|
|
|
|
# and create the document
|
|
|
|
# It's important to have a create callback since you can't check if a document
|
|
|
|
# was new after you saved it
|
|
|
|
#
|
|
|
|
# When creating a document, both the create and the save callbacks will be triggered.
|
|
|
|
def create(bulk = false)
|
|
|
|
caught = catch(:halt) do
|
|
|
|
_run_create_callbacks do
|
|
|
|
_run_save_callbacks do
|
|
|
|
create_without_callbacks(bulk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# unlike save, create returns the newly created document
|
|
|
|
def create_without_callbacks(bulk =false)
|
|
|
|
raise ArgumentError, "a document requires a database to be created to (The document or the #{self.class} default database were not set)" unless database
|
2009-08-25 02:12:13 +02:00
|
|
|
set_unique_id if new? && self.respond_to?(:set_unique_id)
|
2009-02-13 05:28:07 +01:00
|
|
|
result = database.save_doc(self, bulk)
|
|
|
|
(result["ok"] == true) ? self : false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates the document in the db. Raises an exception
|
|
|
|
# if the document is not created properly.
|
|
|
|
def create!
|
|
|
|
raise "#{self.inspect} failed to save" unless self.create
|
|
|
|
end
|
|
|
|
|
|
|
|
# Trigger the callbacks (before, after, around)
|
|
|
|
# only if the document isn't new
|
|
|
|
def update(bulk = false)
|
|
|
|
caught = catch(:halt) do
|
2009-08-25 02:12:13 +02:00
|
|
|
if self.new?
|
2009-02-13 05:28:07 +01:00
|
|
|
save(bulk)
|
|
|
|
else
|
|
|
|
_run_update_callbacks do
|
|
|
|
_run_save_callbacks do
|
|
|
|
save_without_callbacks(bulk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 02:33:31 +01:00
|
|
|
# Trigger the callbacks (before, after, around)
|
|
|
|
# and save the document
|
|
|
|
def save(bulk = false)
|
|
|
|
caught = catch(:halt) do
|
2009-08-25 02:12:13 +02:00
|
|
|
if self.new?
|
2009-02-13 05:28:07 +01:00
|
|
|
_run_save_callbacks do
|
|
|
|
save_without_callbacks(bulk)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
update(bulk)
|
2009-02-04 02:33:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
# Overridden to set the unique ID.
|
|
|
|
# Returns a boolean value
|
2009-02-04 02:33:31 +01:00
|
|
|
def save_without_callbacks(bulk = false)
|
2009-02-11 01:10:35 +01:00
|
|
|
raise ArgumentError, "a document requires a database to be saved to (The document or the #{self.class} default database were not set)" unless database
|
2009-08-25 02:12:13 +02:00
|
|
|
set_unique_id if new? && self.respond_to?(:set_unique_id)
|
2009-01-30 03:25:45 +01:00
|
|
|
result = database.save_doc(self, bulk)
|
2009-10-12 13:55:02 +02:00
|
|
|
result["ok"] == true
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
|
|
|
|
2009-02-13 05:28:07 +01:00
|
|
|
# Saves the document to the db using save. Raises an exception
|
2009-01-30 03:25:45 +01:00
|
|
|
# if the document is not saved properly.
|
|
|
|
def save!
|
|
|
|
raise "#{self.inspect} failed to save" unless self.save
|
2009-09-03 05:27:04 +02:00
|
|
|
true
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Deletes the document from the database. Runs the :destroy callbacks.
|
|
|
|
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
|
|
|
|
# document to be saved to a new <tt>_id</tt>.
|
2009-02-18 02:59:31 +01:00
|
|
|
def destroy(bulk=false)
|
2009-02-04 02:33:31 +01:00
|
|
|
caught = catch(:halt) do
|
|
|
|
_run_destroy_callbacks do
|
2009-02-18 02:59:31 +01:00
|
|
|
result = database.delete_doc(self, bulk)
|
2009-02-04 02:33:31 +01:00
|
|
|
if result['ok']
|
2009-03-15 21:00:47 +01:00
|
|
|
self.delete('_rev')
|
|
|
|
self.delete('_id')
|
2009-02-04 02:33:31 +01:00
|
|
|
end
|
|
|
|
result['ok']
|
|
|
|
end
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
|
|
|
end
|
2010-06-16 21:04:53 +02:00
|
|
|
|
2009-01-30 03:25:45 +01:00
|
|
|
end
|
2009-04-23 06:24:38 +02:00
|
|
|
end
|