Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Lambda (or Proc) support in validation messages #411

Merged
1 commit merged into from

1 participant

Franco Catena
Franco Catena

I added lambda support to the size and content type validator, so now you can do something like:

validates_attachment_size :avatar, :less_than => 2.megabytes, :message => lambda { I18n.t('invalid_image_size') }

This is consistent with Rails validators, wich receive either a String or a Proc.

Regards.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 4, 2011
  1. Franco Catena
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 0 deletions.
  1. +2 −0  lib/paperclip.rb
  2. +30 −0 test/paperclip_test.rb
2  lib/paperclip.rb
View
@@ -276,6 +276,7 @@ def validates_attachment_size name, options = {}
max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0)
range = (min..max)
message = options[:message] || "file size must be between :min and :max bytes."
+ message = message.call if message.respond_to?(:call)
message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
validates_inclusion_of :"#{name}_file_size",
@@ -330,6 +331,7 @@ def validates_attachment_content_type name, options = {}
if !allowed_types.any?{|t| t === value } && !(value.nil? || value.blank?)
if record.errors.method(:add).arity == -2
message = options[:message] || "is not one of #{allowed_types.join(", ")}"
+ message = message.call if message.respond_to?(:call)
record.errors.add(:"#{name}_content_type", message)
else
record.errors.add(:"#{name}_content_type", :inclusion, :default => options[:message], :value => value)
30 test/paperclip_test.rb
View
@@ -252,6 +252,21 @@ def self.should_validate validation, options, valid_file, invalid_file
should_validate validation, options, valid_file, invalid_file
end
+ context "with content_type validation and lambda message" do
+ context "and assigned an invalid file" do
+ setup do
+ Dummy.send(:"validates_attachment_content_type", :avatar, :content_type => %r{image/.*}, :message => lambda {'lambda content type message'})
+ @dummy = Dummy.new
+ @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "text.txt"), "rb")
+ @dummy.valid?
+ end
+
+ should "have a content type error message" do
+ assert [@dummy.errors[:avatar_content_type]].flatten.any?{|error| error =~ %r/lambda content type message/ }
+ end
+ end
+ end
+
context "with size validation and less_than 10240 option" do
context "and assigned an invalid file" do
setup do
@@ -267,5 +282,20 @@ def self.should_validate validation, options, valid_file, invalid_file
end
end
+ context "with size validation and less_than 10240 option with lambda message" do
+ context "and assigned an invalid file" do
+ setup do
+ Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240, :message => lambda {'lambda between 0 and 10240 bytes'})
+ @dummy = Dummy.new
+ @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
+ @dummy.valid?
+ end
+
+ should "have a file size min/max error message" do
+ assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/lambda between 0 and 10240 bytes/ }
+ end
+ end
+ end
+
end
end
Something went wrong with that request. Please try again.