Sha256: c6cdb6d3e8a33857c4b9ee65fd238787d4f641f343c2170ff2518f11b3c57433

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require  File.dirname(__FILE__) + '/joinfix_test_helper'

class BtMigration < ActiveRecord::Migration
  def self.up
	create_table :bt_parents do |t|
		t.column :field, :string
		t.column :bt_child_id, :integer
		t.column :alt_id, :integer
		t.column :polymorph_id, :integer
		t.column :polymorph_type, :string
	end	
	create_table :bt_children do |t|
		t.column :field, :string
	end	
	create_table :polymorphic_children do |t|
		t.column :field, :string
	end
  end

  def self.down
	drop_table :bt_parents
	drop_table :bt_children
	drop_table :polymorphic_children
  end
end

class BtParent < ActiveRecord::Base
	belongs_to :bt_child
	belongs_to :child, 
		:foreign_key => "alt_id",
		:class_name => "BtChild"
	
	belongs_to :polymorph, 
		:polymorphic => true
end

class BtChild < ActiveRecord::Base
end

class PolymorphicChild < ActiveRecord::Base
	has_many :bt_parents, 
		:as => :polymorph
end

class BelongsToTest < Test::Unit::TestCase
	fixtures :bt_parents, :bt_children, :polymorphic_children
	
	def setup
	end
	
	def teardown
	end
	
	def test_belongs_to
		database_test(BtMigration) do 
			entry = bt_parents(:without_child)
			assert_equal "without_child", entry.field
			assert_nil entry.bt_child
			assert_nil entry.child
			
			entry = bt_parents(:with_bt_child)
			assert_equal "with_bt_child", entry.field
			assert_equal "1", entry.bt_child.field
			assert_nil entry.child
			
			entry = bt_parents(:with_child)
			assert_equal "with_child", entry.field
			assert_nil entry.bt_child
			assert_equal "2", entry.child.field
			
			entry = bt_parents(:with_both)
			assert_equal "with_both", entry.field
			assert_equal "3", entry.bt_child.field
			assert_equal "4", entry.child.field
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joinfix-0.1.0 test/belongs_to_test.rb