Browse files

Refactor average_items

  • Loading branch information...
1 parent b5ce360 commit 68f50e933c089d23abaf41415a27d142791920b1 @brynary committed May 7, 2012
Showing with 3 additions and 10 deletions.
  1. +3 −10 lib/sales_engine/invoice.rb
View
13 lib/sales_engine/invoice.rb
@@ -65,16 +65,9 @@ def self.with_date(date)
end
def self.average_items(date = nil)
- if date
- di = all.select do |i|
- next unless i.successful_transaction
- i.created_at.strftime("%y%m%d") == date.strftime("%y%m%d")
- end
- ti = di.map(&:num_items).inject(:+)
- BigDecimal((ti / di.size.to_f).round(2).to_s) rescue BigDecimal("0")
- else
- BigDecimal((all.map(&:num_items).inject(:+) / suc_size).round(2).to_s)
- end
+ invoices = date ? with_date(date) : all
+ tr = invoices.map(&:num_items).inject(:+)
+ BigDecimal(tr / invoices.size.to_f).round(2)
end
def self.suc_size

0 comments on commit 68f50e9

Please sign in to comment.