<commit_before>module Representer

  # Representer::Simple is an representer which allows work on ActiveRecord::Base instances,
  # also during the second pass
  #
  # It is currently in an experimental phase
  class Simple < Base

    def run_second_pass(prepared)
      prepared.each { |record, hash| second_pass(record, hash) } unless skip_second_pass?
    end

    def finalize(prepared)
      prepared = prepared.collect { |record, hash| hash }
      @returns_many ? prepared : prepared[0]
    end

    def second_pass(record, prepared_hash)
      load_aggregates
      scoped_hash = if self.class.representable_namespace
        prepared_hash[self.class.representable_namespace]
      else
        prepared_hash
      end
      self.class.representable_fields.each do |field|
        if field.is_a?(Array)
          field, method = field
        else
          field, method = field, field
        end
        scoped_hash[field] = self.send(method, record, scoped_hash)
      end
      scoped_hash
    end

    def first_pass(record)
      hash = extract_attributes(record)

      aggregate_keys record, hash

      self.class.representable_methods.each do |method|
        if method.is_a?(Array)
          field, method = method
        else
          field, method = method, method
        end
        hash[field] = record.send(method)
      end

      if self.class.representable_namespace
        { self.class.representable_namespace => hash }
      else
        hash
      end
      [record, hash]
    end

    def aggregate_keys(record, hash)
      @aggregates['id'].push record.id
      self.class.aggregation_keys.each do |key|
        @aggregates[key] ||= []
        @aggregates[key].push record.send(key)
      end
    end

  end

end
<commit_msg>Fix for a problem with second pass in Representer::Simple<commit_after>module Representer

  # Representer::Simple is an representer which allows work on ActiveRecord::Base instances,
  # also during the second pass
  #
  # It is currently in an experimental phase
  class Simple < Base

    def run_second_pass(prepared)
      prepared.each { |record, hash| second_pass(record, hash) } unless skip_second_pass?
    end

    def finalize(prepared)
      prepared = prepared.collect { |record, hash| hash }
      @returns_many ? prepared : prepared[0]
    end

    def second_pass(record, prepared_hash)
      load_aggregates
      scoped_hash = if self.class.representable_namespace
        prepared_hash[self.class.representable_namespace]
      else
        prepared_hash
      end
      self.class.representable_fields.each do |field|
        if field.is_a?(Array)
          field, method = field
        else
          field, method = field, field
        end
        scoped_hash[field] = self.send(method, record, scoped_hash)
      end
      scoped_hash
    end

    def first_pass(record)
      hash = extract_attributes(record)

      aggregate_keys record, hash

      self.class.representable_methods.each do |method|
        if method.is_a?(Array)
          field, method = method
        else
          field, method = method, method
        end
        hash[field] = record.send(method)
      end

      if self.class.representable_namespace
        hash = { self.class.representable_namespace => hash }
      end
      [record, hash]
    end

    def aggregate_keys(record, hash)
      @aggregates['id'].push record.id
      self.class.aggregation_keys.each do |key|
        @aggregates[key] ||= []
        @aggregates[key].push record.send(key)
      end
    end

  end

end

{'fraction_non_alphanumeric': 0.07852518998029834, 'fraction_numerical': 0.0005629045876723895, 'mean_word_length': 2.4205967276227143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '740874', 'n_tokens_mistral': 1048, 'n_tokens_neox': 1009, 'n_words': 265}