This commit is contained in:
Gabe Berke-Williams 2012-03-23 19:50:08 -04:00
parent c10cf343ae
commit 41bccc8cbf
3 changed files with 20 additions and 18 deletions

View File

@ -166,7 +166,7 @@ module Shoulda # :nodoc:
if @through == reflection.options[:through] if @through == reflection.options[:through]
true true
else else
@missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " << @missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " +
"but got it through #{reflection.options[:through]}" "but got it through #{reflection.options[:through]}"
false false
end end
@ -210,7 +210,7 @@ module Shoulda # :nodoc:
def join_table_exists? def join_table_exists?
if @macro != :has_and_belongs_to_many || if @macro != :has_and_belongs_to_many ||
::ActiveRecord::Base.connection.tables.include?(join_table.to_s) ::ActiveRecord::Base.connection.tables.include?(join_table)
true true
else else
@missing = "join table #{join_table} doesn't exist" @missing = "join table #{join_table} doesn't exist"
@ -219,7 +219,7 @@ module Shoulda # :nodoc:
end end
def class_has_foreign_key?(klass) def class_has_foreign_key?(klass)
if klass.column_names.include?(foreign_key.to_s) if klass.column_names.include?(foreign_key)
true true
else else
@missing = "#{klass} does not have a #{foreign_key} foreign key." @missing = "#{klass} does not have a #{foreign_key} foreign key."
@ -232,7 +232,7 @@ module Shoulda # :nodoc:
end end
def join_table def join_table
reflection.options[:join_table] reflection.options[:join_table].to_s
end end
def associated_class def associated_class
@ -240,18 +240,12 @@ module Shoulda # :nodoc:
end end
def foreign_key def foreign_key
fk_reflection = reflection if foreign_key_reflection
if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of) if foreign_key_reflection.respond_to?(:foreign_key)
fk_reflection = associated_class.reflect_on_association( foreign_key_reflection.foreign_key.to_s
reflection.options[:inverse_of] else
) foreign_key_reflection.primary_key_name.to_s
end end
if fk_reflection
fk_reflection.respond_to?(:foreign_key) ?
fk_reflection.foreign_key :
fk_reflection.primary_key_name
else
nil
end end
end end
@ -263,6 +257,14 @@ module Shoulda # :nodoc:
@reflection ||= model_class.reflect_on_association(@name) @reflection ||= model_class.reflect_on_association(@name)
end end
def foreign_key_reflection
if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of)
associated_class.reflect_on_association(reflection.options[:inverse_of])
else
reflection
end
end
def through_reflection def through_reflection
@through_reflection ||= model_class.reflect_on_association(@through) @through_reflection ||= model_class.reflect_on_association(@through)
end end

View File

@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.add_development_dependency('mocha', '~> 0.10.5') s.add_development_dependency('bourne', '~> 1.1.2')
s.add_development_dependency('rspec-rails', '~> 2.6.1') s.add_development_dependency('rspec-rails', '~> 2.6.1')
s.add_development_dependency('cucumber', '~> 1.1.9') s.add_development_dependency('cucumber', '~> 1.1.9')
s.add_development_dependency('aruba') s.add_development_dependency('aruba')

View File

@ -40,8 +40,8 @@ describe Shoulda::Matchers::ActiveRecord::QueryTheDatabaseMatcher do
it "passes arguments to the method to examine" do it "passes arguments to the method to examine" do
model = stub("Model", :count => nil) model = stub("Model", :count => nil)
model.expects(:count).with("arguments")
model.should_not query_the_database.when_calling(:count).with("arguments") model.should_not query_the_database.when_calling(:count).with("arguments")
model.should have_received(:count).with("arguments")
end end
else else
it "should raise an exception on Rails < 3.1" do it "should raise an exception on Rails < 3.1" do