Sha256: bee83c8f91cd6da9a91513fd6578d8ec687bb5896382f14d7020ba63b4ffdca1

Contents?: true

Size: 1.01 KB

Versions: 28

Compression:

Stored size: 1.01 KB

Contents

module Valuation

  #
  # Anything with a :lifetime_donations column and has_many donations can use this module.
  #
  module LifetimeDonations
    extend ActiveSupport::Concern
    
    #
    # 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
    #  
    def lifetime_orders
      orders
    end
    
    #
    # 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 
    #
    def calculate_lifetime_donations
      self.lifetime_donations = 0
      lifetime_orders.each do |o|
        o.donations.each { |i| self.lifetime_donations = self.lifetime_donations + i.total_price}
      end
      save
      self.lifetime_donations
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
artfully_ose-1.2.0.pre.7 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre.6 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre.5 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre.4 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre.3 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre.2 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre.1 app/models/valuation/lifetime_donations.rb
artfully_ose-1.2.0.pre app/models/valuation/lifetime_donations.rb