diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb index 71d2f57..614939a 100644 --- a/app/models/uploaded_file.rb +++ b/app/models/uploaded_file.rb @@ -5,12 +5,15 @@ class UploadedFile < Article # FIXME need to define min/max file size + # + # default max_size is 1.megabyte to redefine it set options: + # :min_size => 2.megabytes + # :max_size => 5.megabytes has_attachment :storage => :file_system, :thumbnails => { :icon => [24,24] }, :thumbnail_class => Thumbnail - - validates_as_attachment + validates_attachment :size => _('The file you uploaded was larger than the maximum size of 1MB') def icon_name self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-') diff --git a/test/unit/uploaded_file_test.rb b/test/unit/uploaded_file_test.rb index 2497bba..040a7b7 100644 --- a/test/unit/uploaded_file_test.rb +++ b/test/unit/uploaded_file_test.rb @@ -58,4 +58,14 @@ class UploadedFileTest < Test::Unit::TestCase assert file.save end + should 'has attachment_fu validation options' do + file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + assert_respond_to file, :attachment_validation_options + end + + should 'has attachment_fu validation option for size' do + file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) + assert_includes file.attachment_validation_options, :size + end + end diff --git a/vendor/plugins/attachment_fu_validates_attachment/init.rb b/vendor/plugins/attachment_fu_validates_attachment/init.rb new file mode 100644 index 0000000..0db131d --- /dev/null +++ b/vendor/plugins/attachment_fu_validates_attachment/init.rb @@ -0,0 +1,34 @@ +# friendlier attachment validations by Tim Lucas +# ref.: http://toolmantim.com/article/2007/12/3/rollin_your_own_attachment_fu_messages_evil_twin_stylee + +Technoweenie::AttachmentFu::InstanceMethods.module_eval do + protected + def attachment_valid? + if self.filename.nil? + errors.add_to_base attachment_validation_options[:empty] + return + end + [:content_type, :size].each do |option| + if attachment_validation_options[option] && attachment_options[option] && !attachment_options[option].include?(self.send(option)) + errors.add_to_base attachment_validation_options[option] + end + end + end +end + +Technoweenie::AttachmentFu::ClassMethods.module_eval do + # Options: + # * :empty - Base error message when no file is uploaded. Default is "No file uploaded" + # * :content_type - Base error message when the uploaded file is not a valid content type. + # * :size - Base error message when the uploaded file is not a valid size. + # + # Example: + # validates_attachment :content_type => "The file you uploaded was not a JPEG, PNG or GIF", + # :size => "The image you uploaded was larger than the maximum size of 10MB" + def validates_attachment(options={}) + options[:empty] ||= "No file uploaded" + class_inheritable_accessor :attachment_validation_options + self.attachment_validation_options = options + validate :attachment_valid? + end +end -- libgit2 0.21.2