Sha256: 2afa3e1c19690af94b0d10f7b9672b451dfbe020ae1dac7c522757e550f03990

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 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 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
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
paperclip-4.1.1 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-4.1.0 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-3.5.4 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-4.0.0 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-3.5.3 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-3.5.2 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-3.5.1 lib/paperclip/matchers/have_attached_file_matcher.rb
paperclip-3.5.0 lib/paperclip/matchers/have_attached_file_matcher.rb