Sha256: 7af53874f708673e352fb59d6f79ea176386038374996bcfd24d87645633a62d

Contents?: true

Size: 1.26 KB

Versions: 28

Compression:

Stored size: 1.26 KB

Contents

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

    def initialize(*product_ids)
      @product_ids = product_ids
    end

    def all
      @all ||= begin
        result = live

        categories_affected_by_current_release.each do |category|
          if product_featured_in_category?(category)
            result += [category] unless result.include?(category)
          else
            result -= [category]
          end
        end

        result
      end
    end

    def live
      @live ||= Catalog::Category.by_product(@product_ids).to_a
    end

    def categories_affected_by_current_release
      return [] if Release.current.blank?

      Catalog::Category.in(
        id: category_changesets_affecting_current_release.map(&:releasable_id)
      )
    end

    def category_changesets_affecting_current_release
      return [] if Release.current.blank?

      FeaturedProducts
        .changesets(*@product_ids)
        .where(releasable_type: Catalog::Category.name)
        .in(release_id: Release.current.preview.releases.map(&:id))
    end

    private

    def product_featured_in_category?(category)
      @product_ids.any? { |id| category.featured_product?(id) }
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

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