Commit 3d745db9 by austin.bain

Added option to customize the validation error messages

parent 044acfcd
......@@ -147,6 +147,7 @@ module Paperclip
# * +in+: a Range of bytes (i.e. +1..1.megabyte+),
# * +less_than+: equivalent to :in => 0..options[:less_than]
# * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
# * +message+: error message to display, use #{min} and #{max} as replacements
def validates_attachment_size name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
unless options[:greater_than].nil?
......@@ -156,16 +157,23 @@ module Paperclip
options[:in] = (0..options[:less_than])
end
unless options[:in].include? instance[:"#{name}_file_size"].to_i
"file size is not between #{options[:in].first} and #{options[:in].last} bytes."
min = options[:in].first
max = options[:in].last
if options[:message]
eval('"' + options[:message] + '"')
else
"file size is not between #{min} and #{max} bytes."
end
end
end
end
# Places ActiveRecord-style validations on the presence of a file.
def validates_attachment_presence name
def validates_attachment_presence name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
if attachment.original_filename.blank?
"must be set."
options[:message] || "must be set."
end
end
end
......
......@@ -64,11 +64,14 @@ class PaperclipTest < Test::Unit::TestCase
assert Dummy.new.respond_to?(:avatar=)
end
[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:content_type1,{:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type2, {:content_type => :image}, "5k.png", "text.txt"],
[:content_type3, {:content_type => "text/plain"}, "text.txt", "5k.png"]].each do |args|
[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:content_type1,{:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type2, {:content_type => :image}, "5k.png", "text.txt"],
[:content_type3, {:content_type => "text/plain"}, "text.txt", "5k.png"],
[:presence2, {:message => "error"}, "5k.png", nil, "error"],
[:size2, {:in => 1..10240, :message => 'size is not between #{min} and #{max} bytes.'}, "5k.png", "12k.png", "size is not between 1 and 10240 bytes."],
[:content_type4,{:content_type => "image/png", :message => "error"}, "5k.png", "text.txt", "error"]].each do |args|
context "with #{args[0]} validations" do
setup do
Dummy.class_eval do
......@@ -100,6 +103,21 @@ class PaperclipTest < Test::Unit::TestCase
assert_equal 1, @dummy.avatar.errors.length
end
end
context "and an invalid file with :message" do
setup do
@file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
end
should "have errors" do
if args[1] && args[1][:message] && args[4]
@dummy.avatar = @file
assert ! @dummy.avatar.valid?
assert_equal 1, @dummy.avatar.errors.length
assert_equal args[4], @dummy.avatar.errors[0]
end
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