couchrest / couchrest_model

Doing the simple stuff so you don't have to
Apache License 2.0
304 stars 116 forks source link

Support proxying to multiple databases #206

Closed ellneal closed 8 years ago

ellneal commented 8 years ago

The current implementation only allows one proxy database per parent class.

class Company < CouchRest::Model::Base
  property :slug
  proxy_database_method :slug

  proxy_for :invoices
  proxy_for :estimates
end

class Invoice < CouchRest::Model::Base
  proxied_by :company
end

class Estimate < CouchRest::Model::Base
  proxied_by :company
end

In this example, both invoices and estimates would be saved in the same database ("#{a_company.slug}"). This PR adds two options to allow these classes to be saved in derived databases.

class Company < CouchRest::Model::Base
  property :slug
  proxy_database_method :slug

  proxy_for :invoices, use_suffix: true
  proxy_for :estimates, database_suffix: "documents"
end

class Invoice < CouchRest::Model::Base
  proxied_by :company
end

class Estimate < CouchRest::Model::Base
  proxied_by :company
end

Here, invoices will be saved in "#{a_company.slug}_invoices" and estimates will be saved in "#{a_company.slug}_documents".

samlown commented 8 years ago

Again, another awesome feature! I've been wanting this myself for a while!

Previously I'd handle this by setting the :database_method (example below) but your solution is much more elegant.

  proxy_for :state_intervals, database_method: :state_intervals_proxy_database
  def state_intervals_proxy_database
    @state_intervals_proxy_database ||= self.class.prepare_database("#{slug}_state_intervals")
  end