Sha256: 6257050888f1b694347fa90934e3992003aa53839f351cc5d7f9cdfe84193dc5

Contents?: true

Size: 1.15 KB

Versions: 28

Compression:

Stored size: 1.15 KB

Contents

module Workarea
  module Pricing
    class Price
      include ApplicationDocument
      include Releasable
      include UnsupportedSegmentation

      field :min_quantity, type: Integer, default: 1
      field :regular, type: Money, default: 0
      field :sale, type: Money
      field :on_sale, type: Boolean, default: false

      embedded_in :sku, class_name: 'Workarea::Pricing::Sku', touch: true
      delegate :tax_code, :discountable?, to: :sku, allow_nil: true

      validates :min_quantity, numericality: { greater_than: 0 }
      before_validation :guard_regular_price

      def name
        ''
      end

      def sell
        if on_sale? && sale.present?
          sale
        else
          regular
        end
      end

      def generic?
        min_quantity == 1
      end

      # Whether this price is on sale. Defaults to the +#on_sale+ value of the
      # +Pricing::Sku+ it's embedded within.
      #
      # @return [Boolean]
      #
      def on_sale?
        super || sku&.on_sale?
      end

      private

      def guard_regular_price
        unless regular.present?
          self.regular = 0.to_m
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/pricing/price.rb
workarea-core-3.5.26 app/models/workarea/pricing/price.rb
workarea-core-3.5.25 app/models/workarea/pricing/price.rb
workarea-core-3.5.23 app/models/workarea/pricing/price.rb
workarea-core-3.5.22 app/models/workarea/pricing/price.rb
workarea-core-3.5.21 app/models/workarea/pricing/price.rb
workarea-core-3.5.20 app/models/workarea/pricing/price.rb
workarea-core-3.5.19 app/models/workarea/pricing/price.rb
workarea-core-3.5.18 app/models/workarea/pricing/price.rb
workarea-core-3.5.17 app/models/workarea/pricing/price.rb
workarea-core-3.5.16 app/models/workarea/pricing/price.rb
workarea-core-3.5.15 app/models/workarea/pricing/price.rb
workarea-core-3.5.14 app/models/workarea/pricing/price.rb
workarea-core-3.5.13 app/models/workarea/pricing/price.rb
workarea-core-3.5.12 app/models/workarea/pricing/price.rb
workarea-core-3.5.11 app/models/workarea/pricing/price.rb
workarea-core-3.5.10 app/models/workarea/pricing/price.rb
workarea-core-3.5.9 app/models/workarea/pricing/price.rb
workarea-core-3.5.8 app/models/workarea/pricing/price.rb
workarea-core-3.5.7 app/models/workarea/pricing/price.rb