Sha256: 69a8c23106d1b536dd341a89e5f26689ea6f95bd10d31c273fe3d6e8bd239528

Contents?: true

Size: 1.53 KB

Versions: 62

Compression:

Stored size: 1.53 KB

Contents

module Workarea
  module Pricing
    class Discount
      # 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 Collection
        include Enumerable

        delegate :each, :select, :reject, :to_set, to: :all

        # TODO remove in v4, unused
        def self.expire_cache
          Rails.cache.delete('discounts_cache')
        end

        # All currently active discounts.
        #
        # @return [Array<Discount>]
        #
        def all
          @all ||= Pricing::Discount
            .all
            .to_a
            .select(&:active?)
            .sort
        end

        # Find a discount by id.
        #
        # @param [String] id
        # @return [Discount, nil]
        #
        def find(id)
          all.detect { |discount| discount.id.to_s == id.to_s }
        end

        # Find an array of discounts that are instances of
        # the passed class.
        #
        # @param [Class] klass
        # @return [Array<Discount>]
        #
        def find_by_class(klass)
          all.select { |discount| discount.class == klass }
        end

        # Get a compiled list of SKUs from all discounts. Useful
        # when trying to lookup all SKUs for a pricing request at once
        # (for performance).
        #
        # @return [Array<String>]
        #
        def skus
          all.select { |d| d.respond_to?(:sku) }.map(&:sku)
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.26 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.45 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.25 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.23 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.44 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.22 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.43 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.21 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.42 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.20 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.41 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.19 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.40 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.18 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.39 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.17 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.38 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.16 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.37 app/models/workarea/pricing/discount/collection.rb