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 ...@@ -385,13 +385,13 @@ module Paperclip
# Places ActiveRecord-style validations on the presence of a file. # Places ActiveRecord-style validations on the presence of a file.
# Options: # Options:
# * +if+: A lambda or name of a method on the instance. Validation will only # * +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. # * +unless+: Same as +if+ but validates if lambda or method returns false.
def validates_attachment_presence name, options = {} def validates_attachment_presence name, options = {}
message = options[:message] || :empty message = options[:message] || :empty
validates_each :"#{name}_file_name" do |record, attr, value| validates_each :"#{name}_file_name" do |record, attr, value|
if_clause_passed = options[:if].nil? || (options[:if].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].call(record) == false) 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? if if_clause_passed && unless_clause_passed && value.blank?
record.errors.add(name, message) record.errors.add(name, message)
record.errors.add("#{name}_file_name", message) record.errors.add("#{name}_file_name", message)
......
...@@ -170,38 +170,78 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -170,38 +170,78 @@ class PaperclipTest < Test::Unit::TestCase
end end
context "a validation with an if guard clause" do context "a validation with an if guard clause" do
setup do context "as a lambda" do
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo }) setup do
@dummy = Dummy.new Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
@dummy.stubs(:avatar_file_name).returns(nil) @dummy = Dummy.new
end @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 should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(true) @dummy.expects(:foo).returns(false)
assert ! @dummy.valid? assert @dummy.valid?
end
end end
should "not attempt validation if the guard returns false" do context "as a method name" do
@dummy.expects(:foo).returns(false) setup do
assert @dummy.valid? 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
end end
context "a validation with an unless guard clause" do context "a validation with an unless guard clause" do
setup do context "as a lambda" do
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo }) setup do
@dummy = Dummy.new Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
@dummy.stubs(:avatar_file_name).returns(nil) @dummy = Dummy.new
end @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 should "not attempt validation if the guard returns false" do
@dummy.expects(:foo).returns(false) @dummy.expects(:foo).returns(true)
assert ! @dummy.valid? assert @dummy.valid?
end
end end
should "not attempt validation if the guard returns false" do context "as a method name" do
@dummy.expects(:foo).returns(true) setup do
assert @dummy.valid? 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
end end
......
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