From ca8a1e5a8b0e5d1317206adc5b459c5ea42c0a4c Mon Sep 17 00:00:00 2001 From: Jacob Morris Date: Thu, 23 Aug 2012 23:48:17 -0600 Subject: [PATCH] allow testing of :foreign_key option for has_one relationships --- .../active_record/association_matcher.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index 4a368dbb..726d2fbd 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -100,6 +100,11 @@ module Shoulda # :nodoc: self end + def with_foreign_key(foreign_key) + @options[:foreign_key] = foreign_key + self + end + def validate(validate = true) @validate = validate self @@ -261,11 +266,15 @@ module Shoulda # :nodoc: end def class_has_foreign_key?(klass) - if klass.column_names.include?(foreign_key) - true + if @options.key?(:foreign_key) + reflection.options[:foreign_key] == @options[:foreign_key] else - @missing = "#{klass} does not have a #{foreign_key} foreign key." - false + if klass.column_names.include?(foreign_key) + true + else + @missing = "#{klass} does not have a #{foreign_key} foreign key." + false + end end end