Sha256: a32a0a82eb6fbe64a756a443007bc834e0283f1afbf1c739fdab092eb6a344c5

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module Remarkable
  module ActiveRecord
    module Matchers
      class HaveIndexMatcher < Remarkable::ActiveRecord::Base #:nodoc:
        arguments :collection => :columns, :as => :column

        optional :unique, :default => true

        collection_assertions :index_exists?, :is_unique?

        protected

          def index_exists?
            !matched_index.nil?
          end

          def is_unique?
            return true unless @options.key?(:unique)
            return @options[:unique] == matched_index.unique, :actual => matched_index.unique
          end

          def matched_index
            columns = [@column].flatten.map(&:to_s)
            indexes.detect { |ind| ind.columns == columns }
          end

          def indexes
            @indexes ||= ::ActiveRecord::Base.connection.indexes(subject_class.table_name)
          end

          def interpolation_options
            @subject ? { :table_name => subject_class.table_name } : {}
          end

      end

      # Ensures the database column has specified index.
      #
      # == Options
      #
      # * <tt>unique</tt> - when supplied, tests if the index is unique or not
      #
      # == Examples
      #
      #   it { should have_index(:ssn).unique(true) }
      #   it { should have_index([:name, :email]).unique(true) }
      #
      def have_index(*args)
        HaveIndexMatcher.new(*args).spec(self)
      end
      alias :have_indices    :have_index
      alias :have_db_index   :have_index
      alias :have_db_indices :have_index

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remarkable_activerecord-3.0.1 lib/remarkable_activerecord/matchers/have_index_matcher.rb