Class: Caboose::VariantLimit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/variant_limit.rb

Instance Method Summary collapse

Instance Method Details

#max_quantity(invoice) ⇒ Object


21
22
23
24
# File 'app/models/caboose/variant_limit.rb', line 21

def max_quantity(invoice)
  return self.max_quantity_value if self.max_quantity_func.nil? || self.max_quantity_func.strip.length == 0      
  return eval(self.max_quantity_func)            
end

#min_quantity(invoice) ⇒ Object


16
17
18
19
# File 'app/models/caboose/variant_limit.rb', line 16

def min_quantity(invoice)
  return self.min_quantity_value if self.min_quantity_func.nil? || self.min_quantity_func.strip.length == 0      
  return eval(self.min_quantity_func)            
end

#no_purchases_allowed(invoice) ⇒ Object


43
44
45
# File 'app/models/caboose/variant_limit.rb', line 43

def no_purchases_allowed(invoice)
  return self.min_quantity(invoice) == 0 && self.max_quantity(invoice) == 0
end

#qty_too_high(qty, invoice) ⇒ Object


57
58
59
# File 'app/models/caboose/variant_limit.rb', line 57

def qty_too_high(qty, invoice)
  return self.max_quantity(invoice) && qty > self.max_quantity(invoice)
end

#qty_too_low(qty, invoice) ⇒ Object


53
54
55
# File 'app/models/caboose/variant_limit.rb', line 53

def qty_too_low(qty, invoice)
  return self.min_quantity(invoice) && qty < self.min_quantity(invoice)
end

#qty_within_range(qty, invoice) ⇒ Object


47
48
49
50
51
# File 'app/models/caboose/variant_limit.rb', line 47

def qty_within_range(qty, invoice)
  return false if self.min_quantity(invoice) && qty < self.min_quantity(invoice)
  return false if self.max_quantity(invoice) && qty > self.max_quantity(invoice)
  return true
end

#quantity_message(invoice) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/caboose/variant_limit.rb', line 26

def quantity_message(invoice)
  if self.min_quantity(invoice) == 0 && self.max_quantity(invoice) == 0
    return "You are not allowed to purchase this item."
  end
  if self.max_quantity(invoice)
    if self.min_quantity(invoice) && self.min_quantity(invoice) > 0
      return "You are allowed to purchase between #{self.min_quantity(invoice)} and #{self.max_quantity(invoice)} of this item."
    else
      return "You are allowed to purchase up to #{self.max_quantity(invoice)} of this item."
    end
  end
  if self.min_quantity(invoice) && self.min_quantity(invoice) > 0
    return "You must purchase at least #{self.min_quantity(invoice)} of this item." 
  end
  return nil
end