Sha256: 929f63af69cbbbfe91afac222cda6a5969ddec770f590122e87c42023309c759

Contents?: true

Size: 1.67 KB

Versions: 14

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

14 entries across 14 versions & 1 rubygems

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