Module: Valuation::LifetimeValue

Extended by:
ActiveSupport::Concern
Included in:
Organization, Person
Defined in:
app/models/valuation/lifetime_value.rb

Overview

Anything with a :lifetime_value column and has_many orders can use this module.

Instance Method Summary collapse

Instance Method Details

#calculate_lifetime_valueObject

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



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

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

#lifetime_ordersObject

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



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

def lifetime_orders
  orders
end