Skip to content
This repository
Browse code

Cleanup.

  • Loading branch information...
commit 41bccc8cbff4c80636250171f48e0907be79d164 1 parent c10cf34
Gabe Berke-Williams gabebw authored
34 lib/shoulda/matchers/active_record/association_matcher.rb
@@ -166,7 +166,7 @@ def through_association_correct?
166 166 if @through == reflection.options[:through]
167 167 true
168 168 else
169   - @missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " <<
  169 + @missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " +
170 170 "but got it through #{reflection.options[:through]}"
171 171 false
172 172 end
@@ -210,7 +210,7 @@ def conditions_correct?
210 210
211 211 def join_table_exists?
212 212 if @macro != :has_and_belongs_to_many ||
213   - ::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
  213 + ::ActiveRecord::Base.connection.tables.include?(join_table)
214 214 true
215 215 else
216 216 @missing = "join table #{join_table} doesn't exist"
@@ -219,7 +219,7 @@ def join_table_exists?
219 219 end
220 220
221 221 def class_has_foreign_key?(klass)
222   - if klass.column_names.include?(foreign_key.to_s)
  222 + if klass.column_names.include?(foreign_key)
223 223 true
224 224 else
225 225 @missing = "#{klass} does not have a #{foreign_key} foreign key."
@@ -232,7 +232,7 @@ def model_class
232 232 end
233 233
234 234 def join_table
235   - reflection.options[:join_table]
  235 + reflection.options[:join_table].to_s
236 236 end
237 237
238 238 def associated_class
@@ -240,18 +240,12 @@ def associated_class
240 240 end
241 241
242 242 def foreign_key
243   - fk_reflection = reflection
244   - if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of)
245   - fk_reflection = associated_class.reflect_on_association(
246   - reflection.options[:inverse_of]
247   - )
248   - end
249   - if fk_reflection
250   - fk_reflection.respond_to?(:foreign_key) ?
251   - fk_reflection.foreign_key :
252   - fk_reflection.primary_key_name
253   - else
254   - nil
  243 + if foreign_key_reflection
  244 + if foreign_key_reflection.respond_to?(:foreign_key)
  245 + foreign_key_reflection.foreign_key.to_s
  246 + else
  247 + foreign_key_reflection.primary_key_name.to_s
  248 + end
255 249 end
256 250 end
257 251
@@ -263,6 +257,14 @@ def reflection
263 257 @reflection ||= model_class.reflect_on_association(@name)
264 258 end
265 259
  260 + def foreign_key_reflection
  261 + if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of)
  262 + associated_class.reflect_on_association(reflection.options[:inverse_of])
  263 + else
  264 + reflection
  265 + end
  266 + end
  267 +
266 268 def through_reflection
267 269 @through_reflection ||= model_class.reflect_on_association(@through)
268 270 end
2  shoulda-matchers.gemspec
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17 17 s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18 18 s.require_paths = ["lib"]
19 19
20   - s.add_development_dependency('mocha', '~> 0.10.5')
  20 + s.add_development_dependency('bourne', '~> 1.1.2')
21 21 s.add_development_dependency('rspec-rails', '~> 2.6.1')
22 22 s.add_development_dependency('cucumber', '~> 1.1.9')
23 23 s.add_development_dependency('aruba')
2  spec/shoulda/active_record/query_the_database_matcher_spec.rb
@@ -40,8 +40,8 @@
40 40
41 41 it "passes arguments to the method to examine" do
42 42 model = stub("Model", :count => nil)
43   - model.expects(:count).with("arguments")
44 43 model.should_not query_the_database.when_calling(:count).with("arguments")
  44 + model.should have_received(:count).with("arguments")
45 45 end
46 46 else
47 47 it "should raise an exception on Rails < 3.1" do

0 comments on commit 41bccc8

Karle Durante

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?

Please sign in to comment.
Something went wrong with that request. Please try again.