class Promotional::Rule::ItemQuantityPriceRule

Attributes

minimum_quantity[R]
value[R]

Public Class Methods

new(minimum_quantity, value, no_validate = false) click to toggle source
# 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

Public Instance Methods

adjustable?(_total) click to toggle source
# File lib/wunder/promotional/rule/item_quantity_price_rule.rb, line 17
def adjustable?(_total)
  false
end
calculate_discounted_price(basket_item, discount_type) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/wunder/promotional/rule/item_quantity_price_rule.rb, line 13
def eligible?(item)
  item.quantity >= minimum_quantity
end
validate() click to toggle source
# 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

Private Instance Methods

compute_discount(basket_item) click to toggle source
# File lib/wunder/promotional/rule/item_quantity_price_rule.rb, line 50
def compute_discount(basket_item)
  discount = (basket_item.product.price * value) / 100
  basket_item.product.price - discount
end