Sha256: 32852a5c1cdf247ecb1940ee2b8ddceefd3a3a4d2cf66ded20cc07b53d79d000

Contents?: true

Size: 1.03 KB

Versions: 31

Compression:

Stored size: 1.03 KB

Contents

class DollarsOffTicketsDiscountType < DiscountType
  include ActionView::Helpers::NumberHelper
  discount_type :dollars_off_tickets

  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

  def self.fee
    0
  end

  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

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

private

  def ensure_amount_exists
    raise "Amount missing!" if @properties[:amount].blank?
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
artfully_ose-1.3.0.pre3 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.3.0.pre2 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.3.0.pre1 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.beta.1 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.alpha.2 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.alpha.1 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.27 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.26 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.24 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.23 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.21 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.20 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.19 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.18 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.17 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.16 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.15 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.12 app/models/discounts/dollars_off_tickets_discount_type.rb
artfully_ose-1.2.0.pre.11 app/models/discounts/dollars_off_tickets_discount_type.rb