Sha256: edf156148116780601754195ab44ffcf252d196841253a0b39d5b4b1fbe5f0b8

Contents?: true

Size: 1.79 KB

Versions: 34

Compression:

Stored size: 1.79 KB

Contents

module Workarea
  module Admin
    class ProductRulesPreviewViewModel < ApplicationViewModel
      delegate :total, :page, :per_page, to: :search

      def self.wrap(model, options = {})
        return model.map { |m| wrap(m, options) } if model.is_a?(Enumerable)

        view_model_class = [
          "Workarea::Admin",
          "#{model.class.model_name.param_key.camelize}ProductRulesViewModel"
        ].join('::')

        view_model_class.constantize.new(model, options)
      rescue
        new(model, options)
      end

      # Fetches search results of products for category. Extends
      # search class with {Search::AdminProductRulesPreview} to allow
      # overriding display rules.
      #
      # @return [Workarea::Search::CategoryBrowse]
      #   enumerable collection of the search results
      #
      def search
        @search ||= Search::CategoryBrowse.new(
          rules: model.product_rules.select(&:valid?),
          sort: model.default_sort,
          page: options[:page] || 1,
          show_all: show_all?
        ).tap { |s| s.extend(Search::AdminProductRulesPreview) }
      end

      # Provides all products found from search results
      #
      # @return [Workarea::PagedArray<Workarea::Admin::ProductViewModel>]
      #
      def results
        @results ||= PagedArray.from(
          search.results.map do |r|
            Admin::ProductViewModel.new(r[:model], inventory: r[:inventory])
          end,
          search.page,
          search.per_page,
          search.total
        )
      end

      def show_all?
        options[:show_all].to_s =~ /true/
      end

      def display_results?
        model.product_rules.select(&:valid?).present?
      end

      def base_query
        return unless model.respond_to?(:query)
        model.query
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

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