Sha256: 428c1f4079276b3a81422941aaf3554717e9f9c9b4047feda9125a3b335dbca7

Contents?: true

Size: 1.67 KB

Versions: 32

Compression:

Stored size: 1.67 KB

Contents

require 'spec_helper'
require 'paperclip/matchers'

describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
  extend Paperclip::Shoulda::Matchers

  before do
    reset_table("dummies") do |d|
      d.string :avatar_file_name
    end
    reset_class "Dummy"
    Dummy.has_attached_file :avatar
    Dummy.do_not_validate_attachment_file_type :avatar
  end

  it "rejects a class with no validation" do
    expect(matcher).to_not accept(Dummy)
  end

  it "accepts a class with a matching validation" do
    Dummy.validates_attachment_presence :avatar
    expect(matcher).to accept(Dummy)
  end

  it "accepts an instance with other attachment validations" do
    reset_table("dummies") do |d|
      d.string :avatar_file_name
      d.string :avatar_content_type
    end
    Dummy.class_eval do
      validates_attachment_presence :avatar
      validates_attachment_content_type :avatar, content_type: 'image/gif'
    end
    dummy = Dummy.new

    dummy.avatar = File.new fixture_file('5k.png')

    expect(matcher).to accept(dummy)
  end

  context "using an :if to control the validation" do
    before do
      Dummy.class_eval do
        validates_attachment_presence :avatar, if: :go
        attr_accessor :go
      end
    end

    it "runs the validation if the control is true" do
      dummy = Dummy.new
      dummy.avatar = nil
      dummy.go = true
      expect(matcher).to accept(dummy)
    end

    it "does not run the validation if the control is false" do
      dummy = Dummy.new
      dummy.avatar = nil
      dummy.go = false
      expect(matcher).to_not accept(dummy)
    end
  end

  private

  def matcher
    self.class.validate_attachment_presence(:avatar)
  end
end

Version data entries

32 entries across 30 versions & 6 rubygems

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