<commit_before>module Spree
  class LicenseKey < ActiveRecord::Base
    belongs_to :inventory_unit, :class_name => "Spree::InventoryUnit"
    belongs_to :variant, :class_name => "Spree::Variant"
    belongs_to :license_key_type, :class_name => "Spree::LicenseKeyType"

    attr_accessible :license_key, :inventory_unit_id, :variant_id

    def self.assign_license_keys!(inventory_unit)
      transaction do
        if inventory_unit.variant.license_key_types.empty?
          [self.get_available_key(inventory_unit)]
        else
          inventory_unit.variant.license_key_types.map do |key_type|
            self.get_available_key(inventory_unit, key_type)
          end
        end
      end
    end

    class InsufficientLicenseKeys < ::StandardError; end

    private

    def self.get_available_key(inventory_unit, license_key_type = nil)
      license_key = self.where(:variant_id => inventory_unit.variant.id, :inventory_unit_id => nil, :license_key_type_id => license_key_type.try(:id)).first
      if license_key.nil?
        raise LicenseKey::InsufficientLicenseKeys, "Variant: #{inventory_unit.variant.to_param}, License Key Type: #{license_key_type.try(:id)}"
      end
      license_key.inventory_unit = inventory_unit
      license_key.save!
      license_key
    end
  end
end
<commit_msg>Add a scope for available<commit_after>module Spree
  class LicenseKey < ActiveRecord::Base
    belongs_to :inventory_unit, :class_name => "Spree::InventoryUnit"
    belongs_to :variant, :class_name => "Spree::Variant"
    belongs_to :license_key_type, :class_name => "Spree::LicenseKeyType"

    attr_accessible :license_key, :inventory_unit_id, :variant_id

    scope :available, where(inventory_unit_id: nil)

    def self.assign_license_keys!(inventory_unit)
      transaction do
        if inventory_unit.variant.license_key_types.empty?
          [self.get_available_key(inventory_unit)]
        else
          inventory_unit.variant.license_key_types.map do |key_type|
            self.get_available_key(inventory_unit, key_type)
          end
        end
      end
    end

    class InsufficientLicenseKeys < ::StandardError; end

    private

    def self.get_available_key(inventory_unit, license_key_type = nil)
      license_key = self.where(:variant_id => inventory_unit.variant.id, :inventory_unit_id => nil, :license_key_type_id => license_key_type.try(:id)).first
      if license_key.nil?
        raise LicenseKey::InsufficientLicenseKeys, "Variant: #{inventory_unit.variant.to_param}, License Key Type: #{license_key_type.try(:id)}"
      end
      license_key.inventory_unit = inventory_unit
      license_key.save!
      license_key
    end
  end
end

{'fraction_non_alphanumeric': 0.08921239268383725, 'fraction_numerical': 0.0, 'mean_word_length': 3.75177304964539, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '4308882', 'n_tokens_mistral': 837, 'n_tokens_neox': 831, 'n_words': 152}