<commit_before>require 'active_support/core_ext/module/delegation'

module ElasticRecord
  module SearchesMany
    class Association
      attr_reader :owner, :reflection, :collection

      delegate :klass, :options, to: :reflection

      def initialize(owner, reflection)
        @owner      = owner
        @reflection = reflection
        @collection = []
        @loaded     = false
      end

      def writer(other_records)
        other_records = other_records.map do |other_record|
          other_record.is_a?(Hash) ? klass.new(other_record) : other_record
        end

        if reflection.counter_cache_column
          owner.send("#{reflection.counter_cache_column}=", other_records.size)
        end

        if reflection.touch_column
          owner.send("#{reflection.touch_column}=", Time.current)
        end

        delete(load_collection - other_records)
        merge_collections(load_collection, other_records)
        concat(other_records - load_collection)
      end
  
      def reader
        CollectionProxy.new(self)
      end

      def loaded?
        @loaded
      end

      def concat(*records)
        load_collection if owner.new_record?

        result = true

        records.flatten.each do |record|
          add_to_collection(record) do |r|
            result &&= record.save unless owner.new_record?
          end
        end

        result && records
      end

      def delete(records)
        if options[:autosave] || owner.new_record?
          records.each(&:mark_for_destruction)
        else
          record.destroy
        end
      end

      def scope
        search = klass.elastic_search.filter "#{reflection.belongs_to}_id" => owner.id
        if options[:as]
          search.filter! "#{reflection.belongs_to}_type" => owner.class.name
        end
        search
      end

      def load_collection
        unless @loaded
          @collection = merge_collections(persisted_collection, collection)
          @loaded = true
        end

        loaded = true
        collection
      end

      private
        def load_persisted_collection?
          !loaded? || owner.new_record?
        end

        def persisted_collection
          scope.to_a
        end
<commit_msg>remove meaningless variable assignment<commit_after>require 'active_support/core_ext/module/delegation'

module ElasticRecord
  module SearchesMany
    class Association
      attr_reader :owner, :reflection, :collection

      delegate :klass, :options, to: :reflection

      def initialize(owner, reflection)
        @owner      = owner
        @reflection = reflection
        @collection = []
        @loaded     = false
      end

      def writer(other_records)
        other_records = other_records.map do |other_record|
          other_record.is_a?(Hash) ? klass.new(other_record) : other_record
        end

        if reflection.counter_cache_column
          owner.send("#{reflection.counter_cache_column}=", other_records.size)
        end

        if reflection.touch_column
          owner.send("#{reflection.touch_column}=", Time.current)
        end

        delete(load_collection - other_records)
        merge_collections(load_collection, other_records)
        concat(other_records - load_collection)
      end
  
      def reader
        CollectionProxy.new(self)
      end

      def loaded?
        @loaded
      end

      def concat(*records)
        load_collection if owner.new_record?

        result = true

        records.flatten.each do |record|
          add_to_collection(record) do |r|
            result &&= record.save unless owner.new_record?
          end
        end

        result && records
      end

      def delete(records)
        if options[:autosave] || owner.new_record?
          records.each(&:mark_for_destruction)
        else
          record.destroy
        end
      end

      def scope
        search = klass.elastic_search.filter "#{reflection.belongs_to}_id" => owner.id
        if options[:as]
          search.filter! "#{reflection.belongs_to}_type" => owner.class.name
        end
        search
      end

      def load_collection
        unless @loaded
          @collection = merge_collections(persisted_collection, collection)
          @loaded = true
        end

        collection
      end

      private
        def load_persisted_collection?
          !loaded? || owner.new_record?
        end

        def persisted_collection
          scope.to_a
        end

{'fraction_non_alphanumeric': 0.07489380728817349, 'fraction_numerical': 0.0, 'mean_word_length': 1.9926421404682275, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2394432', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1198, 'n_words': 267}