Class: Workarea::Pricing::Discount::Collection
- Inherits:
-
Object
- Object
- Workarea::Pricing::Discount::Collection
- Includes:
- Enumerable
- Defined in:
- app/models/workarea/pricing/discount/collection.rb
Overview
This class exists mostly for performance. It gets a list of discounts and caches that list so qualification does not have to fetch from the database over and over again.
Class Method Summary collapse
-
.expire_cache ⇒ Object
TODO remove in v4, unused.
Instance Method Summary collapse
-
#all ⇒ Array<Discount>
All currently active discounts.
-
#find(id) ⇒ Discount?
Find a discount by id.
-
#find_by_class(klass) ⇒ Array<Discount>
Find an array of discounts that are instances of the passed class.
-
#skus ⇒ Array<String>
Get a compiled list of SKUs from all discounts.
Class Method Details
.expire_cache ⇒ Object
TODO remove in v4, unused
14 15 16 |
# File 'app/models/workarea/pricing/discount/collection.rb', line 14 def self.expire_cache Rails.cache.delete('discounts_cache') end |
Instance Method Details
#all ⇒ Array<Discount>
All currently active discounts.
22 23 24 25 26 27 28 |
# File 'app/models/workarea/pricing/discount/collection.rb', line 22 def all @all ||= Pricing::Discount .all .to_a .select(&:active?) .sort end |
#find(id) ⇒ Discount?
Find a discount by id.
35 36 37 |
# File 'app/models/workarea/pricing/discount/collection.rb', line 35 def find(id) all.detect { |discount| discount.id.to_s == id.to_s } end |
#find_by_class(klass) ⇒ Array<Discount>
Find an array of discounts that are instances of the passed class.
45 46 47 |
# File 'app/models/workarea/pricing/discount/collection.rb', line 45 def find_by_class(klass) all.select { |discount| discount.class == klass } end |
#skus ⇒ Array<String>
Get a compiled list of SKUs from all discounts. Useful when trying to lookup all SKUs for a pricing request at once (for performance).
55 56 57 |
# File 'app/models/workarea/pricing/discount/collection.rb', line 55 def skus all.select { |d| d.respond_to?(:sku) }.map(&:sku) end |