Skip to content
This repository

Lambda (or Proc) support in validation messages #411

Merged
1 commit merged into from about 3 years ago

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

Showing 1 unique commit by 1 author.

Feb 04, 2011
Franco Catena Added lambda messages to validators 0bca09a
This page is out of date. Refresh to see the latest.
2  lib/paperclip.rb
@@ -276,6 +276,7 @@ def validates_attachment_size name, options = {}
276 276
       max     = options[:less_than]    || (options[:in] && options[:in].last)  || (1.0/0)
277 277
       range   = (min..max)
278 278
       message = options[:message] || "file size must be between :min and :max bytes."
  279
+      message = message.call if message.respond_to?(:call)
279 280
       message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
280 281
 
281 282
       validates_inclusion_of :"#{name}_file_size",
@@ -330,6 +331,7 @@ def validates_attachment_content_type name, options = {}
330 331
         if !allowed_types.any?{|t| t === value } && !(value.nil? || value.blank?)
331 332
           if record.errors.method(:add).arity == -2
332 333
             message = options[:message] || "is not one of #{allowed_types.join(", ")}"
  334
+            message = message.call if message.respond_to?(:call)
333 335
             record.errors.add(:"#{name}_content_type", message)
334 336
           else
335 337
             record.errors.add(:"#{name}_content_type", :inclusion, :default => options[:message], :value => value)
30  test/paperclip_test.rb
@@ -252,6 +252,21 @@ def self.should_validate validation, options, valid_file, invalid_file
252 252
       should_validate validation, options, valid_file, invalid_file
253 253
     end
254 254
 
  255
+    context "with content_type validation and lambda message" do
  256
+      context "and assigned an invalid file" do
  257
+        setup do
  258
+          Dummy.send(:"validates_attachment_content_type", :avatar, :content_type => %r{image/.*}, :message => lambda {'lambda content type message'})
  259
+          @dummy = Dummy.new
  260
+          @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "text.txt"), "rb")
  261
+          @dummy.valid?
  262
+        end
  263
+
  264
+        should "have a content type error message" do
  265
+          assert [@dummy.errors[:avatar_content_type]].flatten.any?{|error| error =~ %r/lambda content type message/ }
  266
+        end
  267
+      end
  268
+    end
  269
+
255 270
     context "with size validation and less_than 10240 option" do
256 271
       context "and assigned an invalid file" do
257 272
         setup do
@@ -267,5 +282,20 @@ def self.should_validate validation, options, valid_file, invalid_file
267 282
       end
268 283
     end
269 284
 
  285
+    context "with size validation and less_than 10240 option with lambda message" do
  286
+      context "and assigned an invalid file" do
  287
+        setup do
  288
+          Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240, :message => lambda {'lambda between 0 and 10240 bytes'})
  289
+          @dummy = Dummy.new
  290
+          @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
  291
+          @dummy.valid?
  292
+        end
  293
+
  294
+        should "have a file size min/max error message" do
  295
+          assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/lambda between 0 and 10240 bytes/ }
  296
+        end
  297
+      end
  298
+    end
  299
+
270 300
   end
271 301
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.