Commit 89b238e2280a5816a06b8d9da1272fb2df893804
1 parent
59f6602a
Exists in
master
and in
23 other branches
ActionItem461: add custom message to uploaded file larger then size limit
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2044 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
3 changed files
with
49 additions
and
2 deletions
Show diff stats
app/models/uploaded_file.rb
| ... | ... | @@ -5,12 +5,15 @@ |
| 5 | 5 | class UploadedFile < Article |
| 6 | 6 | |
| 7 | 7 | # FIXME need to define min/max file size |
| 8 | + # | |
| 9 | + # default max_size is 1.megabyte to redefine it set options: | |
| 10 | + # :min_size => 2.megabytes | |
| 11 | + # :max_size => 5.megabytes | |
| 8 | 12 | has_attachment :storage => :file_system, |
| 9 | 13 | :thumbnails => { :icon => [24,24] }, |
| 10 | 14 | :thumbnail_class => Thumbnail |
| 11 | - | |
| 12 | 15 | |
| 13 | - validates_as_attachment | |
| 16 | + validates_attachment :size => _('The file you uploaded was larger than the maximum size of 1MB') | |
| 14 | 17 | |
| 15 | 18 | def icon_name |
| 16 | 19 | self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-') | ... | ... |
test/unit/uploaded_file_test.rb
| ... | ... | @@ -58,4 +58,14 @@ class UploadedFileTest < Test::Unit::TestCase |
| 58 | 58 | assert file.save |
| 59 | 59 | end |
| 60 | 60 | |
| 61 | + should 'has attachment_fu validation options' do | |
| 62 | + file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) | |
| 63 | + assert_respond_to file, :attachment_validation_options | |
| 64 | + end | |
| 65 | + | |
| 66 | + should 'has attachment_fu validation option for size' do | |
| 67 | + file = UploadedFile.new(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) | |
| 68 | + assert_includes file.attachment_validation_options, :size | |
| 69 | + end | |
| 70 | + | |
| 61 | 71 | end | ... | ... |
vendor/plugins/attachment_fu_validates_attachment/init.rb
0 → 100644
| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | +# friendlier attachment validations by Tim Lucas | |
| 2 | +# ref.: http://toolmantim.com/article/2007/12/3/rollin_your_own_attachment_fu_messages_evil_twin_stylee | |
| 3 | + | |
| 4 | +Technoweenie::AttachmentFu::InstanceMethods.module_eval do | |
| 5 | + protected | |
| 6 | + def attachment_valid? | |
| 7 | + if self.filename.nil? | |
| 8 | + errors.add_to_base attachment_validation_options[:empty] | |
| 9 | + return | |
| 10 | + end | |
| 11 | + [:content_type, :size].each do |option| | |
| 12 | + if attachment_validation_options[option] && attachment_options[option] && !attachment_options[option].include?(self.send(option)) | |
| 13 | + errors.add_to_base attachment_validation_options[option] | |
| 14 | + end | |
| 15 | + end | |
| 16 | + end | |
| 17 | +end | |
| 18 | + | |
| 19 | +Technoweenie::AttachmentFu::ClassMethods.module_eval do | |
| 20 | + # Options: | |
| 21 | + # * <tt>:empty</tt> - Base error message when no file is uploaded. Default is "No file uploaded" | |
| 22 | + # * <tt>:content_type</tt> - Base error message when the uploaded file is not a valid content type. | |
| 23 | + # * <tt>:size</tt> - Base error message when the uploaded file is not a valid size. | |
| 24 | + # | |
| 25 | + # Example: | |
| 26 | + # validates_attachment :content_type => "The file you uploaded was not a JPEG, PNG or GIF", | |
| 27 | + # :size => "The image you uploaded was larger than the maximum size of 10MB" | |
| 28 | + def validates_attachment(options={}) | |
| 29 | + options[:empty] ||= "No file uploaded" | |
| 30 | + class_inheritable_accessor :attachment_validation_options | |
| 31 | + self.attachment_validation_options = options | |
| 32 | + validate :attachment_valid? | |
| 33 | + end | |
| 34 | +end | ... | ... |