Sha256: 430269c5b4908a790113a879f43a2c9861a370a7b3e48c305f1ef4a801ca1534

Contents?: true

Size: 1.49 KB

Versions: 62

Compression:

Stored size: 1.49 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.class unless Class === @subject
          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 = @subject.new).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 = @subject.new).send(@attachment_name).assign(@file)
          subject.valid?
          subject.errors[:"#{@attachment_name}_file_name"].blank?
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 22 rubygems

Version Path
paperclip-2.3.16 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.3.15 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
olek-paperclip-2.3.3.2 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.3.12 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-cloudfiles-2.3.10.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-cloudfiles-2.3.8.6 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
sayso-paperclip-2.3.10.001 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-cloudfiles-2.3.8.5 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
ryansch-paperclip-2.3.10 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
olek-paperclip-2.3.3.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.3.11 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.3.10 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-2.3.9 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-cloudfiles-2.3.8.4 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-youtube-2.3.8.2 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-youtube-2.3.8.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
radiant-paperclipped-extension-0.8.1 vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-cloudfiles-2.3.8.3 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
whitby3001-paperclip-cloudfiles-2.3.8.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
organicinternet-paperclip-2.3.8.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb