Module: Models::SaleDiscountable

Extended by:
ActiveSupport::Concern
Included in:
CatalogItem, ViewProductCatalog
Defined in:
app/concerns/models/sale_discountable.rb

Instance Method Summary collapse

Instance Method Details

#amountObject



8
9
10
11
12
# File 'app/concerns/models/sale_discountable.rb', line 8

def amount
  return price if respond_to?(:price)

  super
end

#effective_priceObject



28
29
30
# File 'app/concerns/models/sale_discountable.rb', line 28

def effective_price
  (sale_price if sale_price_in_effect?) || amount
end

#money_effective_priceObject



24
25
26
# File 'app/concerns/models/sale_discountable.rb', line 24

def money_effective_price
  (money_sale_price if sale_price_in_effect?) || money_price
end

#money_priceObject



14
15
16
# File 'app/concerns/models/sale_discountable.rb', line 14

def money_price
  Money.new(amount * 100, currency)
end

#money_sale_priceObject



18
19
20
21
22
# File 'app/concerns/models/sale_discountable.rb', line 18

def money_sale_price
  return unless sale_price

  Money.new(sale_price * 100, currency)
end

#sale_price_in_effect?(exclude_effective_date: false, date_to_check: nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/concerns/models/sale_discountable.rb', line 32

def sale_price_in_effect?(exclude_effective_date: false, date_to_check: nil)
  return false unless sale_price.present?

  unless exclude_effective_date
    date_to_check ||= Date.current
    # If today is before the sale start, don't send
    return false if sale_price_effective_date && date_to_check < sale_price_effective_date
    # If today is past the sale expiration, don't send
    return false if sale_price_expiration_date && date_to_check > sale_price_expiration_date
  end
  true
end

#sale_price_percentage_offObject



45
46
47
48
49
# File 'app/concerns/models/sale_discountable.rb', line 45

def sale_price_percentage_off
  return 0.0 unless sale_price.present? && amount.present?

  ((1.0 - (sale_price / amount)) * 100).round(2)
end