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

Instance Method Summary collapse

Methods inherited from DiscountType

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

Constructor Details

This class inherits a constructor from DiscountType

Instance Method Details

#apply_discount_to_cartObject



5
6
7
8
9
10
11
12
13
14
15
# 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.price > @properties[:amount]
      ticket.update_column(:cart_price, ticket.price - @properties[:amount])
    else
      ticket.update_column(:cart_price, 0)
    end
  end
end

#to_sObject



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

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

#validateObject



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

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