2010-06-20 22:01:11 +02:00
|
|
|
module CouchRest
|
|
|
|
module Model
|
|
|
|
class Base < Document
|
|
|
|
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
|
2010-09-17 23:00:55 +02:00
|
|
|
include CouchRest::Model::Configuration
|
2011-04-30 00:34:12 +02:00
|
|
|
include CouchRest::Model::Connection
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::Persistence
|
|
|
|
include CouchRest::Model::Callbacks
|
2011-04-29 21:40:36 +02:00
|
|
|
include CouchRest::Model::DocumentQueries
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::Views
|
|
|
|
include CouchRest::Model::DesignDoc
|
|
|
|
include CouchRest::Model::ExtendedAttachments
|
|
|
|
include CouchRest::Model::ClassProxy
|
2011-02-09 21:21:03 +01:00
|
|
|
include CouchRest::Model::Proxyable
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::Collection
|
2010-10-22 15:39:12 +02:00
|
|
|
include CouchRest::Model::PropertyProtection
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::Associations
|
|
|
|
include CouchRest::Model::Validations
|
2011-02-06 04:32:23 +01:00
|
|
|
include CouchRest::Model::Designs
|
2011-03-03 08:28:57 +01:00
|
|
|
include CouchRest::Model::CastedBy
|
2011-04-20 16:44:49 +02:00
|
|
|
include CouchRest::Model::Dirty
|
2010-06-20 22:01:11 +02:00
|
|
|
|
|
|
|
def self.subclasses
|
|
|
|
@subclasses ||= []
|
|
|
|
end
|
2011-04-20 10:47:36 +02:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
def self.inherited(subklass)
|
|
|
|
super
|
|
|
|
subklass.send(:include, CouchRest::Model::Properties)
|
2011-03-03 08:28:57 +01:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
subklass.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def self.inherited(subklass)
|
|
|
|
super
|
|
|
|
subklass.properties = self.properties.dup
|
|
|
|
# This is nasty:
|
|
|
|
subklass._validators = self._validators.dup
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
subclasses << subklass
|
|
|
|
end
|
2011-04-20 10:47:36 +02:00
|
|
|
|
2010-08-04 04:09:05 +02:00
|
|
|
# Instantiate a new CouchRest::Model::Base by preparing all properties
|
2010-06-20 22:01:11 +02:00
|
|
|
# using the provided document hash.
|
|
|
|
#
|
|
|
|
# Options supported:
|
2011-04-20 10:47:36 +02:00
|
|
|
#
|
2010-06-20 22:01:11 +02:00
|
|
|
# * :directly_set_attributes: true when data comes directly from database
|
2011-02-09 21:21:03 +01:00
|
|
|
# * :database: provide an alternative database
|
2010-06-20 22:01:11 +02:00
|
|
|
#
|
2011-05-11 21:53:28 +02:00
|
|
|
# If a block is provided the new model will be passed into the
|
|
|
|
# block so that it can be populated.
|
2010-06-20 22:01:11 +02:00
|
|
|
def initialize(doc = {}, options = {})
|
2010-10-22 15:39:12 +02:00
|
|
|
doc = prepare_all_attributes(doc, options)
|
2011-02-09 21:21:03 +01:00
|
|
|
# set the instances database, if provided
|
|
|
|
self.database = options[:database] unless options[:database].nil?
|
2010-06-20 22:01:11 +02:00
|
|
|
super(doc)
|
|
|
|
unless self['_id'] && self['_rev']
|
2010-09-17 23:00:55 +02:00
|
|
|
self[self.model_type_key] = self.class.to_s
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
2011-05-11 21:53:28 +02:00
|
|
|
yield self if block_given?
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
after_initialize if respond_to?(:after_initialize)
|
|
|
|
end
|
2011-04-20 10:47:36 +02:00
|
|
|
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Temp solution to make the view_by methods available
|
|
|
|
def self.method_missing(m, *args, &block)
|
|
|
|
if has_view?(m)
|
|
|
|
query = args.shift || {}
|
|
|
|
return view(m, query, *args, &block)
|
|
|
|
elsif m.to_s =~ /^find_(by_.+)/
|
|
|
|
view_name = $1
|
|
|
|
if has_view?(view_name)
|
|
|
|
return first_from_view(view_name, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
2011-04-20 10:47:36 +02:00
|
|
|
|
2010-06-21 13:58:19 +02:00
|
|
|
## Compatibility with ActiveSupport and older frameworks
|
2011-04-20 10:47:36 +02:00
|
|
|
|
2010-06-21 13:58:19 +02:00
|
|
|
# Hack so that CouchRest::Document, which descends from Hash,
|
|
|
|
# doesn't appear to Rails routing as a Hash of options
|
|
|
|
def is_a?(klass)
|
|
|
|
return false if klass == Hash
|
|
|
|
super
|
|
|
|
end
|
|
|
|
alias :kind_of? :is_a?
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
!new?
|
|
|
|
end
|
2011-04-20 10:47:36 +02:00
|
|
|
|
2010-06-21 14:13:19 +02:00
|
|
|
def to_key
|
2011-04-20 10:47:36 +02:00
|
|
|
new? ? nil : [id]
|
2010-06-21 14:13:19 +02:00
|
|
|
end
|
2010-06-21 13:58:19 +02:00
|
|
|
|
2010-06-21 14:13:19 +02:00
|
|
|
alias :to_param :id
|
2010-06-20 22:01:11 +02:00
|
|
|
alias :new_record? :new?
|
|
|
|
alias :new_document? :new?
|
2011-04-20 10:47:36 +02:00
|
|
|
end
|
2010-06-20 22:01:11 +02:00
|
|
|
end
|
|
|
|
end
|