Sha256: 007d6d36b3427c5b6230abb8e87ed8a9cfed46eaa212bf96c3635359dc76004d

Contents?: true

Size: 1.76 KB

Versions: 9

Compression:

Stored size: 1.76 KB

Contents

module RedhillonrailsCoreMatchers

  class Reference
    def initialize(expected)
      @column_names = nil
      unless expected.empty?
        @references_column_names = Array(expected).collect(&:to_s)
        @references_table_name = @references_column_names.shift 
      end
    end

    def matches?(model)
      @model = model
      if @references_table_name
        @result = @model.foreign_keys.select do |fk|
          fk.references_table_name == @references_table_name && 
            @references_column_names.empty? ? true : fk.references_column_names == @references_column_names
        end
      else
        @result = @model.foreign_keys
      end
      if @column_names 
        @result.any? do |fk| 
          fk.column_names == @column_names &&
            (@on_update ? fk.on_update == @on_update : true) &&
            (@on_delete ? fk.on_delete == @on_delete : true)
        end
      else
        !@result.empty?
      end
    end

    def failure_message_for_should(should_not = false)
      target_column_names = @column_names.present? ? "(#{@column_names.join(', ')})" : "" 
      destinantion_column_names = @references_table_name ? "#{@references_table_name}(#{@references_column_names.join(', ')})" : "anything"
      invert = should_not ? 'not' : ''
      "Expected #{@model.table_name}#{target_column_names} to #{invert} reference #{destinantion_column_names}"
    end

    def failure_message_for_should_not
      failure_message_for_should(true)
    end
  
    def on(*column_names)
      @column_names = column_names.collect(&:to_s)
      self
    end

    def on_update(action)
      @on_update = action
      self
    end

    def on_delete(action)
      @on_delete = action
      self
    end

  end

  def reference(*expect)
    Reference.new(expect)
  end

end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
aspgems-redhillonrails_core-2.0.0.beta4 spec/support/reference.rb
redhillonrails_core-2.0.0.pre spec/support/reference.rb
redhillonrails_core-1.2.0 spec/support/reference.rb
aspgems-redhillonrails_core-2.0.0.beta2 spec/support/reference.rb
aspgems-redhillonrails_core-2.0.0.beta1 spec/support/reference.rb
redhillonrails_core-1.1.3 spec/support/reference.rb
redhillonrails_core-1.1.3.pre1 spec/support/reference.rb
redhillonrails_core-1.1.2 spec/support/reference.rb
redhillonrails_core-1.1.1 spec/support/reference.rb