<commit_before>require "cases/helper"
require 'models/developer'
require 'models/computer'

class CallbackDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  class << self
    def callback_string(callback_method)
      "history << [#{callback_method.to_sym.inspect}, :string]"
    end

    def callback_proc(callback_method)
      Proc.new { |model| model.history << [callback_method, :proc] }
    end

    def define_callback_method(callback_method)
      define_method(callback_method) do
        self.history << [callback_method, :method]
      end
      send(callback_method, :"#{callback_method}")
    end

    def callback_object(callback_method)
      klass = Class.new
      klass.send(:define_method, callback_method) do |model|
        model.history << [callback_method, :object]
      end
      klass.new
    end
  end

  ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
    next if callback_method.to_s =~ /^around_/
    define_callback_method(callback_method)
    send(callback_method, callback_string(callback_method))
    send(callback_method, callback_proc(callback_method))
    send(callback_method, callback_object(callback_method))
    send(callback_method) { |model| model.history << [callback_method, :block] }
  end

  def history
    @history ||= []
  end
end

class CallbackDeveloperWithFalseValidation < CallbackDeveloper
  before_validation proc { |model| model.history << [:before_validation, :returning_false]; false }
  before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
end

class ParentDeveloper < ActiveRecord::Base
  self.table_name = 'developers'
  attr_accessor :after_save_called
  before_validation {|record| record.after_save_called = true}
end

class ChildDeveloper < ParentDeveloper

end

class RecursiveCallbackDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  before_save :on_before_save
  after_save :on_after_save

  attr_reader :on_before_save_called, :on_after_save_called

  def on_before_save
    @on_before_save_called ||= 0
    @on_before_save_called += 1
    save unless @on_before_save_called > 1
  end

  def on_after_save
    @on_after_save_called ||= 0
    @on_after_save_called += 1
    save unless @on_after_save_called > 1
  end
end

class ImmutableDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  validates_inclusion_of :salary, :in => 50000..200000

  before_save :cancel
  before_destroy :cancel

  def cancelled?
    @cancelled == true
  end

  private
    def cancel
      @cancelled = true
      false
    end
end

class ImmutableMethodDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  validates_inclusion_of :salary, :in => 50000..200000

  def cancelled?
    @cancelled == true
  end

  before_save do
    @cancelled = true
    false
  end

  before_destroy do
    @cancelled = true
    false
  end
end

class OnCallbacksDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  before_validation { history << :before_validation }
  before_validation(:on => :create){ history << :before_validation_on_create }
  before_validation(:on => :update){ history << :before_validation_on_update }

  validate do
    history << :validate
  end

  after_validation { history << :after_validation }
  after_validation(:on => :create){ history << :after_validation_on_create }
  after_validation(:on => :update){ history << :after_validation_on_update }

  def history
    @history ||= []
  end
end

class ContextualCallbacksDeveloper < ActiveRecord::Base
  self.table_name = 'developers'
<commit_msg>Remove unused "Developer" fixtures from tests<commit_after>require "cases/helper"
require 'models/developer'
require 'models/computer'

class CallbackDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  class << self
    def callback_string(callback_method)
      "history << [#{callback_method.to_sym.inspect}, :string]"
    end

    def callback_proc(callback_method)
      Proc.new { |model| model.history << [callback_method, :proc] }
    end

    def define_callback_method(callback_method)
      define_method(callback_method) do
        self.history << [callback_method, :method]
      end
      send(callback_method, :"#{callback_method}")
    end

    def callback_object(callback_method)
      klass = Class.new
      klass.send(:define_method, callback_method) do |model|
        model.history << [callback_method, :object]
      end
      klass.new
    end
  end

  ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
    next if callback_method.to_s =~ /^around_/
    define_callback_method(callback_method)
    send(callback_method, callback_string(callback_method))
    send(callback_method, callback_proc(callback_method))
    send(callback_method, callback_object(callback_method))
    send(callback_method) { |model| model.history << [callback_method, :block] }
  end

  def history
    @history ||= []
  end
end

class CallbackDeveloperWithFalseValidation < CallbackDeveloper
  before_validation proc { |model| model.history << [:before_validation, :returning_false]; false }
  before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
end

class ParentDeveloper < ActiveRecord::Base
  self.table_name = 'developers'
  attr_accessor :after_save_called
  before_validation {|record| record.after_save_called = true}
end

class ChildDeveloper < ParentDeveloper

end

class ImmutableDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  validates_inclusion_of :salary, :in => 50000..200000

  before_save :cancel
  before_destroy :cancel

  private
    def cancel
      false
    end
end

class OnCallbacksDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

  before_validation { history << :before_validation }
  before_validation(:on => :create){ history << :before_validation_on_create }
  before_validation(:on => :update){ history << :before_validation_on_update }

  validate do
    history << :validate
  end

  after_validation { history << :after_validation }
  after_validation(:on => :create){ history << :after_validation_on_create }
  after_validation(:on => :update){ history << :after_validation_on_update }

  def history
    @history ||= []
  end
end

class ContextualCallbacksDeveloper < ActiveRecord::Base
  self.table_name = 'developers'

{'fraction_non_alphanumeric': 0.09510313336482444, 'fraction_numerical': 0.006140765233821445, 'mean_word_length': 4.447684391080617, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2388442', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1907, 'n_words': 418}