Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix a bug when passing a method name to :if and :unless option in…
… `validates_attachment_presence`

Fixes #631
  • Loading branch information
sikachu committed Oct 21, 2011
1 parent 35b91fb commit 2583a27
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/paperclip.rb
Expand Up @@ -385,13 +385,13 @@ def validates_attachment_thumbnails name, options = {}
# Places ActiveRecord-style validations on the presence of a file.
# Options:
# * +if+: A lambda or name of a method on the instance. Validation will only
# be run is this lambda or method returns true.
# be run if this lambda or method returns true.
# * +unless+: Same as +if+ but validates if lambda or method returns false.
def validates_attachment_presence name, options = {}
message = options[:message] || :empty
validates_each :"#{name}_file_name" do |record, attr, value|
if_clause_passed = options[:if].nil? || (options[:if].call(record) != false)
unless_clause_passed = options[:unless].nil? || (!!options[:unless].call(record) == false)
if_clause_passed = options[:if].nil? || (options[:if].respond_to?(:call) ? options[:if].call(record) != false : record.send(options[:if]))
unless_clause_passed = options[:unless].nil? || (options[:unless].respond_to?(:call) ? !!options[:unless].call(record) == false : !record.send(options[:unless]))
if if_clause_passed && unless_clause_passed && value.blank?
record.errors.add(name, message)
record.errors.add("#{name}_file_name", message)
Expand Down
84 changes: 62 additions & 22 deletions test/paperclip_test.rb
Expand Up @@ -170,38 +170,78 @@ class ::SubDummy < Dummy; end
end

context "a validation with an if guard clause" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
@dummy = Dummy.new
@dummy.stubs(:avatar_file_name).returns(nil)
end
context "as a lambda" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
@dummy = Dummy.new
@dummy.stubs(:avatar_file_name).returns(nil)
end

should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(true)
assert ! @dummy.valid?
end

should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(true)
assert ! @dummy.valid?
should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(false)
assert @dummy.valid?
end
end

should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(false)
assert @dummy.valid?
context "as a method name" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :if => :foo)
@dummy = Dummy.new
@dummy.stubs(:avatar_file_name).returns(nil)
end

should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(true)
assert ! @dummy.valid?
end

should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(false)
assert @dummy.valid?
end
end
end

context "a validation with an unless guard clause" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
@dummy = Dummy.new
@dummy.stubs(:avatar_file_name).returns(nil)
end
context "as a lambda" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
@dummy = Dummy.new
@dummy.stubs(:avatar_file_name).returns(nil)
end

should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(false)
assert ! @dummy.valid?
end

should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(false)
assert ! @dummy.valid?
should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(true)
assert @dummy.valid?
end
end

should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(true)
assert @dummy.valid?
context "as a method name" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :unless => :foo)
@dummy = Dummy.new
@dummy.stubs(:avatar_file_name).returns(nil)
end

should "attempt validation if the guard returns true" do
@dummy.expects(:foo).returns(false)
assert ! @dummy.valid?
end

should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(true)
assert @dummy.valid?
end
end
end

Expand Down

2 comments on commit 2583a27

@edison
Copy link
Contributor

@edison edison commented on 2583a27 Oct 28, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if it could be released sooner.

@andrewtheis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Please sign in to comment.