Sha256: e91cd93d8de7d863016ac0a6098966dde3799ddfe8739c1b7ae0e460a4d651ff

Contents?: true

Size: 1.42 KB

Versions: 32

Compression:

Stored size: 1.42 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}") &&
            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

32 entries across 30 versions & 6 rubygems

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