<commit_before>module Spree
  module Stock
    class AvailabilityValidator < ActiveModel::Validator
      def validate(line_item)
        unit_count = line_item.inventory_units.reject(&:pending?).sum(&:quantity)
        return if unit_count >= line_item.quantity

        quantity = line_item.quantity - unit_count
        return if quantity.zero?

        return if item_available?(line_item, quantity)

        variant = line_item.variant
        display_name = variant.name.to_s
        display_name += " (#{variant.options_text})" unless variant.options_text.blank?

        line_item.errors[:quantity] << Spree.t(
          :selected_quantity_not_available,
          item: display_name.inspect
        )
      end

      private

      def item_available?(line_item, quantity)
        Stock::Quantifier.new(line_item.variant).can_supply?(quantity)<commit_msg>Fixes "You passed A copy of Spree::Stock::AvailabilityValidator has been removed from the module tree but is still active" errors in development env after reload<commit_after>module Spree
  module Stock
    class AvailabilityValidator < ActiveModel::Validator
      def validate(line_item)
        unit_count = line_item.inventory_units.reject(&:pending?).sum(&:quantity)
        return if unit_count >= line_item.quantity

        quantity = line_item.quantity - unit_count
        return if quantity.zero?

        return if item_available?(line_item, quantity)

        variant = line_item.variant
        display_name = variant.name.to_s
        display_name += " (#{variant.options_text})" unless variant.options_text.blank?

        line_item.errors[:quantity] << Spree.t(
          :selected_quantity_not_available,
          item: display_name.inspect
        )
      end

      private

      def item_available?(line_item, quantity)
        Spree::Stock::Quantifier.new(line_item.variant).can_supply?(quantity)
{'fraction_non_alphanumeric': 0.0870026525198939, 'fraction_numerical': 0.0, 'mean_word_length': 3.1179039301310043, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4665155', 'n_tokens_mistral': 538, 'n_tokens_neox': 512, 'n_words': 110}