-
-
Save seancribbs/2408069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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