Class: Promotional::Rule::ItemQuantityPriceRule

Inherits:
PromotionalRule show all
Defined in:
lib/wunder/promotional/rule/item_quantity_price_rule.rb

Constant Summary

Constants included from RuleValidations

RuleValidations::DISCOUNT_TYPES

Instance Attribute Summary collapse

Attributes inherited from PromotionalRule

#discount_type, #label, #on_item, #rule

Instance Method Summary collapse

Methods inherited from PromotionalRule

#calculate_total

Methods included from RuleValidations

#boolean?, #check_discount_type, #either_should_be_present, #should_be_a_boolean, #should_be_a_number, #should_be_more_than, #should_be_present

Constructor Details

#initialize(minimum_quantity, value, no_validate = false) ⇒ ItemQuantityPriceRule

Returns a new instance of ItemQuantityPriceRule.



6
7
8
9
10
11
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 6

def initialize(minimum_quantity, value, no_validate = false)
  @minimum_quantity = minimum_quantity
  @value = value

  validate if no_validate == false
end

Instance Attribute Details

#minimum_quantityObject (readonly)

Returns the value of attribute minimum_quantity.



4
5
6
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 4

def minimum_quantity
  @minimum_quantity
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 4

def value
  @value
end

Instance Method Details

#adjustable?(_total) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 17

def adjustable?(_total)
  false
end

#calculate_discounted_price(basket_item, discount_type) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 21

def calculate_discounted_price(basket_item, discount_type)
  if discount_type == "percentage"
    discount = compute_discount(basket_item)
    basket_item.product.price = discount
  elsif discount_type == "flat_rate"
    basket_item.product.price = value
  end
end

#calculate_total_discounted_price(total, discount_type) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 30

def calculate_total_discounted_price(total, discount_type)
  if discount_type == "percentage"
    discount_price = total - ((total * value) / 100)
  elsif discount_type == "flat_rate"
    discount_price = total
  end

  discount_price
end

#eligible?(item) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 13

def eligible?(item)
  item.quantity >= minimum_quantity
end

#validateObject



40
41
42
43
44
45
46
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 40

def validate
  should_be_present("ItemQuantityPriceRule::Value", value)
  should_be_a_number("ItemQuantityPriceRule::MinimumQuantity",
                     minimum_quantity)
  should_be_more_than("ItemQuantityPriceRule::MinQuantity",
                      minimum_quantity, 1)
end