diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index e21559ae..6dbbe257 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -281,15 +281,26 @@ module ThoughtBot # :nodoc: through = get_options!(associations, :through) klass = model_class associations.each do |association| - should "have many #{association}#{" through #{through}" if through}" do + name = "have many #{association}" + name += " through #{through}" if through + should name do reflection = klass.reflect_on_association(association) assert reflection, "#{klass.name} does not have any relationship to #{association}" assert_equal :has_many, reflection.macro + if through through_reflection = klass.reflect_on_association(through) assert through_reflection, "#{klass.name} does not have any relationship to #{through}" assert_equal(through, reflection.options[:through]) end + + unless reflection.options[:through] + # This is not a through association, so check for the existence of the foreign key on the other table + fk = reflection.options[:foreign_key] || "#{klass.name.downcase}_id" + associated_klass = reflection.options[:class_name] || association.to_s.classify + associated_klass = associated_klass.constantize + assert associated_klass.column_names.include?(fk.to_s), "#{associated_klass.name} does not have a #{fk} foreign key." + end end end end diff --git a/test/README b/test/README new file mode 100644 index 00000000..3906de55 --- /dev/null +++ b/test/README @@ -0,0 +1,8 @@ +The tests for should have two dependencies that I know of: + +* Rails version 1.2.3 +* A working sqlite3 installation. + +If you have problems running these tests, please notify the shoulda mailing list: shoulda@googlegroups.com + +- Tammer Saleh \ No newline at end of file diff --git a/test/rails_root/app/controllers/posts_controller.rb b/test/rails_root/app/controllers/posts_controller.rb index 3bf197bd..87b9df10 100644 --- a/test/rails_root/app/controllers/posts_controller.rb +++ b/test/rails_root/app/controllers/posts_controller.rb @@ -49,7 +49,7 @@ class PostsController < ApplicationController respond_to do |format| if @post.update_attributes(params[:post]) flash[:notice] = 'Post was successfully updated.' - format.html { redirect_to post_url(@post) } + format.html { redirect_to post_url(@post.user, @post) } format.xml { head :ok } else format.html { render :action => "edit" } @@ -65,7 +65,7 @@ class PostsController < ApplicationController flash[:notice] = "Post was removed" respond_to do |format| - format.html { redirect_to posts_url } + format.html { redirect_to posts_url(@post.user) } format.xml { head :ok } end end diff --git a/test/rails_root/app/models/dog.rb b/test/rails_root/app/models/dog.rb new file mode 100644 index 00000000..2532fee5 --- /dev/null +++ b/test/rails_root/app/models/dog.rb @@ -0,0 +1,3 @@ +class Dog < ActiveRecord::Base + belongs_to :user, :foreign_key => :owner_id +end diff --git a/test/rails_root/app/models/user.rb b/test/rails_root/app/models/user.rb index 79f50907..31c96f31 100644 --- a/test/rails_root/app/models/user.rb +++ b/test/rails_root/app/models/user.rb @@ -1,5 +1,6 @@ class User < ActiveRecord::Base has_many :posts + has_many :dogs, :foreign_key => :owner_id attr_protected :password validates_format_of :email, :with => /\w*@\w*.com/ diff --git a/test/rails_root/config/environment.rb b/test/rails_root/config/environment.rb index e3f7a75c..725c7bb4 100644 --- a/test/rails_root/config/environment.rb +++ b/test/rails_root/config/environment.rb @@ -1,5 +1,7 @@ # Specifies gem version of Rails to use when vendor/rails is not present -#RAILS_GEM_VERSION = '1.1.6' +old_verbose, $VERBOSE = $VERBOSE, nil +RAILS_GEM_VERSION = '1.2.3' +$VERBOSE = old_verbose require File.join(File.dirname(__FILE__), 'boot') diff --git a/test/rails_root/db/migrate/005_create_dogs.rb b/test/rails_root/db/migrate/005_create_dogs.rb new file mode 100644 index 00000000..a0d8e035 --- /dev/null +++ b/test/rails_root/db/migrate/005_create_dogs.rb @@ -0,0 +1,11 @@ +class CreateDogs < ActiveRecord::Migration + def self.up + create_table :dogs do |t| + t.column :owner_id, :integer + end + end + + def self.down + drop_table :dogs + end +end diff --git a/test/unit/dog_test.rb b/test/unit/dog_test.rb new file mode 100644 index 00000000..390d142b --- /dev/null +++ b/test/unit/dog_test.rb @@ -0,0 +1,6 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class DogTest < Test::Unit::TestCase + load_all_fixtures + should_belong_to :user +end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index b78ca7cb..2f0bc4be 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -4,6 +4,7 @@ class UserTest < Test::Unit::TestCase load_all_fixtures should_have_many :posts + should_have_many :dogs should_not_allow_values_for :email, "blah", "b lah" should_allow_values_for :email, "a@b.com", "asdf@asdf.com"