Sha256: 34c09b97d6e1b6fca493a04d35b03c6d55dedb20497c9ba0a376162083f225e9

Contents?: true

Size: 1.45 KB

Versions: 34

Compression:

Stored size: 1.45 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"

      @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::Product.find_categories(@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 ||= Catalog::Category.by_product(@product.id).to_a
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/queries/workarea/categorization.rb
workarea-core-3.4.44 app/queries/workarea/categorization.rb
workarea-core-3.4.43 app/queries/workarea/categorization.rb
workarea-core-3.4.42 app/queries/workarea/categorization.rb
workarea-core-3.4.41 app/queries/workarea/categorization.rb
workarea-core-3.4.40 app/queries/workarea/categorization.rb
workarea-core-3.4.39 app/queries/workarea/categorization.rb
workarea-core-3.4.38 app/queries/workarea/categorization.rb
workarea-core-3.4.37 app/queries/workarea/categorization.rb
workarea-core-3.4.36 app/queries/workarea/categorization.rb
workarea-core-3.4.35 app/queries/workarea/categorization.rb
workarea-core-3.4.34 app/queries/workarea/categorization.rb
workarea-core-3.4.33 app/queries/workarea/categorization.rb
workarea-core-3.4.32 app/queries/workarea/categorization.rb
workarea-core-3.4.31 app/queries/workarea/categorization.rb
workarea-core-3.4.30 app/queries/workarea/categorization.rb
workarea-core-3.4.29 app/queries/workarea/categorization.rb
workarea-core-3.4.28 app/queries/workarea/categorization.rb
workarea-core-3.4.27 app/queries/workarea/categorization.rb
workarea-core-3.4.26 app/queries/workarea/categorization.rb