<commit_before>require 'spec_helper'

module ClassMethod; end

class ClassMethod::MyModel < ActiveRecord::Base

  def self.the_async_method(arg1, arg2)
    WhatHappened.instance << "the static async method(#{arg1}, #{arg2})"
  end

end


module InstanceMethod; end

class InstanceMethod::MyModel < ActiveRecord::Base

  def the_async_method(arg1, arg2)
    WhatHappened.instance << "the instance async method(#{arg1}, #{arg2})"
  end

end

module AfterSave; end

# Setup the class to deal with a before_save callback
class AfterSave::MyModel < ActiveRecord::Base
  ayl_after_save :handle_after_save

  private

  def handle_after_save
    WhatHappened.instance << "handle after save"
  end
end

module AfterUpdate; end

# Setup the class to deal with a before_save callback
class AfterUpdate::MyModel < ActiveRecord::Base
  ayl_after_update :handle_after_update

  private

  def handle_after_update
    WhatHappened.instance << "handle after update"
  end
end

module AfterCreate; end

# Setup the class to deal with a before_save callback
class AfterCreate::MyModel < ActiveRecord::Base
  ayl_after_create :handle_after_create

  private

  def handle_after_create
    WhatHappened.instance << "handle after create"
  end
end

module ConditionalCallbacks; end

class ConditionalCallbacks::MyModel < ActiveRecord::Base
  attr_accessor :do_callback

  ayl_after_create :handle_after_create, :if => :should_do_callback?

  ayl_after_update :handle_after_update, :if => :should_do_callback?

  private

  def handle_after_create
    WhatHappened.instance << "handle after create"
  end

  def handle_after_update
    WhatHappened.instance << "handle after update"
  end

  def should_do_callback?
    @do_callback
  end
end

describe "Rails Extensions" do

  before(:each) do
    # Set up a null logger
    Ayl::Logger.instance.logger = Ayl::NullLogger.new

    # Set up an in-memory database so that we can quickly do ActiveRecord
    # tests.
    ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
                                            :database => ":memory:")

    ActiveRecord::Schema.define do
      create_table :my_models do |t|
        t.column :name, :string
      end
    end

  end

  after(:each) do
    WhatHappened.instance.clear
  end

  context "when handling lifecycle callbacks" do
    it "the ayl_after_save handler should fire when the model is saved" do
      
      model = AfterSave::MyModel.new(:name => 'spud')
      model.save.should be_true

      WhatHappened.instance.what_ran.should == [ 'handle after save' ] 
      WhatHappened.instance.clear

      model.update_attribute(:name, 'joan')

      WhatHappened.instance.what_ran.should == [ 'handle after save' ] 
    end

    it "the ayl_after_update handler should fire when the model is updated" do
      
      model = AfterUpdate::MyModel.new(:name => 'spud')
      model.save.should be_true

      WhatHappened.instance.what_ran.should be_nil

      model.update_attribute(:name, 'joan')

      WhatHappened.instance.what_ran.should == [ 'handle after update' ]
    end

    it "the ayl_after_create handler should fire when the model is created" do
      
      model = AfterCreate::MyModel.new(:name => 'spud')
      model.save.should be_true

      WhatHappened.instance.what_ran.should == [ 'handle after create' ]
      WhatHappened.instance.clear

      model.update_attribute(:name, 'joan')

      WhatHappened.instance.what_ran.should be_nil
    end

  end

  context "when using the instance extensions" do

    it "should represent the instance of a particular model using a 'find'" do
      model = InstanceMethod::MyModel.create(:name => 'loud')

      model.to_rrepr.should == "InstanceMethod::MyModel.find(#{model.id})"
    end

    it "should invoke the instance method asynchronously with no options" do
      model = InstanceMethod::MyModel.create(:name => 'loud')

      model.ayl_send(:the_async_method, "first", "second")
      
      WhatHappened.instance.what_ran.should == [ "the instance async method(first, second)" ]
    end

    it "should invoke the instance method asynchronously with options" do
      model = InstanceMethod::MyModel.create(:name => 'loud')

      model.ayl_send_opts(:the_async_method, {}, "first", "second")
      
      WhatHappened.instance.what_ran.should == [ "the instance async method(first, second)" ]
    end

  end

  context "when using the class extensions" do

    it "should invoke the static method asynchronously with no options" do
      ClassMethod::MyModel.ayl_send(:the_async_method, "first", "second")
      
      WhatHappened.instance.what_ran.should == [ "the static async method(first, second)" ]
    end

    it "should invoke the instance method asynchronously with options" do
      ClassMethod::MyModel.ayl_send_opts(:the_async_method, {}, "first", "second")
      <commit_msg>Corrected test to deal with the unscoped find.<commit_after>require 'spec_helper'

module ClassMethod; end

class ClassMethod::MyModel < ActiveRecord::Base

  def self.the_async_method(arg1, arg2)
    WhatHappened.instance << "the static async method(#{arg1}, #{arg2})"
  end

end


module InstanceMethod; end

class InstanceMethod::MyModel < ActiveRecord::Base

  def the_async_method(arg1, arg2)
    WhatHappened.instance << "the instance async method(#{arg1}, #{arg2})"
  end

end

module AfterSave; end

# Setup the class to deal with a before_save callback
class AfterSave::MyModel < ActiveRecord::Base
  ayl_after_save :handle_after_save

  private

  def handle_after_save
    WhatHappened.instance << "handle after save"
  end
end

module AfterUpdate; end

# Setup the class to deal with a before_save callback
class AfterUpdate::MyModel < ActiveRecord::Base
  ayl_after_update :handle_after_update

  private

  def handle_after_update
    WhatHappened.instance << "handle after update"
  end
end

module AfterCreate; end

# Setup the class to deal with a before_save callback
class AfterCreate::MyModel < ActiveRecord::Base
  ayl_after_create :handle_after_create

  private

  def handle_after_create
    WhatHappened.instance << "handle after create"
  end
end

module ConditionalCallbacks; end

class ConditionalCallbacks::MyModel < ActiveRecord::Base
  attr_accessor :do_callback

  ayl_after_create :handle_after_create, :if => :should_do_callback?

  ayl_after_update :handle_after_update, :if => :should_do_callback?

  private

  def handle_after_create
    WhatHappened.instance << "handle after create"
  end

  def handle_after_update
    WhatHappened.instance << "handle after update"
  end

  def should_do_callback?
    @do_callback
  end
end

describe "Rails Extensions" do

  before(:each) do
    # Set up a null logger
    Ayl::Logger.instance.logger = Ayl::NullLogger.new

    # Set up an in-memory database so that we can quickly do ActiveRecord
    # tests.
    ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
                                            :database => ":memory:")

    ActiveRecord::Schema.define do
      create_table :my_models do |t|
        t.column :name, :string
      end
    end

  end

  after(:each) do
    WhatHappened.instance.clear
  end

  context "when handling lifecycle callbacks" do
    it "the ayl_after_save handler should fire when the model is saved" do
      
      model = AfterSave::MyModel.new(:name => 'spud')
      model.save.should be_true

      WhatHappened.instance.what_ran.should == [ 'handle after save' ] 
      WhatHappened.instance.clear

      model.update_attribute(:name, 'joan')

      WhatHappened.instance.what_ran.should == [ 'handle after save' ] 
    end

    it "the ayl_after_update handler should fire when the model is updated" do
      
      model = AfterUpdate::MyModel.new(:name => 'spud')
      model.save.should be_true

      WhatHappened.instance.what_ran.should be_nil

      model.update_attribute(:name, 'joan')

      WhatHappened.instance.what_ran.should == [ 'handle after update' ]
    end

    it "the ayl_after_create handler should fire when the model is created" do
      
      model = AfterCreate::MyModel.new(:name => 'spud')
      model.save.should be_true

      WhatHappened.instance.what_ran.should == [ 'handle after create' ]
      WhatHappened.instance.clear

      model.update_attribute(:name, 'joan')

      WhatHappened.instance.what_ran.should be_nil
    end

  end

  context "when using the instance extensions" do

    it "should represent the instance of a particular model using a 'find'" do
      model = InstanceMethod::MyModel.create(:name => 'loud')

      model.to_rrepr.should == "InstanceMethod::MyModel.unscoped.find(#{model.id})"
    end

    it "should invoke the instance method asynchronously with no options" do
      model = InstanceMethod::MyModel.create(:name => 'loud')

      model.ayl_send(:the_async_method, "first", "second")
      
      WhatHappened.instance.what_ran.should == [ "the instance async method(first, second)" ]
    end

    it "should invoke the instance method asynchronously with options" do
      model = InstanceMethod::MyModel.create(:name => 'loud')

      model.ayl_send_opts(:the_async_method, {}, "first", "second")
      
      WhatHappened.instance.what_ran.should == [ "the instance async method(first, second)" ]
    end

  end

  context "when using the class extensions" do

    it "should invoke the static method asynchronously with no options" do
      ClassMethod::MyModel.ayl_send(:the_async_method, "first", "second")
      
      WhatHappened.instance.what_ran.should == [ "the static async method(first, second)" ]
    end

    it "should invoke the instance method asynchronously with options" do
      ClassMethod::MyModel.ayl_send_opts(:the_async_method, {}, "first", "second")
      
{'fraction_non_alphanumeric': 0.09023789991796555, 'fraction_numerical': 0.0018457752255947497, 'mean_word_length': 3.548973880597015, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3803888', 'n_tokens_mistral': 2986, 'n_tokens_neox': 2888, 'n_words': 807}