Sha256: fcf41d9eba4eb33d592897a117df3b7cd14db802fdb1ad70ce4a1209a47bd564

Contents?: true

Size: 1.11 KB

Versions: 34

Compression:

Stored size: 1.11 KB

Contents

module Workarea
  module Pricing
    class Price
      include ApplicationDocument
      include Releasable

      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

34 entries across 34 versions & 1 rubygems

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