Sha256: 58e76474ba8798244d634c71fc4d57ee8f25b3c8774c811594f7347789080e0b

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

module Spree
  class Price < Spree::Base
    acts_as_paranoid
    belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', touch: true

    validate :check_price
    validates :amount, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
    validate :validate_amount_maximum
    after_save :set_default_price

    extend DisplayMoney
    money_methods :amount, :price

    self.whitelisted_ransackable_attributes = ['amount']

    # @return [Spree::Money] this price as a Spree::Money object
    def money
      Spree::Money.new(amount || 0, { currency: currency })
    end

    # An alias for #amount
    def price
      amount
    end

    # Sets this price's amount to a new value, parsing it if the new value is
    # a string.
    #
    # @param price [String, #to_d] a new amount
    def price=(price)
      self[:amount] = Spree::LocalizedNumber.parse(price)
    end

    private

    def check_price
      self.currency ||= Spree::Config[:currency]
    end

    def maximum_amount
      BigDecimal '999999.99'
    end

    def validate_amount_maximum
      if amount && amount > maximum_amount
        errors.add :amount, I18n.t('errors.messages.less_than_or_equal_to', count: maximum_amount)
      end
    end

    def set_default_price
      if is_default?
        other_default_prices = variant.prices.where(currency: self.currency, is_default: true).where.not(id: self.id)
        other_default_prices.each { |p| p.update_attributes!(is_default: false) }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
solidus_core-1.2.3 app/models/spree/price.rb
solidus_core-1.2.2 app/models/spree/price.rb
solidus_core-1.2.1 app/models/spree/price.rb
solidus_core-1.2.0 app/models/spree/price.rb
solidus_core-1.2.0.rc2 app/models/spree/price.rb
solidus_core-1.2.0.rc1 app/models/spree/price.rb
solidus_core-1.2.0.beta1 app/models/spree/price.rb