Class: DollarsOffTicketsDiscountType

Inherits:
DiscountType show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/models/discounts/dollars_off_tickets_discount_type.rb

Instance Attribute Summary

Attributes inherited from DiscountType

#properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DiscountType

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

Constructor Details

This class inherits a constructor from DiscountType

Class Method Details

.feeObject



19
20
21
# File 'app/models/discounts/dollars_off_tickets_discount_type.rb', line 19

def self.fee
  0
end

Instance Method Details

#apply_discount_to_cartObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/discounts/dollars_off_tickets_discount_type.rb', line 5

def apply_discount_to_cart
  ensure_amount_exists
  eligible_tickets.each do |ticket|
    ticket.update_column(:discount_id, @discount.id)
    
    if ticket.cart_price > @properties[:amount]
      ticket.update_column(:cart_price, ticket.cart_price - @properties[:amount])
    else
      ticket.update_column(:cart_price, 0)
    end
  end
  FeeCalculator.apply(FeeStrategy.new).to(@discount.cart)
end

#to_sObject



29
30
31
# File 'app/models/discounts/dollars_off_tickets_discount_type.rb', line 29

def to_s
  "#{number_to_currency(@properties[:amount].to_i / 100.00)} off each ticket"
end

#validateObject



23
24
25
26
27
# File 'app/models/discounts/dollars_off_tickets_discount_type.rb', line 23

def validate
  @discount.errors[:base] = "Amount must be filled in." unless @properties[:amount].present?
  @properties[:amount] = @properties[:amount].to_i
  @discount.errors[:base] = "Amount must be greater than zero." if @properties[:amount] == 0
end