Commit 80f9b309 by Scott Carleton Committed by Jon Yurek

slice for conditional lambda and add to options

parent 282f1161
...@@ -37,6 +37,8 @@ module Paperclip ...@@ -37,6 +37,8 @@ module Paperclip
validator_options = options.delete(validator_kind) validator_options = options.delete(validator_kind)
validator_options = {} if validator_options == true validator_options = {} if validator_options == true
local_options = attributes + [validator_options] local_options = attributes + [validator_options]
conditional_options = options.slice(:if, :unless)
local_options.last.merge!(conditional_options)
send(:"validates_attachment_#{validator_kind}", *local_options) send(:"validates_attachment_#{validator_kind}", *local_options)
end end
end end
......
...@@ -29,4 +29,33 @@ class ValidatorsTest < Test::Unit::TestCase ...@@ -29,4 +29,33 @@ class ValidatorsTest < Test::Unit::TestCase
assert_raise(RuntimeError){ dummy.valid? } assert_raise(RuntimeError){ dummy.valid? }
end end
end end
context "using the helper with a conditional" do
setup do
Dummy.validates_attachment :avatar, :presence => true,
:content_type => { :content_type => "image/jpeg" },
:size => { :in => 0..10.kilobytes },
:if => :title_present?
end
should "validate the attachment if title is present" do
Dummy.class_eval do
def title_present?
true
end
end
dummy = Dummy.new(:avatar => File.new(fixture_file("12k.png")))
assert_equal [:avatar_content_type, :avatar, :avatar_file_size], dummy.errors.keys
end
should "not validate attachment if tile is not present" do
Dummy.class_eval do
def title_present?
false
end
end
dummy = Dummy.new(:avatar => File.new(fixture_file("12k.png")))
assert_equal [], dummy.errors.keys
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