Module: ESA::Associations::AmountsExtension
- Defined in:
- app/models/esa/associations/amounts_extension.rb
Overview
Association extension for has_many :amounts relations. Internal.
Instance Method Summary collapse
-
#balance ⇒ Object
Returns a sum of the referenced Amount objects.
-
#iterated_balance ⇒ Object
Returns a sum of the referenced Amount objects.
-
#iterated_total ⇒ Object
Returns a sum of the referenced Amount objects.
-
#total ⇒ Object
Returns a total sum of the referenced Amount objects.
Instance Method Details
#balance ⇒ Object
Returns a sum of the referenced Amount objects.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/esa/associations/amounts_extension.rb', line 23 def balance sums = group('esa_amounts.amount is null'). group('esa_amounts.type'). sum(:amount) signed = sums.map do |s| amount_is_nil, amount_type, amount = s.flatten amount_type = amount_type.demodulize.downcase if amount_type == "debit" amount elsif amount_type == "credit" - amount else nil end end if signed.all? return signed.inject(BigDecimal(0)){|x,y| x + y} else return nil end end |
#iterated_balance ⇒ Object
Returns a sum of the referenced Amount objects.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/esa/associations/amounts_extension.rb', line 60 def iterated_balance amounts = map do |a| if a.is_debit? a.amount elsif a.is_credit? and not a.amount.nil? - a.amount else nil end end if amounts.all? amounts.inject(BigDecimal(0)){|x,y| x + y} else return nil end end |
#iterated_total ⇒ Object
Returns a sum of the referenced Amount objects.
49 50 51 52 53 54 55 56 57 |
# File 'app/models/esa/associations/amounts_extension.rb', line 49 def iterated_total amounts = map(&:amount) if amounts.all? amounts.inject(BigDecimal(0)){|x,y| x + y} else return nil end end |
#total ⇒ Object
Returns a total sum of the referenced Amount objects
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/models/esa/associations/amounts_extension.rb', line 6 def total sums = group('esa_amounts.amount is null'). sum(:amount) checked = sums.map do |s| amount_is_nil, amount = s.flatten amount end if checked.all? return checked.inject(BigDecimal(0)){|x,y| x + y} else return nil end end |