thoughtbot/paperclip — Issue #649: Why do the Paperclip Matchers loose the subject and create a new object?
Author: sachin87
Created: 2011-11-21 14:49:37+00:00
State: open

It appears that Paperclip matchers, loose the subject. If the subject is an Object and not a Class then the subject gets changed to the Class of the Object. Matchers then run the presence validation test on the class itself. Since my real subject is now lost, my validations fail because the Proc associated with the attachment is now associated with this "blank new" object and not on the subject I provided.

If I provide an object, then it should run on the object, If I provide a class then the matcher should new a new on the class. It should not meddle with the subject provided.

e.g.
Given the class

class Ponies < ActiveRecord::Base
has_attachment :pony_pics
validate_attachment_presence :pony_pics, :if => Proc.new{|x| !!x.rainbows_have_risen }
attribute_accessor :rainbows_have_risen
end

In the tests -->

@unicorny = Ponies.new
@unicorny.rainbows_have_risen = false
@unicorny.should validate_attachment_presence(:pony_pics) # => this is ALWAYS failing
@unicorny.rainbows_have_risen = true
@unicorny.should validate_attachment_presence(:pony_pics) # => this is ALWAYS failing

Essentially my virtual attribute is ignore, it always evaluates to nil, which does is not equal to false (in the lib/paperclip.rb) and thus the error is NEVER added. and validate_attachment_presence(:pony_pics) ALWAYS returns false.

This is because matching is happening NOT between the subject @unicorny but on the subject Ponies.new.