Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

converting should_have_index to use have_index matcher

  • Loading branch information...
commit 4512a9a8f6b60036219e6698ae58e4a74955e4a3 1 parent f1ec3e2
Dan Croak authored January 25, 2009 jferris committed January 27, 2009
17  lib/shoulda/active_record/macros.rb
@@ -459,19 +459,12 @@ def should_have_db_columns(*columns)
459 459
       #
460 460
       def should_have_indices(*columns)
461 461
         unique = get_options!(columns, :unique)
462  
-        table = model_class.table_name
463  
-        indices = ::ActiveRecord::Base.connection.indexes(table)
464  
-        index_types = { true => "unique", false => "non-unique" }
465  
-        index_type = index_types[unique] || "an"
466  
-
  462
+        klass  = model_class
  463
+        
467 464
         columns.each do |column|
468  
-          should "have #{index_type} index on #{table} for #{column.inspect}" do
469  
-            columns = [column].flatten.map(&:to_s)
470  
-            index = indices.detect {|ind| ind.columns == columns }
471  
-            assert index, "#{table} does not have an index for #{column.inspect}"
472  
-            if [true, false].include?(unique)
473  
-              assert_equal unique, index.unique, "Expected #{index_type} index but was #{index_types[index.unique]}."
474  
-            end
  465
+          matcher = have_index(column).unique(unique)
  466
+          should matcher.description do
  467
+            assert_accepts(matcher, klass.new)
475 468
           end
476 469
         end
477 470
       end
32  lib/shoulda/active_record/matchers/have_index_matcher.rb
@@ -10,14 +10,14 @@ module Matchers
10 10
       # Example:
11 11
       #   it { should have_index(:ssn).unique(true) }
12 12
       #
13  
-      def have_index(index)
14  
-        HaveIndexMatcher.new(:have_index, index)
  13
+      def have_index(columns)
  14
+        HaveIndexMatcher.new(:have_index, columns)
15 15
       end
16 16
 
17 17
       class HaveIndexMatcher # :nodoc:
18  
-        def initialize(macro, index)
  18
+        def initialize(macro, columns)
19 19
           @macro = macro
20  
-          @index = normalize_index_to_array(index)
  20
+          @columns = normalize_columns_to_array(columns)
21 21
         end
22 22
         
23 23
         def unique(unique)
@@ -39,7 +39,7 @@ def negative_failure_message
39 39
         end
40 40
 
41 41
         def description
42  
-          "have index named #{@column}"
  42
+          "have a #{index_type} index on columns #{@columns}"
43 43
         end
44 44
 
45 45
         protected
@@ -53,33 +53,41 @@ def correct_unique?
53 53
           if matched_index.unique == @unique
54 54
             true
55 55
           else
56  
-            @missing = "#{model_class} has an index named #{matched_index.name} " <<
  56
+            @missing = "#{table_name} has an index named #{matched_index.name} " <<
57 57
                        "of unique #{matched_index.unique}, not #{@unique}."
58 58
             false
59 59
           end
60 60
         end
61 61
         
62 62
         def matched_index
63  
-          indexes.detect { |each| each.columns == @index }
  63
+          indexes.detect { |each| each.columns == @columns }
64 64
         end
65 65
 
66 66
         def model_class
67 67
           @subject.class
68 68
         end
69 69
         
  70
+        def table_name
  71
+          model_class.table_name
  72
+        end
  73
+        
70 74
         def indexes
71  
-          ::ActiveRecord::Base.connection.indexes(model_class.table_name)
  75
+          ::ActiveRecord::Base.connection.indexes(table_name)
72 76
         end
73 77
 
74 78
         def expectation
75 79
           expected = "#{model_class.name} to #{description}"
76 80
         end
77 81
         
78  
-        def normalize_index_to_array(index)
79  
-          if index.class == Array
80  
-            index.collect { |each| each.to_s }
  82
+        def index_type
  83
+          @unique ? "unique" : "non-unique"
  84
+        end
  85
+        
  86
+        def normalize_columns_to_array(columns)
  87
+          if columns.class == Array
  88
+            columns.collect { |each| each.to_s }
81 89
           else
82  
-            [index.to_s]
  90
+            [columns.to_s]
83 91
           end
84 92
         end
85 93
       end
4  test/test_helper.rb
... ...
@@ -1,9 +1,5 @@
1 1
 require 'fileutils'
2 2
 
3  
-require 'rubygems'
4  
-require 'quietbacktrace'
5  
-require 'redgreen'
6  
-
7 3
 # Load the environment
8 4
 ENV['RAILS_ENV'] = 'test'
9 5
 

0 notes on commit 4512a9a

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