<commit_before># This class is inherited by the has_many and has_many_and_belongs_to_many association classes 

require 'active_record/associations'

module ActiveRecord::Associations::Builder
  class CollectionAssociation < Association #:nodoc:

    CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]

    def valid_options
      super + [:table_name, :before_add,
               :after_add, :before_remove, :after_remove, :extend]
    end

    attr_reader :block_extension

    def initialize(name, scope, options)
      super
      @mod = nil
      if block_given?
        @mod = Module.new(&Proc.new)
        @scope = wrap_scope @scope, @mod
      end
    end

    def define_callbacks(model, reflection)
      super
      CALLBACKS.each { |callback_name| define_callback(model, callback_name) }
    end

    def define_extensions(model)
      if @mod
        extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
        model.parent.const_set(extension_module_name, @mod)
      end
    end

    def define_callback(model, callback_name)
      full_callback_name = "#{callback_name}_for_#{name}"

      # TODO : why do i need method_defined? I think its because of the inheritance chain
      model.class_attribute full_callback_name unless model.method_defined?(full_callback_name)
      callbacks = Array(options[callback_name.to_sym]).map do |callback|
        case callback
        when Symbol
          ->(method, owner, record) { owner.send(callback, record) }
        when Proc
          ->(method, owner, record) { callback.call(owner, record) }
        else
          ->(method, owner, record) { callback.send(method, owner, record) }
        end
      end
      model.send "#{full_callback_name}=", callbacks
    end

    # Defines the setter and getter methods for the collection_singular_ids.

    def define_readers(mixin)
      super

      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids
          association(:#{name}).ids_reader
        end
      CODE
    end

    def define_writers(mixin)
      super

      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids=(ids)
          association(:#{name}).ids_writer(ids)
        end
      CODE
    end

    private

    def wrap_scope(scope, mod)
      if scope
        proc { |owner| instance_exec(owner, &scope).extending(mod) }
      else
        proc { extending(mod) }
      end
    end
  end
end
<commit_msg>decouple define_callback from the instance<commit_after># This class is inherited by the has_many and has_many_and_belongs_to_many association classes 

require 'active_record/associations'

module ActiveRecord::Associations::Builder
  class CollectionAssociation < Association #:nodoc:

    CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]

    def valid_options
      super + [:table_name, :before_add,
               :after_add, :before_remove, :after_remove, :extend]
    end

    attr_reader :block_extension

    def initialize(name, scope, options)
      super
      @mod = nil
      if block_given?
        @mod = Module.new(&Proc.new)
        @scope = wrap_scope @scope, @mod
      end
    end

    def define_callbacks(model, reflection)
      super
      CALLBACKS.each { |callback_name|
        define_callback(model, callback_name, reflection.name, reflection.options)
      }
    end

    def define_extensions(model)
      if @mod
        extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
        model.parent.const_set(extension_module_name, @mod)
      end
    end

    def define_callback(model, callback_name, name, options)
      full_callback_name = "#{callback_name}_for_#{name}"

      # TODO : why do i need method_defined? I think its because of the inheritance chain
      model.class_attribute full_callback_name unless model.method_defined?(full_callback_name)
      callbacks = Array(options[callback_name.to_sym]).map do |callback|
        case callback
        when Symbol
          ->(method, owner, record) { owner.send(callback, record) }
        when Proc
          ->(method, owner, record) { callback.call(owner, record) }
        else
          ->(method, owner, record) { callback.send(method, owner, record) }
        end
      end
      model.send "#{full_callback_name}=", callbacks
    end

    # Defines the setter and getter methods for the collection_singular_ids.

    def define_readers(mixin)
      super

      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids
          association(:#{name}).ids_reader
        end
      CODE
    end

    def define_writers(mixin)
      super

      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids=(ids)
          association(:#{name}).ids_writer(ids)
        end
      CODE
    end

    private

    def wrap_scope(scope, mod)
      if scope
        proc { |owner| instance_exec(owner, &scope).extending(mod) }
      else
        proc { extending(mod) }
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0920257360109183, 'fraction_numerical': 0.0007798791187365958, 'mean_word_length': 2.7417943107221006, 'pattern_counts': {'":': 0, '<': 13, '<?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': '7662910', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1528, 'n_words': 374}