Module: Models::SaleDiscountable
- Extended by:
- ActiveSupport::Concern
- Included in:
- CatalogItem, ViewProductCatalog
- Defined in:
- app/concerns/models/sale_discountable.rb
Overview
ActiveSupport::Concern mixin: sale discountable.
Adds sale-price helpers to models carrying price/sale_price columns
(regular amount, sale amount, effective price honoring sale date windows,
and their Money-wrapped variants).
Instance Method Summary collapse
-
#amount ⇒ BigDecimal
The base (non-sale) amount, preferring
pricewhen the model exposes it. -
#effective_price ⇒ BigDecimal
The currently applicable price.
-
#money_effective_price ⇒ Money
The currently applicable price wrapped in a Money object.
-
#money_price ⇒ Money
The base amount wrapped in a Money object.
-
#money_sale_price ⇒ Money?
The sale price wrapped in a Money object.
-
#sale_price_in_effect?(exclude_effective_date: false, date_to_check: nil) ⇒ Boolean
Whether the sale price is currently in effect.
-
#sale_price_percentage_off ⇒ Float
Percentage discount the sale price represents off the regular amount.
Instance Method Details
#amount ⇒ BigDecimal
The base (non-sale) amount, preferring price when the model exposes it.
16 17 18 19 20 |
# File 'app/concerns/models/sale_discountable.rb', line 16 def amount return price if respond_to?(:price) super end |
#effective_price ⇒ BigDecimal
The currently applicable price.
48 49 50 |
# File 'app/concerns/models/sale_discountable.rb', line 48 def effective_price (sale_price if sale_price_in_effect?) || amount end |
#money_effective_price ⇒ Money
The currently applicable price wrapped in a Money object.
41 42 43 |
# File 'app/concerns/models/sale_discountable.rb', line 41 def money_effective_price (money_sale_price if sale_price_in_effect?) || money_price end |
#money_price ⇒ Money
The base amount wrapped in a Money object.
25 26 27 |
# File 'app/concerns/models/sale_discountable.rb', line 25 def money_price Money.new(amount * 100, currency) end |
#money_sale_price ⇒ Money?
The sale price wrapped in a Money object.
32 33 34 35 36 |
# File 'app/concerns/models/sale_discountable.rb', line 32 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
Whether the sale price is currently in effect.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/concerns/models/sale_discountable.rb', line 59 def sale_price_in_effect?(exclude_effective_date: false, date_to_check: nil) return false if sale_price.blank? 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_off ⇒ Float
Percentage discount the sale price represents off the regular amount.
76 77 78 79 80 |
# File 'app/concerns/models/sale_discountable.rb', line 76 def sale_price_percentage_off return 0.0 unless sale_price.present? && amount.present? ((1.0 - (sale_price / amount)) * 100).round(2) end |