Sha256: e03ab23a20621733419b2b9a26d76859ff960614f46a9a85efd4623dd09ce5ca

Contents?: true

Size: 1.67 KB

Versions: 32

Compression:

Stored size: 1.67 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 failure_message_when_negated
          "Attachment #{@attachment_name} should not be required"
        end
        alias negative_failure_message failure_message_when_negated

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

        protected

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

        def no_error_when_valid?
          @file = StringIO.new(".")
          @subject.send(@attachment_name).assign(@file)
          @subject.valid?
          expected_message = [
            @attachment_name.to_s.titleize,
            I18n.t(:blank, scope: [:errors, :messages])
          ].join(' ')
          @subject.errors.full_messages.exclude?(expected_message)
        end
      end
    end
  end
end

Version data entries

32 entries across 30 versions & 6 rubygems

Version Path
kt-paperclip-5.4.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
kt-paperclip-4.4.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-6.1.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-6.0.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.3.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.2.1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.2.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paperclip-5.1.0/lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.1.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.0.0 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-4.3.7 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip_jk-5.0.0.beta2 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.0.0.beta2 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-5.0.0.beta1 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-4.3.6 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-4.3.5 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-4.3.4 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-4.3.3 lib/paperclip/matchers/validate_attachment_presence_matcher.rb
paperclip-4.3.2 lib/paperclip/matchers/validate_attachment_presence_matcher.rb