Module: Valuation::LifetimeDonations

Extended by:
ActiveSupport::Concern
Included in:
Person
Defined in:
app/models/valuation/lifetime_donations.rb

Overview

Anything with a :lifetime_donations column and has_many donations can use this module.

Instance Method Summary collapse

Instance Method Details

#calculate_lifetime_donationsObject

Calculate the lifetime donations of this model by summing the price of all donations attached to orders attached to this person. Save the donations in lifetime_donations. Return the total

This could be done (probably faster) in a single sql SELECT SUM suery



26
27
28
29
30
31
32
33
# File 'app/models/valuation/lifetime_donations.rb', line 26

def calculate_lifetime_donations
  self.lifetime_donations = 0
  lifetime_orders.each do |o|
    o.donations.each { |i| self.lifetime_donations = self.lifetime_donations + i.price}
  end
  save
  lifetime_donations
end

#lifetime_ordersObject

Includers can define a method called lifetime_donations which will override this method.

lifetime_donations should return the donations that this model wants to include in the calculation



15
16
17
# File 'app/models/valuation/lifetime_donations.rb', line 15

def lifetime_orders
  orders
end