Sha256: ca32df017f4bccab32e73547703c2dc4175e335c3d1ff56c591d0b56e6b902f1

Contents?: true

Size: 1.43 KB

Versions: 14

Compression:

Stored size: 1.43 KB

Contents

module Paperclip
  module Shoulda
    module Matchers
      # Ensures that the given instance or class has an attachment with the
      # given name.
      #
      # Example:
      #   describe User do
      #     it { should have_attached_file(:avatar) }
      #   end
      def have_attached_file(name)
        HaveAttachedFileMatcher.new(name)
      end

      class HaveAttachedFileMatcher
        def initialize(attachment_name)
          @attachment_name = attachment_name
        end

        def matches?(subject)
          @subject = subject
          @subject = @subject.class unless Class === @subject
          responds? && has_column?
        end

        def failure_message
          "Should have an attachment named #{@attachment_name}"
        end

        def failure_message_when_negated
          "Should not have an attachment named #{@attachment_name}"
        end
        alias negative_failure_message failure_message_when_negated

        def description
          "have an attachment named #{@attachment_name}"
        end

        protected

        def responds?
          methods = @subject.instance_methods.map(&:to_s)
          methods.include?(@attachment_name.to_s) &&
            methods.include?("#{@attachment_name}=") &&
            methods.include?("#{@attachment_name}?")
        end

        def has_column?
          @subject.column_names.include?("#{@attachment_name}_file_name")
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kt-paperclip-7.2.2 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-7.2.1 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-7.2.0 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.4.2 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-7.1.1 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-7.1.0 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-7.0.1 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-7.0.0 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.4.1 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.4.0 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.3.0 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.2.2 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.2.1 lib/paperclip/matchers/have_attached_file_matcher.rb
kt-paperclip-6.2.0 lib/paperclip/matchers/have_attached_file_matcher.rb