Sha256: 21cdf0add4a78f8b5577a10747dc14998aefb9987f84c3da00bf26acbfa2d4d4
Contents?: true
Size: 1.59 KB
Versions: 45
Compression:
Stored size: 1.59 KB
Contents
module Shoulda # :nodoc: module Matchers module ActiveModel # :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 reflection [:has_many, :has_and_belongs_to_many].include?(reflection.macro) else false end end def reflection @subject.class.respond_to?(:reflect_on_association) && @subject.class.reflect_on_association(@attribute) end end end end end
Version data entries
45 entries across 35 versions & 4 rubygems