This file is indexed.

/usr/lib/ruby/vendor_ruby/shoulda/matchers/active_record/have_db_index_matcher.rb is in ruby-shoulda-matchers 1.0.0~beta2-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
module Shoulda # :nodoc:
  module Matchers
    module ActiveRecord # :nodoc:

      # Ensures that there are DB indices on the given columns or tuples of
      # columns.
      #
      # Options:
      # * <tt>unique</tt> - whether or not the index has a unique
      #   constraint. Use <tt>true</tt> to explicitly test for a unique
      #   constraint.  Use <tt>false</tt> to explicitly test for a non-unique
      #   constraint. Use <tt>nil</tt> if you don't care whether the index is
      #   unique or not.  Default = <tt>nil</tt>
      #
      # Examples:
      #
      #   it { should have_db_index(:age) }
      #   it { should have_db_index([:commentable_type, :commentable_id]) }
      #   it { should have_db_index(:ssn).unique(true) }
      #
      def have_db_index(columns)
        HaveDbIndexMatcher.new(:have_index, columns)
      end

      class HaveDbIndexMatcher # :nodoc:
        def initialize(macro, columns)
          @macro = macro
          @columns = normalize_columns_to_array(columns)
        end

        def unique(unique)
          @unique = unique
          self
        end

        def matches?(subject)
          @subject = subject
          index_exists? && correct_unique?
        end

        def failure_message
          "Expected #{expectation} (#{@missing})"
        end

        def negative_failure_message
          "Did not expect #{expectation}"
        end

        def description
          "have a #{index_type} index on columns #{@columns.join(' and ')}"
        end

        protected

        def index_exists?
          ! matched_index.nil?
        end

        def correct_unique?
          return true if @unique.nil?
          if matched_index.unique == @unique
            true
          else
            @missing = "#{table_name} has an index named #{matched_index.name} " <<
                       "of unique #{matched_index.unique}, not #{@unique}."
            false
          end
        end

        def matched_index
          indexes.detect { |each| each.columns == @columns }
        end

        def model_class
          @subject.class
        end

        def table_name
          model_class.table_name
        end

        def indexes
          ::ActiveRecord::Base.connection.indexes(table_name)
        end

        def expectation
          expected = "#{model_class.name} to #{description}"
        end

        def index_type
          case @unique
          when nil
            ''
          when false
            'non-unique'
          else
            'unique'
          end
        end

        def normalize_columns_to_array(columns)
          if columns.class == Array
            columns.collect { |each| each.to_s }
          else
            [columns.to_s]
          end
        end
      end

    end
  end
end