<commit_before>module Spree
  LineItem.class_eval do
    validate :ensure_valid_add_ons

    after_save :create_add_on_adjustments
    after_save :persist_add_on_total
    after_save :ensure_valid_add_ons

    attr_accessor :add_on_ids

    def add_ons
      Spree::AddOn.joins(:adjustments).where("source_id = spree_add_ons.id")
    end

    def add_ons=(*new_add_ons)
      @add_on_ids = new_add_ons.flatten!.map { |add_on| add_on.id }
      save!
    end

    private

    def create_add_on_adjustments
      unless add_ons.empty?
        adjustments.add_ons.destroy_all
      end

      if @add_on_ids.present?
        add_ons_to_add = Spree::AddOn.find(@add_on_ids)
        add_ons_to_add.each { |add_on| add_on.adjust(self) }
        @add_on_ids = nil
      end

      ensure_valid_add_ons(true)
    end

    def update_add_on_adjustments
      # create add ons if we update a line item to have some
      create_add_on_adjustments if @add_on_ids.present?
      if quantity_changed?
        add_ons.each { |add_on| add_on.adjust(self) }
      end
    end

    def persist_add_on_total
      update_columns(:add_on_total => add_on_total)
    end

    def ensure_valid_add_ons(raise = false)
      invalid_add_ons = add_ons - product.add_ons
      unless add_ons.empty? || invalid_add_ons.empty?
        invalid_message = invalid_add_ons.map { |add| add.name }.join(", ") + (invalid_add_ons.length > 1 ? " are" : " is")
        errors[:base] << "#{invalid_message} not a valid add on for #{product.name}"
      end
    end
  end
end<commit_msg>cleanup<commit_after>module Spree
  LineItem.class_eval do
    validate :ensure_valid_add_ons

    after_save :create_add_on_adjustments
    after_save :persist_add_on_total
    after_save :ensure_valid_add_ons

    attr_accessor :add_on_ids

    def add_ons
      Spree::AddOn.joins(:adjustments).where("source_id = spree_add_ons.id")
    end

    def add_ons=(*new_add_ons)
      @add_on_ids = new_add_ons.flatten!.map { |add_on| add_on.id }
      save!
    end

    private

    def create_add_on_adjustments
      unless add_ons.empty?
        adjustments.add_ons.destroy_all
      end

      if @add_on_ids.present?
        add_ons_to_add = Spree::AddOn.find(@add_on_ids)
        add_ons_to_add.each { |add_on| add_on.adjust(self) }
        @add_on_ids = nil
      end

      ensure_valid_add_ons(true)
    end

    def persist_add_on_total
      update_columns(:add_on_total => add_on_total)
    end

    def ensure_valid_add_ons(raise = false)
      invalid_add_ons = add_ons - product.add_ons
      unless add_ons.empty? || invalid_add_ons.empty?
        invalid_message = invalid_add_ons.map { |add| add.name }.join(", ") + (invalid_add_ons.length > 1 ? " are" : " is")
        errors[:base] << "#{invalid_message} not a valid add on for #{product.name}"
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08770693906305037, 'fraction_numerical': 0.0007044734061289186, 'mean_word_length': 2.9335180055401664, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '511635', 'n_tokens_mistral': 1034, 'n_tokens_neox': 1007, 'n_words': 187}