Sha256: 361d65502d4e3e1ddb80a50e42a10963369ae9bea943e5b21b2c0c67b3d6a369

Contents?: true

Size: 1.47 KB

Versions: 89

Compression:

Stored size: 1.47 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? && included?
        end

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

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

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

        protected

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

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

        def included?
          @subject.ancestors.include?(Paperclip::InstanceMethods)
        end
      end
    end
  end
end

Version data entries

89 entries across 89 versions & 24 rubygems

Version Path
paperclip-cloudfiles-2.3.8.3 lib/paperclip/matchers/have_attached_file_matcher.rb
whitby3001-paperclip-cloudfiles-2.3.8.1 lib/paperclip/matchers/have_attached_file_matcher.rb
organicinternet-paperclip-2.3.8.1 lib/paperclip/matchers/have_attached_file_matcher.rb
arielvalentin-paperclip-2.3.6 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-cloudfiles-2.3.8.1 lib/paperclip/matchers/have_attached_file_matcher.rb
twm_paperclip-2.3.8t lib/paperclip/matchers/have_attached_file_matcher.rb
reenhanced-paperclip-2.3.8 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-cloudfiles-2.3.8 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-2.3.8 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-2.3.7 lib/paperclip/matchers/have_attached_file_matcher.rb
twm_paperclip-2.3.6b lib/paperclip/matchers/have_attached_file_matcher.rb
twm_paperclip-2.3.6 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-2.3.6 lib/paperclip/matchers/have_attached_file_matcher.rb
path-paperclip-2.3.3 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-2.3.5 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-2.3.4 lib/paperclip/matchers/have_attached_file_matcher.rb
bookis-paperclip-2.3.3.1 lib/paperclip/matchers/have_attached_file_matcher.rb
bookis-paperclip-2.3.3 lib/paperclip/matchers/have_attached_file_matcher.rb
olek-paperclip-2.3.3 lib/paperclip/matchers/have_attached_file_matcher.rb
davidray-paperclip-2.3.3 lib/paperclip/matchers/have_attached_file_matcher.rb