class Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher

@private

Constants

OPTIONS

Public Class Methods

new(column) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 113
def initialize(column)
  @column = column
  @options = {}
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 160
def description
  desc = "have db column named #{@column}"
  desc << " of type #{@options[:column_type]}" if @options.key?(:column_type)
  desc << " of sql_type #{@options[:sql_column_type]}" if @options.key?(:sql_column_type)
  desc << " of precision #{@options[:precision]}" if @options.key?(:precision)
  desc << " of limit #{@options[:limit]}" if @options.key?(:limit)
  desc << " of default #{@options[:default]}" if @options.key?(:default)
  desc << " of null #{@options[:null]}" if @options.key?(:null)
  desc << " of primary #{@options[:primary]}" if @options.key?(:primary)
  desc << " of scale #{@options[:scale]}" if @options.key?(:scale)
  desc
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 152
def failure_message
  "Expected #{expectation} (#{@missing})"
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 156
def failure_message_when_negated
  "Did not expect #{expectation}"
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 138
def matches?(subject)
  @subject = subject
  column_exists? &&
    correct_column_type? &&
    correct_sql_column_type? &&
    correct_precision? &&
    correct_limit? &&
    correct_default? &&
    correct_null? &&
    correct_scale? &&
    correct_primary? &&
    correct_array?
end
of_sql_type(sql_column_type) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 123
def of_sql_type(sql_column_type)
  @options[:sql_column_type] = sql_column_type
  self
end
of_type(column_type) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 118
def of_type(column_type)
  @options[:column_type] = column_type
  self
end
with_options(opts = {}) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 128
def with_options(opts = {})
  validate_options(opts)
  OPTIONS.each do |attribute|
    if opts.key?(attribute.to_sym)
      @options[attribute.to_sym] = opts[attribute.to_sym]
    end
  end
  self
end

Protected Instance Methods

actual_primary?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 336
def actual_primary?
  model_class.primary_key == matched_column.name
end
actual_scale() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 332
def actual_scale
  matched_column.scale
end
column_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 185
def column_exists?
  if model_class.column_names.include?(@column.to_s)
    true
  else
    @missing =
      "#{model_class} does not have a db column named #{@column}."
    false
  end
end
correct_array?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 302
def correct_array?
  return true unless @options.key?(:array)

  if matched_column.array? == @options[:array]
    true
  else
    @missing = "#{model_class} has a db column named #{@column} "
    @missing <<
      if @options[:primary]
        'that is not array, but should be'
      else
        'that is array, but should not be'
      end
    false
  end
end
correct_column_type?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 195
def correct_column_type?
  return true unless @options.key?(:column_type)

  if matched_column.type.to_s == @options[:column_type].to_s
    true
  else
    @missing =
      "#{model_class} has a db column named #{@column} " <<
      "of type #{matched_column.type}, not #{@options[:column_type]}."
    false
  end
end
correct_default?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 247
def correct_default?
  return true unless @options.key?(:default)

  if matched_column.type_cast_default.to_s == @options[:default].to_s
    true
  else
    @missing = "#{model_class} has a db column named #{@column} " <<
               "of default #{matched_column.type_cast_default}, " <<
               "not #{@options[:default]}."
    false
  end
end
correct_limit?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 234
def correct_limit?
  return true unless @options.key?(:limit)

  if matched_column.limit.to_s == @options[:limit].to_s
    true
  else
    @missing = "#{model_class} has a db column named #{@column} " <<
               "of limit #{matched_column.limit}, " <<
               "not #{@options[:limit]}."
    false
  end
end
correct_null?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 260
def correct_null?
  return true unless @options.key?(:null)

  if matched_column.null.to_s == @options[:null].to_s
    true
  else
    @missing = "#{model_class} has a db column named #{@column} " <<
               "of null #{matched_column.null}, " <<
               "not #{@options[:null]}."
    false
  end
end
correct_precision?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 221
def correct_precision?
  return true unless @options.key?(:precision)

  if matched_column.precision.to_s == @options[:precision].to_s
    true
  else
    @missing = "#{model_class} has a db column named #{@column} " <<
               "of precision #{matched_column.precision}, " <<
               "not #{@options[:precision]}."
    false
  end
end
correct_primary?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 285
def correct_primary?
  return true unless @options.key?(:primary)

  if matched_column.primary? == @options[:primary]
    true
  else
    @missing = "#{model_class} has a db column named #{@column} "
    @missing <<
      if @options[:primary]
        'that is not primary, but should be'
      else
        'that is primary, but should not be'
      end
    false
  end
end
correct_scale?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 273
def correct_scale?
  return true unless @options.key?(:scale)

  if actual_scale.to_s == @options[:scale].to_s
    true
  else
    @missing = "#{model_class} has a db column named #{@column} "
    @missing << "of scale #{actual_scale}, not #{@options[:scale]}."
    false
  end
end
correct_sql_column_type?() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 208
def correct_sql_column_type?
  return true unless @options.key?(:sql_column_type)

  if matched_column.sql_type.to_s == @options[:sql_column_type].to_s
    true
  else
    @missing =
      "#{model_class} has a db column named #{@column} " <<
      "of sql type #{matched_column.sql_type}, not #{@options[:sql_column_type]}."
    false
  end
end
expectation() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 340
def expectation
  "#{model_class.name} to #{description}"
end
matched_column() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 319
def matched_column
  @_matched_column ||= begin
    column = model_class.columns.detect do |each|
      each.name == @column.to_s
    end
    DecoratedColumn.new(model_class, column)
  end
end
model_class() click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 328
def model_class
  @subject.class
end
validate_options(opts) click to toggle source
# File lib/shoulda/matchers/active_record/have_db_column_matcher.rb, line 175
def validate_options(opts)
  invalid_options = opts.keys.map(&:to_sym) - OPTIONS
  if invalid_options.any?
    raise(
      ArgumentError,
      "Unknown option(s): #{invalid_options.map(&:inspect).join(', ')}",
    )
  end
end