Class: BuyOneGetOneFreeDiscountType

Inherits:
DiscountType show all
Defined in:
app/models/discounts/buy_one_get_one_free_discount_type.rb

Instance Attribute Summary

Attributes inherited from DiscountType

#properties

Instance Method Summary collapse

Methods inherited from DiscountType

discount_type, #eligible_tickets, fee, #initialize, #tickets, types

Constructor Details

This class inherits a constructor from DiscountType

Instance Method Details

#apply_discount_to_cartObject



4
5
6
7
8
9
10
11
12
# File 'app/models/discounts/buy_one_get_one_free_discount_type.rb', line 4

def apply_discount_to_cart
  eligible_tickets.each do |ticket|
    ticket.update_column(:discount_id, @discount.id) unless ticket == eligible_tickets.last && eligible_tickets.count.odd?
  end
  every_other_ticket.each do |ticket|
    ticket.update_column(:cart_price, 0)
  end
  FeeCalculator.apply(FeeStrategy.new).to(@discount.cart)
end

#every_other_ticketObject



14
15
16
# File 'app/models/discounts/buy_one_get_one_free_discount_type.rb', line 14

def every_other_ticket
  eligible_tickets.values_at(* eligible_tickets.each_index.select {|i| i.odd?})
end

#to_sObject



22
23
24
# File 'app/models/discounts/buy_one_get_one_free_discount_type.rb', line 22

def to_s
  "Buy one, get one free"
end

#validateObject



18
19
20
# File 'app/models/discounts/buy_one_get_one_free_discount_type.rb', line 18

def validate
  # Nothing to do here.
end