Sha256: 251ad65591b50723b00225fdcadcdfb1de787ad8abc4852cc8dcdd611ef6c009

Contents?: true

Size: 894 Bytes

Versions: 20

Compression:

Stored size: 894 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 stock_item.should_track_inventory?
      stock_item.adjust_count_on_hand quantity
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
spree_core-3.4.6 app/models/spree/stock_movement.rb
spree_core-3.6.6 app/models/spree/stock_movement.rb
spree_core-3.6.5 app/models/spree/stock_movement.rb
spree_core-3.6.4 app/models/spree/stock_movement.rb
spree_core-3.6.3 app/models/spree/stock_movement.rb
spree_core-3.6.2 app/models/spree/stock_movement.rb
spree_core-3.6.1 app/models/spree/stock_movement.rb
spree_core-3.6.0 app/models/spree/stock_movement.rb
spree_core-3.5.0 app/models/spree/stock_movement.rb
spree_core-3.6.0.rc1 app/models/spree/stock_movement.rb
spree_core-3.5.0.rc2 app/models/spree/stock_movement.rb
spree_core-3.5.0.rc1 app/models/spree/stock_movement.rb
spree_core-3.4.5 app/models/spree/stock_movement.rb
spree_core-3.4.4 app/models/spree/stock_movement.rb
spree_core-3.4.3 app/models/spree/stock_movement.rb
spree_core-3.4.2 app/models/spree/stock_movement.rb
spree_core-3.4.1 app/models/spree/stock_movement.rb
spree_core-3.4.0 app/models/spree/stock_movement.rb
spree_core-3.4.0.rc2 app/models/spree/stock_movement.rb
spree_core-3.4.0.rc1 app/models/spree/stock_movement.rb