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.
Instance Method Summary collapse
- #amount ⇒ Object
- #effective_price ⇒ Object
- #money_effective_price ⇒ Object
- #money_price ⇒ Object
- #money_sale_price ⇒ Object
- #sale_price_in_effect?(exclude_effective_date: false, date_to_check: nil) ⇒ Boolean
- #sale_price_percentage_off ⇒ Object
Instance Method Details
#amount ⇒ Object
9 10 11 12 13 |
# File 'app/concerns/models/sale_discountable.rb', line 9 def amount return price if respond_to?(:price) super end |
#effective_price ⇒ Object
29 30 31 |
# File 'app/concerns/models/sale_discountable.rb', line 29 def effective_price (sale_price if sale_price_in_effect?) || amount end |
#money_effective_price ⇒ Object
25 26 27 |
# File 'app/concerns/models/sale_discountable.rb', line 25 def money_effective_price (money_sale_price if sale_price_in_effect?) || money_price end |
#money_price ⇒ Object
15 16 17 |
# File 'app/concerns/models/sale_discountable.rb', line 15 def money_price Money.new(amount * 100, currency) end |
#money_sale_price ⇒ Object
19 20 21 22 23 |
# File 'app/concerns/models/sale_discountable.rb', line 19 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
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/concerns/models/sale_discountable.rb', line 33 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 ⇒ Object
46 47 48 49 50 |
# File 'app/concerns/models/sale_discountable.rb', line 46 def sale_price_percentage_off return 0.0 unless sale_price.present? && amount.present? ((1.0 - (sale_price / amount)) * 100).round(2) end |