Sha256: 7d8f29e0fe4ac167675f5cd78f9ea912f11d9e1cf503ae2417d576a662ace873

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

module Workarea
  module Admin
    class PricingSkuViewModel < ApplicationViewModel
      def timeline
        @timeline ||= TimelineViewModel.new(model)
      end

      def product
        @product ||=
          begin
            product = Catalog::Product.find_by_sku(model.id)
            ProductViewModel.wrap(product, options) if product.present?
          end
      end

      def inventory
        @inventory ||= Inventory::Sku.where(id: model.id).first
      end

      # All prices that this SKU will be sold at. Makes a determination
      # based on whether the price is on sale.
      #
      # @return [Array<Workarea::Pricing::Price>]
      def sell_prices
        prices.sort_by(&:sell)
      end

      # The lowest price that this SKU can be purchased for. Dependent
      # on the current sale state of the SKU.
      #
      # @return [Money]
      def min_price
        sell_prices.first&.sell
      end

      # The highest regular price set on this SKU.
      #
      # @return [Money]
      def max_price
        sell_prices.last&.sell
      end

      # Show a price range if the `min_price` and `max_price` are not
      # the same value.
      #
      # @return [Boolean]
      def show_range?
        min_price.present? && max_price.present? && min_price != max_price
      end

      # This SKU is considered "on sale" if it is marked as such, or if
      # any prices that it contains are marked as such.
      #
      # @return [Boolean]
      def on_sale?
        model.on_sale? || prices.any?(&:on_sale?)
      end

      # The price of this SKU as rendered on the index page. Shows a
      # price range when multiple prices are contained within the SKU,
      # otherwise it just shows the only sell price available.
      #
      # @return [String]
      def sell_price
        return min_price.format unless show_range?
        "#{max_price.format} – #{min_price.format}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-admin-3.5.7 app/view_models/workarea/admin/pricing_sku_view_model.rb
workarea-admin-3.4.29 app/view_models/workarea/admin/pricing_sku_view_model.rb