Sha256: 111159837d0cf56d94236e8046b5f5fb8e5c43f1b20ae18a48f00c6b4a752876

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

module Workarea
  decorate Storefront::ProductViewModel, with: :product_badges do
    def badges
      @badges ||= begin
        badges = Badges.sort.call(model.badges + automatic_badges)

        if Badges.max_badges.present?
          badges.first(Badges.max_badges)
        else
          badges
        end
      end
    end

    def automatic_badges
      [new_product_badge, sale_badge, best_seller_badge].compact
    end

    private
      def new_product_badge
        if new? && model.new_badge?
          t("workarea.storefront.products.badges.new")
        end
      end

      def sale_badge
        if on_sale? && model.sale_badge?
          t("workarea.storefront.products.badges.sale")
        end
      end

      def best_seller_badge
        if best_seller? && model.best_seller_badge?
          t("workarea.storefront.products.badges.best_seller")
        end
      end

      def best_sellers
        top_seller_limit = Badges.number_of_top_sellers
        @best_sellers ||= Workarea::Insights::TopProducts.current.results.first(top_seller_limit).map { |p| p["product_id"] }
      end

      def best_seller?
        return false unless best_sellers.present?
        model.id.in?(best_sellers)
      end

      def new?
        model.public_send(Workarea.config.product_badges[:new_date_field]) > Badges.new_threshold.days.ago
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workarea-product_badges-1.4.1 app/view_models/workarea/storefront/product_view_model.decorator
workarea-product_badges-1.4.0 app/view_models/workarea/storefront/product_view_model.decorator
workarea-product_badges-1.3.3 app/view_models/workarea/storefront/product_view_model.decorator
workarea-product_badges-1.3.2 app/view_models/workarea/storefront/product_view_model.decorator