Sha256: f0d88a38d19c982d3f102691229ab648b3c260ecc247011f324aaf964d073bb5

Contents?: true

Size: 1.48 KB

Versions: 153

Compression:

Stored size: 1.48 KB

Contents

module Shoulda # :nodoc:
  module ActiveRecord # :nodoc:
    module Matchers

      # 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 = @subject.class.reflect_on_association(@attribute)
            [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
          else
            false
          end
        end
      end

    end
  end
end

Version data entries

153 entries across 123 versions & 25 rubygems

Version Path
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.5.1 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.5.0 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.4.1 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.4.0 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.3.5 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
faster_shoulda-2.11.4 lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.3.4 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.3.3 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.3.2 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.3.1 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb
challah-0.3.0 vendor/bundle/gems/shoulda-2.11.3/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb