Commit 2583a27d by Prem Sichanugrist

Fix a bug when passing a method name to `:if` and `:unless` option in…

Fix a bug when passing a method name to `:if` and `:unless` option in `validates_attachment_presence`

Fixes #631
parent 35b91fb7
......@@ -385,13 +385,13 @@ module Paperclip
# 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)
......
......@@ -170,6 +170,7 @@ class PaperclipTest < Test::Unit::TestCase
end
context "a validation with an if guard clause" do
context "as a lambda" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
@dummy = Dummy.new
......@@ -187,7 +188,27 @@ class PaperclipTest < Test::Unit::TestCase
end
end
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
context "as a lambda" do
setup do
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
@dummy = Dummy.new
......@@ -205,6 +226,25 @@ class PaperclipTest < Test::Unit::TestCase
end
end
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
should "not have Attachment in the ActiveRecord::Base namespace" do
assert_raises(NameError) do
ActiveRecord::Base::Attachment
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment