Class: Workarea::Pricing::Discount::Collection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.expire_cacheObject

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

#allArray<Discount>

All currently active discounts.

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • klass (Class)

Returns:



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

#skusArray<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).

Returns:



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