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:
parent
6b91e8b325
commit
c397d016f8
6 changed files with 32 additions and 2 deletions
|
@ -355,15 +355,20 @@ module ThoughtBot # :nodoc:
|
||||||
assert reflection, "#{klass.name} does not have any relationship to #{association}"
|
assert reflection, "#{klass.name} does not have any relationship to #{association}"
|
||||||
assert_equal :has_one, reflection.macro
|
assert_equal :has_one, reflection.macro
|
||||||
|
|
||||||
|
associated_klass = (reflection.options[:class_name] || association.to_s.camelize).constantize
|
||||||
|
|
||||||
if reflection.options[:foreign_key]
|
if reflection.options[:foreign_key]
|
||||||
fk = reflection.options[:foreign_key]
|
fk = reflection.options[:foreign_key]
|
||||||
elsif reflection.options[:as]
|
elsif reflection.options[:as]
|
||||||
fk = reflection.options[:as].to_s.foreign_key
|
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
|
else
|
||||||
fk = klass.name.foreign_key
|
fk = klass.name.foreign_key
|
||||||
end
|
end
|
||||||
associated_klass = (reflection.options[:class_name] || association.to_s.classify).constantize
|
assert associated_klass.column_names.include?(fk.to_s),
|
||||||
assert associated_klass.column_names.include?(fk.to_s), "#{associated_klass.name} does not have a #{fk} foreign key."
|
"#{associated_klass.name} does not have a #{fk} foreign key."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
3
test/rails_root/app/models/address.rb
Normal file
3
test/rails_root/app/models/address.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class Address < ActiveRecord::Base
|
||||||
|
belongs_to :addressable, :polymorphic => true
|
||||||
|
end
|
|
@ -2,6 +2,8 @@ class User < ActiveRecord::Base
|
||||||
has_many :posts
|
has_many :posts
|
||||||
has_many :dogs, :foreign_key => :owner_id
|
has_many :dogs, :foreign_key => :owner_id
|
||||||
|
|
||||||
|
has_one :address, :as => :addressable
|
||||||
|
|
||||||
attr_protected :password
|
attr_protected :password
|
||||||
|
|
||||||
validates_format_of :email, :with => /\w*@\w*.com/
|
validates_format_of :email, :with => /\w*@\w*.com/
|
||||||
|
|
12
test/rails_root/db/migrate/006_create_addresses.rb
Normal file
12
test/rails_root/db/migrate/006_create_addresses.rb
Normal 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
|
6
test/unit/address_test.rb
Normal file
6
test/unit/address_test.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
require File.dirname(__FILE__) + '/../test_helper'
|
||||||
|
|
||||||
|
class AddressTest < Test::Unit::TestCase
|
||||||
|
load_all_fixtures
|
||||||
|
should_belong_to :addressable
|
||||||
|
end
|
|
@ -6,6 +6,8 @@ class UserTest < Test::Unit::TestCase
|
||||||
should_have_many :posts
|
should_have_many :posts
|
||||||
should_have_many :dogs
|
should_have_many :dogs
|
||||||
|
|
||||||
|
should_have_one :address
|
||||||
|
|
||||||
should_not_allow_values_for :email, "blah", "b lah"
|
should_not_allow_values_for :email, "blah", "b lah"
|
||||||
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
|
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
|
||||||
should_ensure_length_in_range :email, 1..100
|
should_ensure_length_in_range :email, 1..100
|
||||||
|
|
Loading…
Add table
Reference in a new issue