2011-02-09 21:21:03 +01:00
|
|
|
module CouchRest
|
|
|
|
module Model
|
|
|
|
# :nodoc: Because I like inventing words
|
|
|
|
module Proxyable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2011-04-29 21:40:36 +02:00
|
|
|
def proxy_database
|
|
|
|
raise StandardError, "Please set the #proxy_database_method" if self.class.proxy_database_method.nil?
|
|
|
|
@proxy_database ||= self.class.prepare_database(self.send(self.class.proxy_database_method))
|
|
|
|
end
|
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
module ClassMethods
|
|
|
|
|
2011-04-29 21:40:36 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
# Define a collection that will use the base model for the database connection
|
|
|
|
# details.
|
2011-04-04 01:10:31 +02:00
|
|
|
def proxy_for(assoc_name, options = {})
|
2011-02-09 21:21:03 +01:00
|
|
|
db_method = options[:database_method] || "proxy_database"
|
2011-04-04 01:10:31 +02:00
|
|
|
options[:class_name] ||= assoc_name.to_s.singularize.camelize
|
2011-02-09 21:21:03 +01:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
2011-04-04 01:10:31 +02:00
|
|
|
def #{assoc_name}
|
|
|
|
@#{assoc_name} ||= CouchRest::Model::Proxyable::ModelProxy.new(::#{options[:class_name]}, self, self.class.to_s.underscore, #{db_method})
|
2011-02-09 21:21:03 +01:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2011-04-29 21:40:36 +02:00
|
|
|
# Tell this model which other model to use a base for the database
|
|
|
|
# connection to use.
|
2011-02-09 21:21:03 +01:00
|
|
|
def proxied_by(model_name, options = {})
|
2011-04-04 01:10:31 +02:00
|
|
|
raise "Model can only be proxied once or ##{model_name} already defined" if method_defined?(model_name) || !proxy_owner_method.nil?
|
|
|
|
self.proxy_owner_method = model_name
|
2011-04-05 20:41:24 +02:00
|
|
|
attr_accessor :model_proxy
|
2011-02-09 21:21:03 +01:00
|
|
|
attr_accessor model_name
|
2011-04-29 21:40:36 +02:00
|
|
|
overwrite_database_reader(model_name)
|
2011-02-09 21:21:03 +01:00
|
|
|
end
|
2011-04-05 20:41:24 +02:00
|
|
|
|
|
|
|
# Define an a class variable accessor ready to be inherited and unique
|
|
|
|
# for each Class using the base.
|
|
|
|
# Perhaps there is a shorter way of writing this.
|
|
|
|
def proxy_owner_method=(name); @proxy_owner_method = name; end
|
|
|
|
def proxy_owner_method; @proxy_owner_method; end
|
|
|
|
|
2011-04-29 21:40:36 +02:00
|
|
|
# Define the name of a method to call to determine the name of
|
|
|
|
# the database to use as a proxy.
|
2011-04-30 01:04:04 +02:00
|
|
|
def proxy_database_method(name = nil)
|
|
|
|
@proxy_database_method = name if name
|
|
|
|
@proxy_database_method
|
|
|
|
end
|
2011-04-29 21:40:36 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-04-29 23:06:31 +02:00
|
|
|
# Ensure that no attempt is made to autoload a database connection
|
|
|
|
# by overwriting it to provide a basic accessor.
|
2011-04-29 21:40:36 +02:00
|
|
|
def overwrite_database_reader(model_name)
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
2011-04-30 01:04:04 +02:00
|
|
|
def self.database
|
|
|
|
raise StandardError, "#{self.to_s} database must be accessed via '#{model_name}' proxy"
|
|
|
|
end
|
2011-04-29 21:40:36 +02:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
class ModelProxy
|
|
|
|
|
|
|
|
attr_reader :model, :owner, :owner_name, :database
|
|
|
|
|
|
|
|
def initialize(model, owner, owner_name, database)
|
|
|
|
@model = model
|
|
|
|
@owner = owner
|
|
|
|
@owner_name = owner_name
|
|
|
|
@database = database
|
|
|
|
end
|
|
|
|
|
|
|
|
# Base
|
|
|
|
def new(*args)
|
|
|
|
proxy_update(model.new(*args))
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_from_database(doc = {})
|
|
|
|
proxy_update(model.build_from_database(doc))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def method_missing(m, *args, &block)
|
|
|
|
if has_view?(m)
|
|
|
|
if model.respond_to?(m)
|
|
|
|
return model.send(m, *args).proxy(self)
|
|
|
|
else
|
|
|
|
query = args.shift || {}
|
|
|
|
return view(m, query, *args, &block)
|
|
|
|
end
|
|
|
|
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-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
# DocumentQueries
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def all(opts = {}, &block)
|
|
|
|
proxy_update_all(@model.all({:database => @database}.merge(opts), &block))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def count(opts = {})
|
|
|
|
@model.count({:database => @database}.merge(opts))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def first(opts = {})
|
|
|
|
proxy_update(@model.first({:database => @database}.merge(opts)))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def last(opts = {})
|
|
|
|
proxy_update(@model.last({:database => @database}.merge(opts)))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def get(id)
|
|
|
|
proxy_update(@model.get(id, @database))
|
|
|
|
end
|
|
|
|
alias :find :get
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
# Views
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def has_view?(view)
|
|
|
|
@model.has_view?(view)
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_by(*args)
|
|
|
|
@model.view_by(*args)
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def view(name, query={}, &block)
|
|
|
|
proxy_update_all(@model.view(name, {:database => @database}.merge(query), &block))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
def first_from_view(name, *args)
|
|
|
|
# add to first hash available, or add to end
|
|
|
|
(args.last.is_a?(Hash) ? args.last : (args << {}).last)[:database] = @database
|
|
|
|
proxy_update(@model.first_from_view(name, *args))
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-09 21:21:03 +01:00
|
|
|
# DesignDoc
|
|
|
|
def design_doc
|
|
|
|
@model.design_doc
|
|
|
|
end
|
2011-04-17 02:46:33 +02:00
|
|
|
|
2011-02-13 17:45:59 +01:00
|
|
|
def save_design_doc(db = nil)
|
|
|
|
@model.save_design_doc(db || @database)
|
2011-02-09 21:21:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Update the document's proxy details, specifically, the fields that
|
|
|
|
# link back to the original document.
|
|
|
|
def proxy_update(doc)
|
2011-04-05 20:41:24 +02:00
|
|
|
if doc && doc.is_a?(model)
|
|
|
|
doc.database = @database
|
|
|
|
doc.model_proxy = self
|
|
|
|
doc.send("#{owner_name}=", owner)
|
2011-02-09 21:21:03 +01:00
|
|
|
end
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_update_all(docs)
|
|
|
|
docs.each do |doc|
|
|
|
|
proxy_update(doc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|