<commit_before>require 'open_food_network/scope_variant_to_hub'
require 'open_food_network/variant_and_line_item_naming'

Spree::LineItem.class_eval do
  include OpenFoodNetwork::VariantAndLineItemNaming
  include LineItemBasedAdjustmentHandling
  has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items', class_name: 'Spree::OptionValue'

  # Redefining here to add the inverse_of option
  belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items

  # Allows manual skipping of Stock::AvailabilityValidator
  attr_accessor :skip_stock_check

  before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed?
  after_save :update_units

  before_destroy :update_inventory_before_destroy

  delegate :product, :unit_description, to: :variant

  # -- Scopes
  scope :managed_by, lambda { |user|
    if user.has_spree_role?('admin')
      where(nil)
    else
      # Find line items that are from orders distributed by the user or supplied by the user
      joins(variant: :product).
        joins(:order).
        where('spree_orders.distributor_id IN (?) OR spree_products.supplier_id IN (?)', user.enterprises, user.enterprises).
        select('spree_line_items.*')
    end
  }

  # Find line items that are from order sorted by variant name and unit value
  scope :sorted_by_name_and_unit_value, -> {
    joins(variant: :product).
      reorder("
        lower(spree_products.name) asc,
          lower(spree_variants.display_name) asc,
          spree_variants.unit_value asc")
  }

  scope :from_order_cycle, lambda { |order_cycle|
    joins(order: :order_cycle).
      where('order_cycles.id = ?', order_cycle)
  }

  scope :supplied_by_any, lambda { |enterprises|
    joins("LEFT OUTER JOIN spree_variants
              ON spree_line_items.variant_id = spree_variants.id
           LEFT OUTER JOIN spree_products
              ON spree_variants.product_id = spree_products.id").
      where("spree_products.supplier_id IN (?)", enterprises)
  }

  scope :with_tax, -> {
    joins(:adjustments).
      where('spree_adjustments.originator_type = ?', 'Spree::TaxRate').
      select('DISTINCT spree_line_items.*')
  }

  # Line items without a Spree::TaxRate-originated adjustment
  scope :without_tax, -> {<commit_msg>Add comment to better explain why this scope is not simply using joins(:product)<commit_after>require 'open_food_network/scope_variant_to_hub'
require 'open_food_network/variant_and_line_item_naming'

Spree::LineItem.class_eval do
  include OpenFoodNetwork::VariantAndLineItemNaming
  include LineItemBasedAdjustmentHandling
  has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items', class_name: 'Spree::OptionValue'

  # Redefining here to add the inverse_of option
  belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items

  # Allows manual skipping of Stock::AvailabilityValidator
  attr_accessor :skip_stock_check

  before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed?
  after_save :update_units

  before_destroy :update_inventory_before_destroy

  delegate :product, :unit_description, to: :variant

  # -- Scopes
  scope :managed_by, lambda { |user|
    if user.has_spree_role?('admin')
      where(nil)
    else
      # Find line items that are from orders distributed by the user or supplied by the user
      joins(variant: :product).
        joins(:order).
        where('spree_orders.distributor_id IN (?) OR spree_products.supplier_id IN (?)', user.enterprises, user.enterprises).
        select('spree_line_items.*')
    end
  }

  # Find line items that are from order sorted by variant name and unit value
  scope :sorted_by_name_and_unit_value, -> {
    joins(variant: :product).
      reorder("
        lower(spree_products.name) asc,
          lower(spree_variants.display_name) asc,
          spree_variants.unit_value asc")
  }

  scope :from_order_cycle, lambda { |order_cycle|
    joins(order: :order_cycle).
      where('order_cycles.id = ?', order_cycle)
  }

  # Here we are simply joining the line item to its variant and product
  # We do this with SQL to avoid the default scopes,
  #   and with that, include deleted variants and deleted products
  scope :supplied_by_any, lambda { |enterprises|
    joins("LEFT OUTER JOIN spree_variants
              ON spree_line_items.variant_id = spree_variants.id
           LEFT OUTER JOIN spree_products
              ON spree_variants.product_id = spree_products.id").
      where("spree_products.supplier_id IN (?)", enterprises)
  }

  scope :with_tax, -> {
    joins(:adjustments).
      where('spree_adjustments.originator_type = ?', 'Spree::TaxRate').
      select('DISTINCT spree_line_items.*')
  }

  # Line items without a Spree::TaxRate-originated adjustment
  scope :without_tax, -> {
{'fraction_non_alphanumeric': 0.08991993430507082, 'fraction_numerical': 0.0, 'mean_word_length': 4.1069182389937104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3091288', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1519, 'n_words': 358}