2010-06-20 22:01:11 +02:00
|
|
|
module CouchRest::Model
|
2011-06-25 19:24:43 +02:00
|
|
|
module Embeddable
|
2010-06-20 22:01:11 +02:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2011-07-04 18:53:25 +02:00
|
|
|
# Include Attributes early to ensure super() will work
|
|
|
|
include CouchRest::Attributes
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
included do
|
2010-09-17 23:25:56 +02:00
|
|
|
include CouchRest::Model::Configuration
|
2010-06-20 22:01:11 +02:00
|
|
|
include CouchRest::Model::Properties
|
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-06-09 01:05:22 +02:00
|
|
|
include CouchRest::Model::Callbacks
|
2011-04-20 16:44:49 +02:00
|
|
|
include CouchRest::Model::CastedBy
|
2011-02-28 16:00:41 +01:00
|
|
|
include CouchRest::Model::Dirty
|
2011-06-08 22:31:57 +02:00
|
|
|
include CouchRest::Model::Callbacks
|
|
|
|
|
2011-04-20 16:44:49 +02:00
|
|
|
class_eval do
|
|
|
|
# Override CastedBy's base_doc?
|
|
|
|
def base_doc?
|
|
|
|
false # Can never be base doc!
|
|
|
|
end
|
2011-04-20 12:31:46 +02:00
|
|
|
|
2011-06-25 00:58:50 +02:00
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
end
|
2011-04-20 12:31:46 +02:00
|
|
|
|
2011-07-04 18:53:25 +02:00
|
|
|
# Initialize a new Casted Model. Accepts the same
|
|
|
|
# options as CouchRest::Model::Base for preparing and initializing
|
|
|
|
# attributes.
|
|
|
|
def initialize(keys = {}, options = {})
|
|
|
|
super()
|
|
|
|
prepare_all_attributes(keys, options)
|
|
|
|
run_callbacks(:initialize) { self }
|
|
|
|
end
|
|
|
|
|
2009-05-29 02:00:06 +02:00
|
|
|
# False if the casted model has already
|
2009-05-29 01:09:53 +02:00
|
|
|
# been saved in the containing document
|
2009-06-05 05:44:44 +02:00
|
|
|
def new?
|
2011-04-20 16:44:49 +02:00
|
|
|
casted_by.nil? ? true : casted_by.new?
|
2009-05-29 01:09:53 +02:00
|
|
|
end
|
2009-06-05 05:44:44 +02:00
|
|
|
alias :new_record? :new?
|
2010-06-21 13:58:19 +02:00
|
|
|
|
|
|
|
def persisted?
|
|
|
|
!new?
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
return nil if base_doc.nil?
|
|
|
|
base_doc.id
|
|
|
|
end
|
|
|
|
alias :to_key :id
|
|
|
|
alias :to_param :id
|
2011-03-13 14:06:38 +01:00
|
|
|
|
2009-05-27 22:24:25 +02:00
|
|
|
# Sets the attributes from a hash
|
|
|
|
def update_attributes_without_saving(hash)
|
|
|
|
hash.each do |k, v|
|
|
|
|
raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
|
|
|
|
end
|
|
|
|
hash.each do |k, v|
|
|
|
|
self.send("#{k}=",v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :attributes= :update_attributes_without_saving
|
2011-02-28 16:00:41 +01:00
|
|
|
|
2011-06-25 19:24:43 +02:00
|
|
|
end # End Embeddable
|
|
|
|
|
|
|
|
# Provide backwards compatability with previous versions (pre 1.1.0)
|
|
|
|
module CastedModel
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
|
|
include CouchRest::Model::Embeddable
|
|
|
|
end
|
2009-02-09 20:20:23 +01:00
|
|
|
end
|
2011-04-20 16:44:49 +02:00
|
|
|
|
2009-05-29 02:00:06 +02:00
|
|
|
end
|