<commit_before># frozen_string_literal: true

module Spree
  module Stock
    class Quantifier
      attr_reader :stock_items

      def initialize(variant)
        @variant = variant
        @stock_items = fetch_stock_items
      end

      def total_on_hand
        return 0 if @variant.deleted?

        stock_items.sum(&:count_on_hand)
      end

      def backorderable?
        stock_items.any?(&:backorderable)
      end

      def can_supply?(required)
        total_on_hand >= required || backorderable?
      end

      private

      def fetch_stock_items
        # Don't re-fetch associated stock items from the DB if we've already eager-loaded them
        return @variant.stock_items.to_a if @variant.stock_items.loaded?
<commit_msg>Add explanatory comment on soft-deleted variant stock logic<commit_after># frozen_string_literal: true

module Spree
  module Stock
    class Quantifier
      attr_reader :stock_items

      def initialize(variant)
        @variant = variant
        @stock_items = fetch_stock_items
      end

      def total_on_hand
        # Associated stock_items no longer exist if the variant has been soft-deleted. A variant
        # may still be in an active cart after it's deleted, so this will mark it as out of stock.
        return 0 if @variant.deleted?

        stock_items.sum(&:count_on_hand)
      end

      def backorderable?
        stock_items.any?(&:backorderable)
      end

      def can_supply?(required)
        total_on_hand >= required || backorderable?
      end

      private

      def fetch_stock_items
        # Don't re-fetch associated stock items from the DB if we've already eager-loaded them
        return @variant.stock_items.to_a if @variant.stock_items.loaded?

{'fraction_non_alphanumeric': 0.0587557603686636, 'fraction_numerical': 0.001152073732718894, 'mean_word_length': 2.5448979591836736, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7642053', 'n_tokens_mistral': 501, 'n_tokens_neox': 483, 'n_words': 151}