Sha256: 00ad7978b6c51836a545daa0b124a61d23ac975bb011b61e4e9cbcd7ffffd32a

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

module Paperclip
  module Shoulda
    module Matchers
      # Ensures that the given instance or class validates the presence of the
      # given attachment.
      #
      # describe User do
      #   it { should validate_attachment_presence(:avatar) }
      # end
      def validate_attachment_presence name
        ValidateAttachmentPresenceMatcher.new(name)
      end

      class ValidateAttachmentPresenceMatcher
        def initialize attachment_name
          @attachment_name = attachment_name
        end

        def matches? subject
          @subject = subject
          @subject = subject.new if subject.class == Class
          error_when_not_valid? && no_error_when_valid?
        end

        def failure_message
          "Attachment #{@attachment_name} should be required"
        end

        def negative_failure_message
          "Attachment #{@attachment_name} should not be required"
        end

        def description
          "require presence of attachment #{@attachment_name}"
        end

        protected

        def error_when_not_valid?
          @subject.send(@attachment_name).assign(nil)
          @subject.valid?
          not @subject.errors[:"#{@attachment_name}_file_name"].blank?
        end

        def no_error_when_valid?
          @file = StringIO.new(".")
          @subject.send(@attachment_name).assign(@file)
          @subject.valid?
          @subject.errors[:"#{@attachment_name}_file_name"].blank?
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
paperclip-v2_7-patched-ruby-1_8_6-2.7.5 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.7.5 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.7.4 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.7.2 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.8.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.7.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.7.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.6.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb