Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Forked from jimmybaker/pseudo code
Created February 24, 2010 19:46
Show Gist options
  • Save rubysolo/313771 to your computer and use it in GitHub Desktop.
Save rubysolo/313771 to your computer and use it in GitHub Desktop.
class Discount
# name
# code
# effective_at
# ends_at
# type # (DollarOff, PercentOff, FreeShipping..)
# promotion?
# customer_exclusive?
# amount
# minimum_total_requirement
has_many :assignments, :class_name => "DiscountAssignment"
has_many :users, :through => :assignments
has_many :sales_orders, :through => :assignments
has_many :filters
named_scope :promotions, :conditions => { :promotion => true }
named_scope :customer_credits, :conditions => { :promotion => false }
named_scope :global, :conditions => { :customer_exclusive => false }
def filter(order)
order.details.select do |d|
filters.all? {|f| f.is_applicable? d }
end
end
def applicable?(order)
active? &&
order.total >= minimum_total_requirement &&
# check dates, yadda, yadda
filter(order).any? # do ANY details pass ALL the filters?
end
end
DiscountAssignment
promo_id
user_id
sales_order_id
class DiscountFilter < ActiveRecord::Base
# criterion
# interface definition
def is_applicable?(order_detail)
raise "Implement this in your subclass"
end
end
class ColorDiscountFilter < ActiveRecord::Base
def is_applicable?(order_detail)
order_detail.product.color == self.criterion
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment