Skip to content

Instantly share code, notes, and snippets.

@seancribbs
Forked from stevenbristol/gist:2406727
Created April 17, 2012 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seancribbs/2408069 to your computer and use it in GitHub Desktop.
Save seancribbs/2408069 to your computer and use it in GitHub Desktop.
module InvoiceFinders
def paid
klass.find_by_index(:business_paid, "#{owner.key}-true")
end
def unpaid
klass.find_by_index(:business_paid, "#{owner.key}-false")
end
end
class Business
many :invoices,
using: :index, # [SDC] Not available yet, might co-opt "reference" type
extend: InvoiceFinders
end
class Invoice
property :ref, String
property :total, BigDecimal
property :paid_total, BigDecimal
property :business_key, String
one :business, using: :stored_key
index :business_ref, String do
[business_key, ref].join '-'
end
index :business_paid, String do
"#{business_key}-#{paid_total >= total}"
end
end
b = business.find(1)
# [SDC] This might work if the yet-to-be-implemented index relationship type does it properly
b.invoices.count # 3
# [SDC] These are currently inefficient because they will load all related documents to do the count
b.invoices.paid.count #2
b.invoices.unpaid.count #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment