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.4.27 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.4 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.26 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.3 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.25 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.2 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.24 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.1 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.23 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.22 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.0 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.21 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.5.0.beta.1 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.20 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.19 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.18 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.17 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.16 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.15 app/models/workarea/pricing/discount/collection.rb
workarea-core-3.4.14 app/models/workarea/pricing/discount/collection.rb