You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @mbulat
Currently the balance method in AmountsExtension supports two cases:
All - No date hash provided; return sum of all amounts
Closed Range - Hash provided with from_date and to_date; return sum of all amounts between these dates.
I believe that a reasonable argument could be made for provision of two additional cases, based on the frequency with which to_date = Date.today is used and eliminating the need to find the earliest entry date to set a from_date:
Open Range (after) - Hash provided with from_date; return sum of all amounts after this date (up to today).
Open Range (before) - Hash provided with to_date; return sum of all amounts before this date (down to the earliest entry).
Current balance method:
def balance(hash={})
if hash[:from_date] && hash[:to_date]
from_date = hash[:from_date].kind_of?(Date) ? hash[:from_date] : Date.parse(hash[:from_date])
to_date = hash[:to_date].kind_of?(Date) ? hash[:to_date] : Date.parse(hash[:to_date])
includes(:entry).where('plutus_entries.date' => from_date..to_date).sum(:amount)
else
sum(:amount)
end
end
Revised method:
def balance(hash={})
if hash[:from_date] && hash[:to_date]
from_date = hash[:from_date].kind_of?(Date) ? hash[:from_date] : Date.parse(hash[:from_date])
to_date = hash[:to_date].kind_of?(Date) ? hash[:to_date] : Date.parse(hash[:to_date])
includes(:entry).where('plutus_entries.date' => from_date..to_date).sum(:amount)
elsif hash[:from_date]
# if only :from_date provided default assumes all amounts requested after that date
from_date = hash[:from_date].kind_of?(Date) ? hash[:from_date] : Date.parse(hash[:from_date])
includes(:entry).where('plutus_entries.date >= ?, from_date).sum(:amount)
elsif hash[:to_date]
# if only :to_date provided default assumes all amounts requested before that date
to_date = hash[:to_date].kind_of?(Date) ? hash[:to_date] : Date.parse(hash[:to_date])
includes(:entry).where('plutus_entries.date <= ?, to_date).sum(:amount)
else
sum(:amount)
end
end
Some class methods might make this neater
def balance(hash={})
if hash[:from_date] && hash[:to_date]
from_date = hash[:from_date].kind_of?(Date) ? hash[:from_date] : Date.parse(hash[:from_date])
to_date = hash[:to_date].kind_of?(Date) ? hash[:to_date] : Date.parse(hash[:to_date])
between(from_date, to_date)
elsif hash[:from_date]
# if only :from_date provided default assumes all amounts requested after that date
from_date = hash[:from_date].kind_of?(Date) ? hash[:from_date] : Date.parse(hash[:from_date])
after(from_date)
elsif hash[:to_date]
# if only :to_date provided default assumes all amounts requested before that date
to_date = hash[:to_date].kind_of?(Date) ? hash[:to_date] : Date.parse(hash[:to_date])
before(to_date)
else
sum(:amount)
end
end
def self.between(from_date, to_date)
includes(:entry).where('plutus_entries.date' => from_date..to_date).sum(:amount)
end
def self.after(from_date, to_date)
includes(:entry).where('plutus_entries.date >= ?, from_date).sum(:amount)
end
def self.before(to_date)
includes(:entry).where('plutus_entries.date <= ?, to_date).sum(:amount)
end
I don't see any impact to existing installations either as the existing if hash[:from_date] && hash[:to_date] and else catch-all remain unchanged.
Let me know if you'd like a PR.
The text was updated successfully, but these errors were encountered:
Hi @mbulat
Currently the balance method in AmountsExtension supports two cases:
I believe that a reasonable argument could be made for provision of two additional cases, based on the frequency with which
to_date = Date.today
is used and eliminating the need to find the earliest entry date to set afrom_date
:Current balance method:
Revised method:
Some class methods might make this neater
I don't see any impact to existing installations either as the existing
if hash[:from_date] && hash[:to_date]
andelse
catch-all remain unchanged.Let me know if you'd like a PR.
The text was updated successfully, but these errors were encountered: