Sha256: 5364c4ee33b916e094d02ea415a2156e8c599e788731ea61b833ba2f0d55df9e

Contents?: true

Size: 901 Bytes

Versions: 11

Compression:

Stored size: 901 Bytes

Contents

module Spree
  class StockMovement < Spree::Base
    QUANTITY_LIMITS = {
      max: 2**31 - 1,
      min: -2**31
    }.freeze

    belongs_to :stock_item, class_name: 'Spree::StockItem', inverse_of: :stock_movements
    belongs_to :originator, polymorphic: true

    after_create :update_stock_item_quantity

    with_options presence: true do
      validates :stock_item
      validates :quantity, numericality: {
        greater_than_or_equal_to: QUANTITY_LIMITS[:min],
        less_than_or_equal_to: QUANTITY_LIMITS[:max],
        only_integer: true
      }
    end

    scope :recent, -> { order(created_at: :desc) }

    self.whitelisted_ransackable_attributes = ['quantity']

    def readonly?
      persisted?
    end

    private

    def update_stock_item_quantity
      return unless self.stock_item.should_track_inventory?
      stock_item.adjust_count_on_hand quantity
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spree_core-3.3.6 app/models/spree/stock_movement.rb
spree_core-3.3.5 app/models/spree/stock_movement.rb
spree_core-3.3.4 app/models/spree/stock_movement.rb
spree_core-3.3.3 app/models/spree/stock_movement.rb
spree_core-3.3.2 app/models/spree/stock_movement.rb
spree_core-3.3.1 app/models/spree/stock_movement.rb
spree_core-3.3.0 app/models/spree/stock_movement.rb
spree_core-3.3.0.rc4 app/models/spree/stock_movement.rb
spree_core-3.3.0.rc3 app/models/spree/stock_movement.rb
spree_core-3.3.0.rc2 app/models/spree/stock_movement.rb
spree_core-3.3.0.rc1 app/models/spree/stock_movement.rb