1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@370 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2008-02-28 21:40:42 +00:00
parent 6b91e8b325
commit c397d016f8
6 changed files with 32 additions and 2 deletions

View file

@ -355,15 +355,20 @@ module ThoughtBot # :nodoc:
assert reflection, "#{klass.name} does not have any relationship to #{association}"
assert_equal :has_one, reflection.macro
associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
if reflection.options[:foreign_key]
fk = reflection.options[:foreign_key]
elsif reflection.options[:as]
fk = reflection.options[:as].to_s.foreign_key
fk_type = fk.gsub(/_id$/, '_type')
assert associated_klass.column_names.include?(fk_type),
"#{associated_klass.name} does not have a #{fk_type} column."
else
fk = klass.name.foreign_key
end
associated_klass = (reflection.options[:class_name] || association.to_s.classify).constantize
assert associated_klass.column_names.include?(fk.to_s), "#{associated_klass.name} does not have a #{fk} foreign key."
assert associated_klass.column_names.include?(fk.to_s),
"#{associated_klass.name} does not have a #{fk} foreign key."
end
end
end

View file

@ -0,0 +1,3 @@
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end

View file

@ -1,6 +1,8 @@
class User < ActiveRecord::Base
has_many :posts
has_many :dogs, :foreign_key => :owner_id
has_one :address, :as => :addressable
attr_protected :password

View file

@ -0,0 +1,12 @@
class CreateAddresses < ActiveRecord::Migration
def self.up
create_table :addresses do |t|
t.column :addressable_id, :integer
t.column :addressable_type, :string
end
end
def self.down
drop_table :addresses
end
end

View file

@ -0,0 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper'
class AddressTest < Test::Unit::TestCase
load_all_fixtures
should_belong_to :addressable
end

View file

@ -6,6 +6,8 @@ class UserTest < Test::Unit::TestCase
should_have_many :posts
should_have_many :dogs
should_have_one :address
should_not_allow_values_for :email, "blah", "b lah"
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
should_ensure_length_in_range :email, 1..100