<commit_before>module MetaSearch

  module JoinDependency

    def self.included(base)
      base.class_eval do
        alias_method_chain :graft, :metasearch
      end
    end

    def graft_with_metasearch(*associations)
      associations.each do |association|
        join_associations.detect {|a| association == a} ||
        (
          association.class == MetaSearch::PolymorphicJoinAssociation ?
          build_with_metasearch(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass) :
          build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
        )
      end
      self
    end

    protected

      def build_with_metasearch(associations, parent = nil, join_type = Arel::Nodes::InnerJoin, polymorphic_class = nil)
        parent ||= @joins.last

        case associations
        when Symbol, String
          reflection = parent.reflections[associations.to_s.intern] or
            raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?"
          unless (association = find_join_association(reflection, parent)) && (!polymorphic_class || association.active_record == polymorphic_class)
            @reflections << reflection
            if reflection.options[:polymorphic]
              raise ArgumentError, "You can't create a polymorphic belongs_to join without specifying the polymorphic class!" unless polymorphic_class
              association = build_polymorphic_join_association(reflection, parent, polymorphic_class)
            else
              association = build_join_association(reflection, parent)
            end
            association.join_type = join_type
            @joins << association
          end
          association
        else
          build(associations, parent, join_type)
        end
      end

      def build_polymorphic_join_association(reflection, parent, klass)
        PolymorphicJoinAssociation.new(reflection, self, klass, parent)
      end
  end

  class PolymorphicJoinAssociation < ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation

    def initialize(reflection, join_dependency, polymorphic_class, parent = nil)
      reflection.check_validity!
      @active_record = polymorphic_class
      @cached_record = {}
      @join_dependency    = join_dependency
      @parent             = parent || join_dependency.join_base
      @reflection         = reflection.clone
      @reflection.instance_eval "def klass; #{polymorphic_class} end;"
      @aliased_prefix     = "t#{ join_dependency.joins.size }"
      @parent_table_name  = @parent.active_record.table_name
      @aliased_table_name = aliased_table_name_for(table_name)
      @join               = nil
      @join_type          = Arel::Nodes::InnerJoin
    end

    def ==(other)
      other.class == self.class &&
      other.reflection == reflection &&
      other.active_record == active_record &&
      other.parent == parent
    end
<commit_msg>No need to define klass method, just set the @klass instance variable<commit_after>module MetaSearch

  module JoinDependency

    def self.included(base)
      base.class_eval do
        alias_method_chain :graft, :metasearch
      end
    end

    def graft_with_metasearch(*associations)
      associations.each do |association|
        join_associations.detect {|a| association == a} ||
        (
          association.class == MetaSearch::PolymorphicJoinAssociation ?
          build_with_metasearch(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass) :
          build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
        )
      end
      self
    end

    protected

      def build_with_metasearch(associations, parent = nil, join_type = Arel::Nodes::InnerJoin, polymorphic_class = nil)
        parent ||= @joins.last

        case associations
        when Symbol, String
          reflection = parent.reflections[associations.to_s.intern] or
            raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?"
          unless (association = find_join_association(reflection, parent)) && (!polymorphic_class || association.active_record == polymorphic_class)
            @reflections << reflection
            if reflection.options[:polymorphic]
              raise ArgumentError, "You can't create a polymorphic belongs_to join without specifying the polymorphic class!" unless polymorphic_class
              association = build_polymorphic_join_association(reflection, parent, polymorphic_class)
            else
              association = build_join_association(reflection, parent)
            end
            association.join_type = join_type
            @joins << association
          end
          association
        else
          build(associations, parent, join_type)
        end
      end

      def build_polymorphic_join_association(reflection, parent, klass)
        PolymorphicJoinAssociation.new(reflection, self, klass, parent)
      end
  end

  class PolymorphicJoinAssociation < ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation

    def initialize(reflection, join_dependency, polymorphic_class, parent = nil)
      reflection.check_validity!
      @active_record = polymorphic_class
      @cached_record = {}
      @join_dependency    = join_dependency
      @parent             = parent || join_dependency.join_base
      @reflection         = reflection.clone
      @reflection.instance_variable_set(:"@klass", polymorphic_class)
      @aliased_prefix     = "t#{ join_dependency.joins.size }"
      @parent_table_name  = @parent.active_record.table_name
      @aliased_table_name = aliased_table_name_for(table_name)
      @join               = nil
      @join_type          = Arel::Nodes::InnerJoin
    end

    def ==(other)
      other.class == self.class &&
      other.reflection == reflection &&
      other.active_record == active_record &&
      other.parent == parent
    end

{'fraction_non_alphanumeric': 0.07834918587780107, 'fraction_numerical': 0.0, 'mean_word_length': 2.879924953095685, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '4719741', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1629, 'n_words': 354}