Sha256: f9959e79b27b63338f68eaf7896d44e949a338b5d4cdd218116050b113ef6df5

Contents?: true

Size: 1.26 KB

Versions: 34

Compression:

Stored size: 1.26 KB

Contents

module Workarea
  module Storefront
    module ContentBlocks
      class ProductInsightsViewModel < ContentBlockViewModel
        def products
          @products ||= ProductViewModel.wrap(
            add_fallbacks(find_insight_products(data[:type]))
          )
        end

        private

        def find_insight_products(type)
          return [] if type.blank?

          klass = "Workarea::Insights::#{type.to_s.remove(/\s/).camelize}".constantize
          ids = klass.current.results.map { |r| r['product_id'] }
          Catalog::Product.find_ordered_for_display(ids)
        end

        def add_fallbacks(products)
          return products if products.count >= results_count

          results = products +
            find_insight_products(:top_products) +
            find_insight_products(:trending_products)

          results.uniq!(&:id)
          return results.take(results_count) if results.count >= results_count

          results.push(*newest_products)
          results.uniq!(&:id)
          results.take(results_count)
        end

        def newest_products
          @newest_products ||= Catalog::Product.active.recent(results_count)
        end

        def results_count
          Workarea.config.product_insights_count
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

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