Quick example for proxy

This commit is contained in:
Sam Lown 2011-03-13 20:42:53 +01:00
parent 0e51dcfb9a
commit 3a3fc3c41d

View file

@ -499,6 +499,48 @@ A really interesting use of `:proxy` and `:view` together could be where you'd l
Pretty cool! Pretty cool!
## Proxy Support
CouchDB makes it really easy to create databases on the fly, so easy in fact that it is perfectly
feasable to have one database per user or per company or per whatever makes sense to split into
its own individual database. CouchRest Model now makes it really easy to support this scenario
using the proxy methods. Here's a quick example:
# Define a master company class, its children should be in their own DB
class Company < CouchRest::Model::Base
use_database COUCHDB_DATABASE
property :name
property :slug
proxy_for :invoices
def proxy_database
@proxy_database ||= COUCHDB_SERVER.database!("project_#{slug}")
end
end
# Invoices belong to a company
class Invoice < CouchRest::Model::Base
property :date
property :total
proxied_by :company
design do
view :by_date
end
end
By setting up our models like this, the invoices should be accessed via a company object:
company = Company.first
company.invoices.new # build a new invoice
company.invoices.by_date.first # find company's first invoice by date
Internally, all requests for invoices are passed through a model proxy. Aside from the
basic methods and views, it also ensures that some of the more complex queries are supported
such as validating for uniqueness and associations.
## Configuration ## Configuration