Sha256: b4cb65d0d4fbb406367522b1b3db94924fe407b91b3dd3173efb3da3aaf70ed2

Contents?: true

Size: 1.49 KB

Versions: 28

Compression:

Stored size: 1.49 KB

Contents

module Workarea
  class Categorization
    include Enumerable
    delegate :blank?, :present?, :size, :length, :to_a, to: :all

    def initialize(product = nil)
      @product = product
    end

    def each(&block)
      all.each(&block)
    end

    def default_model
      key = [@product.cache_key, 'default_category', Release.current&.id].compact.join('/')

      @default_model ||= Rails.cache.fetch(key, expires_in: Workarea.config.cache_expirations.products_default_category) do
        manual_default_model ||
          to_models.sort_by(&:created_at).select(&:active?).first
      end
    end

    def default
      default_model.try(:id)
    end

    def manual
      manual_models.map(&:id)
    end

    def queries
      return [] if @product.blank?
      @queries ||= Search::Storefront::CategoryQuery.find_by_product(@product)
    end

    def to_models
      @to_models ||= (manual_models + query_models).uniq
    end

    def manual_default_model
      return unless @product.default_category_id.present?

      @manual_default_model ||=
        Catalog::Category.where(id: @product.default_category_id)
                         .detect { |c| c.active? && c.id.to_s.in?(all)  }
    end

    private

    def all
      (manual + queries).map(&:to_s).uniq
    end

    def query_models
      @query_models ||= Catalog::Category.any_in(id: queries).to_a
    end

    def manual_models
      return [] if @product.blank?
      @manual_models ||= FeaturedCategorization.new(@product.id).to_a
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/queries/workarea/categorization.rb
workarea-core-3.5.26 app/queries/workarea/categorization.rb
workarea-core-3.5.25 app/queries/workarea/categorization.rb
workarea-core-3.5.23 app/queries/workarea/categorization.rb
workarea-core-3.5.22 app/queries/workarea/categorization.rb
workarea-core-3.5.21 app/queries/workarea/categorization.rb
workarea-core-3.5.20 app/queries/workarea/categorization.rb
workarea-core-3.5.19 app/queries/workarea/categorization.rb
workarea-core-3.5.18 app/queries/workarea/categorization.rb
workarea-core-3.5.17 app/queries/workarea/categorization.rb
workarea-core-3.5.16 app/queries/workarea/categorization.rb
workarea-core-3.5.15 app/queries/workarea/categorization.rb
workarea-core-3.5.14 app/queries/workarea/categorization.rb
workarea-core-3.5.13 app/queries/workarea/categorization.rb
workarea-core-3.5.12 app/queries/workarea/categorization.rb
workarea-core-3.5.11 app/queries/workarea/categorization.rb
workarea-core-3.5.10 app/queries/workarea/categorization.rb
workarea-core-3.5.9 app/queries/workarea/categorization.rb
workarea-core-3.5.8 app/queries/workarea/categorization.rb
workarea-core-3.5.7 app/queries/workarea/categorization.rb