/usr/lib/ruby/vendor_ruby/shoulda/matchers/active_record/validate_presence_of_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 | module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
# Ensures that the model is not valid if the given attribute is not
# present.
#
# Options:
# * <tt>with_message</tt> - value the test expects to find in
# <tt>errors.on(:attribute)</tt>. <tt>Regexp</tt> or <tt>String</tt>.
# Defaults to the translation for <tt>:blank</tt>.
#
# Examples:
# it { should validate_presence_of(:name) }
# it { should validate_presence_of(:name).
# with_message(/is not optional/) }
#
def validate_presence_of(attr)
ValidatePresenceOfMatcher.new(attr)
end
class ValidatePresenceOfMatcher < ValidationMatcher # :nodoc:
def with_message(message)
@expected_message = message if message
self
end
def matches?(subject)
super(subject)
@expected_message ||= :blank
disallows_value_of(blank_value, @expected_message)
end
def description
"require #{@attribute} to be set"
end
private
def blank_value
if collection?
[]
else
nil
end
end
def collection?
if @subject.class.respond_to?(:reflect_on_association) &&
reflection = @subject.class.reflect_on_association(@attribute)
[:has_many, :has_and_belongs_to_many].include?(reflection.macro)
else
false
end
end
end
end
end
end
|