Class: Sale

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/forge/app/models/sale.rb

Constant Summary collapse

@@sale_types =

Define the types of sale calculation

{:fixed => "All products in this sale are set to the same price. (e.g. Sale = $5.50: Product A = $5.50, Product B = $5.50",
:percentage => "All products in this sale are reduced by a percentage of their regular price. (e.g. Sale = 10%: Product A = $20 - $20/10 = $18",
:reduction => "All products are reduced by a set price. (e.g. Sale = $3: Product A = $20 - $3 = $17"}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sale_typesObject



15
16
17
# File 'lib/forge/app/models/sale.rb', line 15

def self.sale_types
  @@sale_types
end

Instance Method Details

#fixed(opts = {}) ⇒ Object

Calculate sale price based on product options and sale rules



20
21
22
# File 'lib/forge/app/models/sale.rb', line 20

def fixed(opts = {})
  self.value
end

#percentage(opts = {}) ⇒ Object



24
25
26
# File 'lib/forge/app/models/sale.rb', line 24

def percentage(opts = {})
  opts[:price] - opts[:price]/value
end

#price(opts = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/forge/app/models/sale.rb', line 32

def price(opts = {})
  # Call the relevant sale calculator
  # It should be enough to call this from the product with product.sale.price({:price => product.price})
  m = self
  fp = m.method(m.sale_type.intern) # 'Function' Pointer
  fp.call(opts)
end

#product_idsObject



40
41
42
# File 'lib/forge/app/models/sale.rb', line 40

def product_ids
  self.products.map { |p| p.id }
end

#reduction(opts = {}) ⇒ Object



28
29
30
# File 'lib/forge/app/models/sale.rb', line 28

def reduction(opts = {})
  opts[:price] - value
end