Sha256: 903ca996d10c3dbe5ab2a24d8a8176c2a936ff6b51fce324d1fe8ce14823da24

Contents?: true

Size: 1.28 KB

Versions: 28

Compression:

Stored size: 1.28 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.recent(results_count * 2).select(&:active?)
        end

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

Version data entries

28 entries across 28 versions & 1 rubygems

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