Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanup.
  • Loading branch information
Gabe Berke-Williams committed Mar 23, 2012
1 parent c10cf34 commit 41bccc8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
34 changes: 18 additions & 16 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -166,7 +166,7 @@ def through_association_correct?
if @through == reflection.options[:through]
true
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]}"
false
end
Expand Down Expand Up @@ -210,7 +210,7 @@ def conditions_correct?

def join_table_exists?
if @macro != :has_and_belongs_to_many ||
::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
::ActiveRecord::Base.connection.tables.include?(join_table)

This comment has been minimized.

Copy link
@karledurante

karledurante Sep 18, 2012

Contributor

This assumes that your join table exists in the "primary" rails database. It is possible to point models at an alternate database. It's also possible to configure a HABTM relationship where all three tables exists in a completely different database (in which case this matcher fails). Using this line of code, we can fix the problem:

model_class.connection.tables.include?(join_table.to_s)

This uses the model's connection to determine where the join table is located. I have no problem making this change, but honestly, I'm not sure how to write a test for it? The change is somewhat benign and all the existing tests should continue to pass. Any guidance for me?

This comment has been minimized.

Copy link
@karledurante

karledurante Sep 18, 2012

Contributor

I've moved this into pull request 154: #154

true
else
@missing = "join table #{join_table} doesn't exist"
Expand All @@ -219,7 +219,7 @@ def join_table_exists?
end

def class_has_foreign_key?(klass)
if klass.column_names.include?(foreign_key.to_s)
if klass.column_names.include?(foreign_key)
true
else
@missing = "#{klass} does not have a #{foreign_key} foreign key."
Expand All @@ -232,26 +232,20 @@ def model_class
end

def join_table
reflection.options[:join_table]
reflection.options[:join_table].to_s
end

def associated_class
reflection.klass
end

def foreign_key
fk_reflection = reflection
if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of)
fk_reflection = associated_class.reflect_on_association(
reflection.options[:inverse_of]
)
end
if fk_reflection
fk_reflection.respond_to?(:foreign_key) ?
fk_reflection.foreign_key :
fk_reflection.primary_key_name
else
nil
if foreign_key_reflection
if foreign_key_reflection.respond_to?(:foreign_key)
foreign_key_reflection.foreign_key.to_s
else
foreign_key_reflection.primary_key_name.to_s
end
end
end

Expand All @@ -263,6 +257,14 @@ def reflection
@reflection ||= model_class.reflect_on_association(@name)
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
@through_reflection ||= model_class.reflect_on_association(@through)
end
Expand Down
2 changes: 1 addition & 1 deletion shoulda-matchers.gemspec
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
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('cucumber', '~> 1.1.9')
s.add_development_dependency('aruba')
Expand Down
Expand Up @@ -40,8 +40,8 @@

it "passes arguments to the method to examine" do
model = stub("Model", :count => nil)
model.expects(:count).with("arguments")
model.should_not query_the_database.when_calling(:count).with("arguments")
model.should have_received(:count).with("arguments")
end
else
it "should raise an exception on Rails < 3.1" do
Expand Down

0 comments on commit 41bccc8

Please sign in to comment.