couchrest / couchrest_model

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

Implement proxy for factory methods #210

Closed ellneal closed 8 years ago

ellneal commented 8 years ago

Allow the proxy to forward factory methods via method_missing.

class Invoice < CouchRest::Model::Base
  proxied_by :company
  property :date, Time

  def self.from_timestamp(proxy, timestamp)
    instance = proxy.new
    instance.date = Time.at(timestamp)
    instance.save
  end
end

# allows the proxy to use the from_timestamp method
company = Company.get("my_company_id")
new_invoice = company.invoices.from_timestamp(Time.now.to_id)
ellneal commented 8 years ago

The CI failure appears to be unrelated to the changes made.

samlown commented 8 years ago

Thanks for this! I'm not a 100% fan of method missing, but I can't see any issues here. Merged!

ellneal commented 8 years ago

I don't really like method missing either, but I just thought that company.invoices.from_timestamp(timestamp) was much prettier than Invoice.from_timestamp(company, timestamp) and couldn't think of a better solution. Thanks for merging :+1: