Adding configuration support and changing 'couchrest-type' key to 'model' along with config options

This commit is contained in:
Sam Lown 2010-09-17 23:00:55 +02:00
parent 5c21de8586
commit 85cd1308bc
14 changed files with 157 additions and 17 deletions

View file

@ -4,6 +4,7 @@ module CouchRest
extend ActiveModel::Naming
include CouchRest::Model::Configuration
include CouchRest::Model::Persistence
include CouchRest::Model::Callbacks
include CouchRest::Model::DocumentQueries
@ -37,7 +38,7 @@ module CouchRest
# Accessors
attr_accessor :casted_by
# Instantiate a new CouchRest::Model::Base by preparing all properties
# using the provided document hash.
@ -50,7 +51,7 @@ module CouchRest
prepare_all_attributes(doc, options)
super(doc)
unless self['_id'] && self['_rev']
self['couchrest-type'] = self.class.to_s
self[self.model_type_key] = self.class.to_s
end
after_initialize if respond_to?(:after_initialize)
end

View file

@ -0,0 +1,49 @@
module CouchRest
# CouchRest Model Configuration support, stolen from Carrierwave by jnicklas
# http://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/uploader/configuration.rb
module Model
module Configuration
extend ActiveSupport::Concern
included do
add_config :model_type_key
configure do |config|
config.model_type_key = 'model'
end
end
module ClassMethods
def add_config(name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{name}(value=nil)
@#{name} = value if value
return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
name = superclass.#{name}
return nil if name.nil? && !instance_variable_defined?("@#{name}")
@#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
end
def self.#{name}=(value)
@#{name} = value
end
def #{name}
self.class.#{name}
end
RUBY
end
def configure
yield self
end
end
end
end
end

View file

@ -31,7 +31,7 @@ module CouchRest
"views" => {
'all' => {
'map' => "function(doc) {
if (doc['couchrest-type'] == '#{self.to_s}') {
if (doc['#{self.model_type_key}'] == '#{self.to_s}') {
emit(doc['_id'],1);
}
}"

View file

@ -8,21 +8,21 @@ module CouchRest
module ClassMethods
# Load all documents that have the "couchrest-type" field equal to the
# Load all documents that have the model_type_key's field equal to the
# name of the current class. Take the standard set of
# CouchRest::Database#view options.
def all(opts = {}, &block)
view(:all, opts, &block)
end
# Returns the number of documents that have the "couchrest-type" field
# Returns the number of documents that have the model_type_key's field
# equal to the name of the current class. Takes the standard set of
# CouchRest::Database#view options
def count(opts = {}, &block)
all({:raw => true, :limit => 0}.merge(opts), &block)['total_rows']
end
# Load the first document that have the "couchrest-type" field equal to
# Load the first document that have the model_type_key's field equal to
# the name of the current class.
#
# ==== Returns

View file

@ -97,7 +97,7 @@ module CouchRest
# ==== Returns
# a document instance
def create_from_database(doc = {})
base = (doc['couchrest-type'].blank? || doc['couchrest-type'] == self.to_s) ? self : doc['couchrest-type'].constantize
base = (doc[model_type_key].blank? || doc[model_type_key] == self.to_s) ? self : doc[model_type_key].constantize
base.new(doc, :directly_set_attributes => true)
end

View file

@ -23,7 +23,7 @@ module CouchRest
# view_by :tags,
# :map =>
# "function(doc) {
# if (doc['couchrest-type'] == 'Post' && doc.tags) {
# if (doc['model'] == 'Post' && doc.tags) {
# doc.tags.forEach(function(tag){
# emit(doc.tag, 1);
# });
@ -39,7 +39,7 @@ module CouchRest
# function:
#
# function(doc) {
# if (doc['couchrest-type'] == 'Post' && doc.date) {
# if (doc['model'] == 'Post' && doc.date) {
# emit(doc.date, null);
# }
# }
@ -77,7 +77,7 @@ module CouchRest
ducktype = opts.delete(:ducktype)
unless ducktype || opts[:map]
opts[:guards] ||= []
opts[:guards].push "(doc['couchrest-type'] == '#{self.to_s}')"
opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"
end
keys.push opts
design_doc.view_by(*keys)