Sha256: 8d5741d9da79d5242bb357852c74444216ad3ebfa00f05e9e4d2285fd2b90262

Contents?: true

Size: 962 Bytes

Versions: 5

Compression:

Stored size: 962 Bytes

Contents

module Valuation

  #
  # Anything with a :lifetime_value column and has_many orders can use this module.
  #
  module LifetimeValue
    extend ActiveSupport::Concern
    
    #
    # Includers can define a method called lifetime_orders which
    # will override this method.
    #
    # lifetime_orders should return the orders that this model wants to include in the calculation
    #  
    def lifetime_orders
      orders
    end
    
    #
    # Calculate the lifetime value of this model by summing the price of all items 
    # attached to orders attached to this person.  Save the value in lifetime_value.
    # Return the value
    #
    # This could be done (probably faster) in a single sql SELECT SUM suery 
    #
    def calculate_lifetime_value
      self.lifetime_value = 0
      lifetime_orders.each do |o|
        o.items.each { |i| self.lifetime_value = self.lifetime_value + i.price}
      end
      save
      lifetime_value
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
artfully_ose-1.1.0 app/models/valuation/lifetime_value.rb
artfully_ose-1.1.0.rc2 app/models/valuation/lifetime_value.rb
artfully_ose-1.1.0.rc1 app/models/valuation/lifetime_value.rb
artfully_ose-1.0.0.rc4 app/models/valuation/lifetime_value.rb
artfully_ose-1.0.0.rc3 app/models/valuation/lifetime_value.rb