Sha256: 7c81e5f27d7e563728a42b7965164a8ab916ce2076d5e0d85e538eb67e3ca31b

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'test_helper'

class User < ActiveRecord::Base
end

class NoteWithUserWithDependency < ActiveRecord::Base
  set_table_name "notes"
  has_many :memos, :class_name => "MemoWithUser", :foreign_key => "note_id", :dependent => :destroy

  protected
  def before_destroy
    raise "can't destroy without operator" unless operator
  end
end

class MemoWithUserWithDependency < ActiveRecord::Base
  set_table_name "memos"

  protected
  def before_destroy
    raise "can't destroy without operator" unless operator
  end
end


class RecordWithOperatorHasManyDependentTest < ActiveSupport::TestCase
  def setup
    RecordWithOperator.config[:user_class_name] = "User"
    @user1 = User.create!(:name => "user1")
    raise "@user1.id is nil" unless @user1.id
    @user2 = User.create!(:name => "user2")
    raise "@user2.id is nil" unless @user2.id
    @note_created_by_user1 = NoteWithUserWithDependency.create!(:body => "test", :operator => @user1)
    @note_created_by_user1.memos.create!
    @note_created_by_user1.memos.create!
    @note_created_by_user1.reload
  end

  def test_memos_should_be_destroyed_when_note_is_destroyed
    @note_created_by_user1.destroy
    assert_nil NoteWithUserWithDependency.find_by_id(@note_created_by_user1.id)
    assert MemoWithUserWithDependency.find_all_by_note_id(@note_created_by_user1.id).empty?
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
record_with_operator-0.0.22 test/record_with_operator_has_many_dependent_test.rb