Class: SchemaExpectations::RSpecMatchers::ValidateSchemaNullableMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb

Instance Method Summary collapse

Instance Method Details

#descriptionObject



76
77
78
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 76

def description
  'validate NOT NULL columns are present'
end

#except(*args) ⇒ ValidateSchemaNullableMatcher

Specifies a list of columns for matcher to ignore

Returns:



93
94
95
96
97
98
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 93

def except(*args)
  fail 'cannot use only and except' if @only
  @except = Array(args)
  fail 'empty except list' if @except.empty?
  self
end

#failure_messageObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 52

def failure_message
  errors = []

  (@present_column_names - @not_null_column_names).each do |column_name|
    errors << "#{@model.name} #{column_name} has unconditional presence validation but is missing NOT NULL"
  end

  (@not_null_column_names - @present_column_names).each do |column_name|
    conditions = validator_allow_nil_conditions_for_column_name(column_name) ||
      validator_conditions_for_column_name(column_name)
    if conditions
      errors << "#{@model.name} #{column_name} is NOT NULL but its presence validator was conditional: #{conditions.inspect}"
    else
      errors << "#{@model.name} #{column_name} is NOT NULL but has no presence validation"
    end
  end

  errors.join(', ')
end

#failure_message_when_negatedObject



72
73
74
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 72

def failure_message_when_negated
  "#{@model.name} should not match NOT NULL with its presence validation but does"
end

#matches?(model) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 43

def matches?(model)
  @model = cast_model model
  @validation_reflector = ActiveRecord::ValidationReflector.new(@model)
  @column_reflector = ActiveRecord::ColumnReflector.new(@model)
  @not_null_column_names = filter_column_names(not_null_column_names).sort
  @present_column_names = filter_column_names(present_column_names).sort
  @not_null_column_names == @present_column_names
end

#only(*args) ⇒ ValidateSchemaNullableMatcher

Specifies a list of columns to restrict matcher

Returns:



83
84
85
86
87
88
# File 'lib/schema_expectations/rspec_matchers/validate_schema_nullable.rb', line 83

def only(*args)
  fail 'cannot use only and except' if @except
  @only = Array(args)
  fail 'empty only list' if @only.empty?
  self
end