Class: SpecialOffer
- Inherits:
-
Ruleby::Rulebook
- Object
- Ruleby::Rulebook
- SpecialOffer
- Defined in:
- lib/special_offer.rb
Instance Attribute Summary collapse
-
#cart ⇒ Object
writeonly
Sets the attribute cart.
-
#current_rules ⇒ Object
writeonly
Sets the attribute current_rules.
-
#free_product_ids ⇒ Object
writeonly
Sets the attribute free_product_ids.
-
#selected_products ⇒ Object
writeonly
Sets the attribute selected_products.
Instance Method Summary collapse
- #apply_discount_on_product(special_offer, product) ⇒ Object
- #eval_rule(special_offer) ⇒ Object
- #rule_preview(rule_preview) ⇒ Object
- #rules ⇒ Object
Instance Attribute Details
#cart=(value) ⇒ Object (writeonly)
Sets the attribute cart
3 4 5 |
# File 'lib/special_offer.rb', line 3 def cart=(value) @cart = value end |
#current_rules=(value) ⇒ Object (writeonly)
Sets the attribute current_rules
3 4 5 |
# File 'lib/special_offer.rb', line 3 def current_rules=(value) @current_rules = value end |
#free_product_ids=(value) ⇒ Object (writeonly)
Sets the attribute free_product_ids
3 4 5 |
# File 'lib/special_offer.rb', line 3 def free_product_ids=(value) @free_product_ids = value end |
#selected_products=(value) ⇒ Object (writeonly)
Sets the attribute selected_products
3 4 5 |
# File 'lib/special_offer.rb', line 3 def selected_products=(value) @selected_products = value end |
Instance Method Details
#apply_discount_on_product(special_offer, product) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/special_offer.rb', line 52 def apply_discount_on_product(special_offer,product) return unless special_offer and product if rate = special_offer.variables[:discount] = { :voucher_discount => false, :special_offer_discount => false } discount_price = special_offer.variables[:fixed_discount] ? rate : (product.price() * rate).to_f / 100 product.special_offer_discount_price = discount_price special_offer.variables[:fixed_discount] ? product.special_offer_discount = "-#{rate}" : product.special_offer_discount = "-#{rate}%" end end |
#eval_rule(special_offer) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/special_offer.rb', line 23 def eval_rule(special_offer) rule eval(special_offer.conditions) do |context| product = context[:product] if @cart # Free products --cart and product_in_cart special_offer.variables[:product_ids].each do |product_id| @free_product_ids << product_id end if special_offer.variables[:product_ids] # Discount cart total price --cart if rate = special_offer.variables[:cart_discount] = {:cart_voucher_discount => false, :cart_special_offer_discount => false, :product_voucher_discount => false, :product_special_offer_discount => false} discount_price = special_offer.variables[:fixed_discount] ? rate : (@cart.total() * rate).to_f / 100 @cart.special_offer_discount_price = discount_price special_offer.variables[:fixed_discount]? @cart.special_offer_discount = "-#{rate}" : @cart.special_offer_discount = "-#{rate}%" end # Free delivery @cart.free_shipping = true if special_offer.variables[:shipping] end # Product in Shop apply_discount_on_product(special_offer,product) @selected_products << product if @selected_products.kind_of?(Array) or @selected_products.kind_of?(Set) retract product if product end end |
#rule_preview(rule_preview) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/special_offer.rb', line 13 def rule_preview(rule_preview) if rule_preview.is_a?(SpecialOfferRule) rule eval(rule_preview.conditions) do |context| product = context[:product] @selected_products << product.id retract product end end end |
#rules ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/special_offer.rb', line 5 def rules #OPTIMIZE @current_rules ||= SpecialOfferRule.find_all_by_active(true) @current_rules.each do |special_offer| eval_rule(special_offer) end end |