<commit_before>module Spree
  class StockItem < Spree::Base
    acts_as_paranoid

    with_options inverse_of: :stock_items do
      belongs_to :stock_location, class_name: 'Spree::StockLocation'
      belongs_to :variant, class_name: 'Spree::Variant', counter_cache: true
    end
    has_many :stock_movements, inverse_of: :stock_item

    validates :stock_location, :variant, presence: true
    validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at] }, allow_blank: true

    validates :count_on_hand, numericality: {
                              greater_than_or_equal_to: 0,
                              less_than_or_equal_to: 2**31 - 1,
                              only_integer: true }, if: :verify_count_on_hand?

    delegate :weight, :should_track_inventory?, to: :variant
    delegate :name, to: :variant, prefix: true

    after_save :conditional_variant_touch, if: :changed?
    after_touch { variant.touch }

    self.whitelisted_ransackable_attributes = ['count_on_hand', 'stock_location_id']
    self.whitelisted_ransackable_associations = ['variant']

    scope :with_active_stock_location, -> { joins(:stock_location).merge(Spree::StockLocation.active) }

    def backordered_inventory_units
      Spree::InventoryUnit.backordered_for_stock_item(self)
    end

    def adjust_count_on_hand(value)
      self.with_lock do
        set_count_on_hand(count_on_hand + value)<commit_msg>refs#7399<commit_after>module Spree
  class StockItem < Spree::Base
    acts_as_paranoid

    with_options inverse_of: :stock_items do
      belongs_to :stock_location, class_name: 'Spree::StockLocation'
      belongs_to :variant, class_name: 'Spree::Variant', counter_cache: true
    end
    has_many :stock_movements, inverse_of: :stock_item

    validates :stock_location, :variant, presence: true
    validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at] }, allow_blank: true

    validates :count_on_hand, numericality: {
                              greater_than_or_equal_to: 0,
                              less_than_or_equal_to: 2**31 - 1,
                              only_integer: true }, if: :verify_count_on_hand?

    delegate :weight, :should_track_inventory?, to: :variant
    delegate :name, to: :variant, prefix: true

    after_save :conditional_variant_touch, if: :changed?
    after_touch { variant.touch }
    after_destroy { variant.touch }

    self.whitelisted_ransackable_attributes = ['count_on_hand', 'stock_location_id']
    self.whitelisted_ransackable_associations = ['variant']

    scope :with_active_stock_location, -> { joins(:stock_location).merge(Spree::StockLocation.active) }

    def backordered_inventory_units
      Spree::InventoryUnit.backordered_for_stock_item(self)
    end

    def adjust_count_on_hand(value)
      self.with_lock do
        set_count_on_hand(count_on_hand + value)
{'fraction_non_alphanumeric': 0.08825573314801946, 'fraction_numerical': 0.004864489228630994, 'mean_word_length': 3.599041533546326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '651885', 'n_tokens_mistral': 927, 'n_tokens_neox': 888, 'n_words': 171}