/usr/lib/ruby/vendor_ruby/shoulda/matchers/active_record/have_db_column_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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
# Ensures the database column exists.
#
# Options:
# * <tt>of_type</tt> - db column type (:integer, :string, etc.)
# * <tt>with_options</tt> - same options available in migrations
# (:default, :null, :limit, :precision, :scale)
#
# Examples:
# it { should_not have_db_column(:admin).of_type(:boolean) }
# it { should have_db_column(:salary).
# of_type(:decimal).
# with_options(:precision => 10, :scale => 2) }
#
def have_db_column(column)
HaveDbColumnMatcher.new(:have_db_column, column)
end
class HaveDbColumnMatcher # :nodoc:
def initialize(macro, column)
@macro = macro
@column = column
end
def of_type(column_type)
@column_type = column_type
self
end
def with_options(opts = {})
@precision = opts[:precision]
@limit = opts[:limit]
@default = opts[:default]
@null = opts[:null]
@scale = opts[:scale]
self
end
def matches?(subject)
@subject = subject
column_exists? &&
correct_column_type? &&
correct_precision? &&
correct_limit? &&
correct_default? &&
correct_null? &&
correct_scale?
end
def failure_message
"Expected #{expectation} (#{@missing})"
end
def negative_failure_message
"Did not expect #{expectation}"
end
def description
desc = "have db column named #{@column}"
desc << " of type #{@column_type}" unless @column_type.nil?
desc << " of precision #{@precision}" unless @precision.nil?
desc << " of limit #{@limit}" unless @limit.nil?
desc << " of default #{@default}" unless @default.nil?
desc << " of null #{@null}" unless @null.nil?
desc << " of primary #{@primary}" unless @primary.nil?
desc << " of scale #{@scale}" unless @scale.nil?
desc
end
protected
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
def correct_column_type?
return true if @column_type.nil?
if matched_column.type.to_s == @column_type.to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of type #{matched_column.type}, not #{@column_type}."
false
end
end
def correct_precision?
return true if @precision.nil?
if matched_column.precision.to_s == @precision.to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of precision #{matched_column.precision}, " <<
"not #{@precision}."
false
end
end
def correct_limit?
return true if @limit.nil?
if matched_column.limit.to_s == @limit.to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of limit #{matched_column.limit}, " <<
"not #{@limit}."
false
end
end
def correct_default?
return true if @default.nil?
if matched_column.default.to_s == @default.to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of default #{matched_column.default}, " <<
"not #{@default}."
false
end
end
def correct_null?
return true if @null.nil?
if matched_column.null.to_s == @null.to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of null #{matched_column.null}, " <<
"not #{@null}."
false
end
end
def correct_scale?
return true if @scale.nil?
if matched_column.scale.to_s == @scale.to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of scale #{matched_column.scale}, not #{@scale}."
false
end
end
def matched_column
model_class.columns.detect { |each| each.name == @column.to_s }
end
def model_class
@subject.class
end
def expectation
expected = "#{model_class.name} to #{description}"
end
end
end
end
end
|