Sha256: d7155300f9e8db0287316fc9c1cb54825ce8631ecd5bed22f557a80997e4a984

Contents?: true

Size: 1.98 KB

Versions: 13

Compression:

Stored size: 1.98 KB

Contents

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

        optional :table_name
        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(current_table_name)
          end

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

        private

          def current_table_name
            @options[: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
      # * <tt>table_name</tt> - when supplied, tests if the index is defined for the given table
      #
      # == Examples
      #
      #   it { should have_index(:ssn).unique(true) }
      #   it { should have_index([:name, :email]).unique(true) }
      #
      #   should_have_index :ssn, :unique => true, :limit => 9, :null => false
      #
      #   should_have_index :ssn do |m|
      #     m.unique
      #     m.limit = 9
      #     m.null = false
      #   end
      #
      def have_index(*args, &block)
        HaveIndexMatcher.new(*args, &block).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

13 entries across 13 versions & 5 rubygems

Version Path
specstar-remarkable-0.0.0 lib/remarkable/active_record/matchers/have_index_matcher.rb
maksar-remarkable_activerecord-4.0.0.alpha6 lib/remarkable/active_record/matchers/have_index_matcher.rb
schubert-remarkable_activerecord-4.0.0.alpha4 lib/remarkable/active_record/matchers/have_index_matcher.rb
benhutton-remarkable_activerecord-4.0.0.alpha6 lib/remarkable/active_record/matchers/have_index_matcher.rb
remarkable_activerecord-4.0.0.alpha4 lib/remarkable/active_record/matchers/have_index_matcher.rb
remarkable_activerecord-4.0.0.alpha3 lib/remarkable/active_record/matchers/have_index_matcher.rb
remarkable_activerecord-4.0.0.alpha2 lib/remarkable/active_record/matchers/have_index_matcher.rb
remarkable_activerecord-4.0.0.alpha1 lib/remarkable_activerecord/matchers/have_index_matcher.rb
remarkable_activerecord-3.1.13 lib/remarkable_activerecord/matchers/have_index_matcher.rb
remarkable_activerecord-3.1.12 lib/remarkable_activerecord/matchers/have_index_matcher.rb
remarkable_activerecord-3.1.11 lib/remarkable_activerecord/matchers/have_index_matcher.rb
remarkable_activerecord-3.1.10 lib/remarkable_activerecord/matchers/have_index_matcher.rb
remarkable_activerecord-3.1.9 lib/remarkable_activerecord/matchers/have_index_matcher.rb