module Discounter

We expect the entity to have a price attribute.

Constants

VERSION

Attributes

price[RW]

Public Class Methods

evaluate_discount(user, join) click to toggle source

User is the user that may have a discount for this entity. Join is the entity in which there is a setting for yes this user has a discount associated to them. We expect the join model to have a discount_tier column that is set to either. So we expect it to be set up this way:

  • Model Join - Attributes: user_id, discount_tier (optional column = entity_id (for discounting on)).

nil if there is no discount, or the discount tier if there is indeed one.

# File lib/discounter.rb, line 27
def evaluate_discount(user, join)
  Join.find_all_by_user_id(user.id).each(&method(:verify_discount_applicability))
end
initialize(entity) click to toggle source

Entity is the entity in which the price to be set is on.

# File lib/discounter.rb, line 11
def initialize(entity)
  @entity ||= entity
  @price  ||= entity.try(:price)
end
initialize_discount() click to toggle source
# File lib/discounter.rb, line 16
def initialize_discount 
  # ?
end

Private Class Methods

verify_discount_applicability(row) click to toggle source

This method is passed a row from the join table to verify the discount on.

# File lib/discounter.rb, line 33
def verify_discount_applicability(row)
  if row.try(:discount).nil?
    # There is no discount set for this user in the system
    @price
  else
    # There is a discount tier applied.  Let's do the calculation
    # And return out the price for the Interface.
    @price = (@price.to_f * row.discount.to_f).to_i
  end
end