Permalink
Browse files

- Allow for custom CarrierWave error messages.

  • Loading branch information...
1 parent aea8d4c commit 5585a1fd5a8207be653da9ba4236369a5db66428 Bart Teeuwisse committed Sep 6, 2011
Showing with 8 additions and 23 deletions.
  1. +1 −1 lib/carrierwave/locale/en.yml
  2. +7 −22 lib/carrierwave/validations/active_model.rb
@@ -2,4 +2,4 @@ en:
errors:
messages:
carrierwave_processing_error: failed to be processed
- carrierwave_integrity_error: is not an allowed file type
+ carrierwave_integrity_error: is not of an allowed file type
@@ -14,17 +14,19 @@ module ActiveModel
class ProcessingValidator < ::ActiveModel::EachValidator
def validate_each(record, attribute, value)
- if record.send("#{attribute}_processing_error")
- record.errors.add(attribute, :carrierwave_processing_error)
+ if e = record.send("#{attribute}_processing_error")
+ message = (e.message == e.class.to_s) ? :carrierwave_processing_error : e.message
+ record.errors.add(attribute, message)
end
end
end
class IntegrityValidator < ::ActiveModel::EachValidator
def validate_each(record, attribute, value)
- if record.send("#{attribute}_integrity_error")
- record.errors.add(attribute, :carrierwave_integrity_error)
+ if e = record.send("#{attribute}_integrity_error")
+ message = (e.message == e.class.to_s) ? :carrierwave_integrity_error : e.message
+ record.errors.add(attribute, message)
end
end
end
@@ -36,14 +38,6 @@ module HelperMethods
#
# Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
#
- # === Note
- #
- # Set this key in your translations file for I18n:
- #
- # carrierwave:
- # errors:
- # integrity: 'Here be an error message'
- #
def validates_integrity_of(*attr_names)
validates_with IntegrityValidator, _merge_attributes(attr_names)
end
@@ -54,14 +48,6 @@ def validates_integrity_of(*attr_names)
#
# Accepts the usual parameters for validations in Rails (:if, :unless, etc...)
#
- # === Note
- #
- # Set this key in your translations file for I18n:
- #
- # carrierwave:
- # errors:
- # processing: 'Here be an error message'
- #
def validates_processing_of(*attr_names)
validates_with ProcessingValidator, _merge_attributes(attr_names)
end
@@ -75,5 +61,4 @@ def validates_processing_of(*attr_names)
end
end
-I18n.load_path << File.join(File.dirname(__FILE__), "..", "locale", 'en.yml')
-
+I18n.load_path << File.join(File.dirname(__FILE__), "..", "locale", 'en.yml')

0 comments on commit 5585a1f

Please sign in to comment.