<commit_before>class VariantOverride < ActiveRecord::Base
  extend Spree::LocalizedNumber
  include StockSettingsOverrideValidation

  acts_as_taggable

  belongs_to :hub, class_name: 'Enterprise'
  belongs_to :variant, class_name: 'Spree::Variant'

  validates_presence_of :hub_id, :variant_id
  # Default stock can be nil, indicating stock should not be reset or zero, meaning reset to zero. Need to ensure this can be set by the user.
  validates :default_stock, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true

  after_save :refresh_products_cache_from_save
  after_destroy :refresh_products_cache_from_destroy

  default_scope where(permission_revoked_at: nil)

  scope :for_hubs, lambda { |hubs|
    where(hub_id: hubs)
  }

  scope :distinct_import_dates, lambda {
    select('DISTINCT variant_overrides.import_date').
      where('variant_overrides.import_date IS NOT NULL').
      order('import_date DESC')
  }

  localize_number :price

  def self.indexed(hub)
    Hash[
      for_hubs(hub).preload(:variant).map { |vo| [vo.variant, vo] }
    ]
  end

  def self.price_for(hub, variant)
    self.for(hub, variant).andand.price
  end

  def self.count_on_hand_for(hub, variant)
    self.for(hub, variant).andand.count_on_hand
  end

  def self.stock_overridden?(hub, variant)
    count_on_hand_for(hub, variant).present?
  end

  def self.decrement_stock!(hub, variant, quantity)
    vo = self.for(hub, variant)

    if vo.nil?
      Bugsnag.notify RuntimeError.new "Attempting to decrement stock level for a variant without a VariantOverride."
    else
      vo.decrement_stock! quantity
    end
  end

  def stock_overridden?
    on_demand == false && count_on_hand.present?
  end<commit_msg>Make variant_override.stock_overridden? simpler and add comment to explain relationship between count_on_hand and on_demand in a VO<commit_after>class VariantOverride < ActiveRecord::Base
  extend Spree::LocalizedNumber
  include StockSettingsOverrideValidation

  acts_as_taggable

  belongs_to :hub, class_name: 'Enterprise'
  belongs_to :variant, class_name: 'Spree::Variant'

  validates_presence_of :hub_id, :variant_id
  # Default stock can be nil, indicating stock should not be reset or zero, meaning reset to zero. Need to ensure this can be set by the user.
  validates :default_stock, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true

  after_save :refresh_products_cache_from_save
  after_destroy :refresh_products_cache_from_destroy

  default_scope where(permission_revoked_at: nil)

  scope :for_hubs, lambda { |hubs|
    where(hub_id: hubs)
  }

  scope :distinct_import_dates, lambda {
    select('DISTINCT variant_overrides.import_date').
      where('variant_overrides.import_date IS NOT NULL').
      order('import_date DESC')
  }

  localize_number :price

  def self.indexed(hub)
    Hash[
      for_hubs(hub).preload(:variant).map { |vo| [vo.variant, vo] }
    ]
  end

  def self.price_for(hub, variant)
    self.for(hub, variant).andand.price
  end

  def self.count_on_hand_for(hub, variant)
    self.for(hub, variant).andand.count_on_hand
  end

  def self.stock_overridden?(hub, variant)
    count_on_hand_for(hub, variant).present?
  end

  def self.decrement_stock!(hub, variant, quantity)
    vo = self.for(hub, variant)

    if vo.nil?
      Bugsnag.notify RuntimeError.new "Attempting to decrement stock level for a variant without a VariantOverride."
    else
      vo.decrement_stock! quantity
    end
  end

  def stock_overridden?
    # If count_on_hand is present, it means on_demand is false
    #   See StockSettingsOverrideValidation for details
    count_on_hand.present?
  end
{'fraction_non_alphanumeric': 0.08134757600657354, 'fraction_numerical': 0.0005477951246233908, 'mean_word_length': 4.262247838616715, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '641879', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1180, 'n_words': 301}