Commit 1c4bc5e782b2cb6bc83566f893f5d9d5efb61750

Authored by JoenioCosta
1 parent 42eee5cd

ActionItem461: told user about the upload limit before uploading


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2049 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/profile_editor_controller.rb
... ... @@ -39,4 +39,3 @@ class ProfileEditorController < MyProfileController
39 39 end
40 40  
41 41 end
42   -
... ...
app/models/uploaded_file.rb
... ... @@ -13,7 +13,11 @@ class UploadedFile < Article
13 13 :thumbnails => { :icon => [24,24] },
14 14 :thumbnail_class => Thumbnail
15 15  
16   - validates_attachment :size => _('The file you uploaded was larger than the maximum size of 1MB')
  16 + def self.max_size
  17 + UploadedFile.attachment_options[:max_size]
  18 + end
  19 +
  20 + validates_attachment :size => _("The file you uploaded was larger than the maximum size of %s") % UploadedFile.max_size.to_humanreadable
17 21  
18 22 def icon_name
19 23 self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-')
... ...
app/views/cms/_uploaded_file.rhtml
1   -<%= labelled_form_field(_('Select the file you want to upload.'), file_field(:article, :uploaded_data)) %>
  1 +<%= labelled_form_field(_("Select the file you want to upload (max size %s).") % UploadedFile.max_size.to_humanreadable, file_field(:article, :uploaded_data)) %>
2 2  
3 3 <%= labelled_form_field(_('Describe this file:'), text_area(:article, :abstract)) %>
... ...
lib/noosfero/core_ext.rb
1 1 require 'noosfero/core_ext/string'
  2 +require 'noosfero/core_ext/integer'
... ...
lib/noosfero/core_ext/integer.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +class Integer
  2 + def to_humanreadable
  3 + value = self
  4 + if value < 1023
  5 + return "%i bytes" % value
  6 + end
  7 + value /= 1024
  8 +
  9 + if value < 1023
  10 + return "%1.1f KB" % value
  11 + end
  12 + value /= 1024
  13 +
  14 + if value < 1023
  15 + return "%1.1f MB" % value
  16 + end
  17 + value /= 1024
  18 +
  19 + if value < 1023
  20 + return "%1.1f GB" % value
  21 + end
  22 + value /= 1024
  23 +
  24 + if value < 1023
  25 + return "%1.1f TB" % value
  26 + end
  27 + value /= 1024
  28 +
  29 + if value < 1023
  30 + return "%1.1f PB" % value
  31 + end
  32 + value /= 1024
  33 +
  34 + if value < 1023
  35 + return "%1.1f EB" % value
  36 + end
  37 + value /= 1024
  38 + end
  39 +end
... ...
test/functional/cms_controller_test.rb
... ... @@ -248,6 +248,11 @@ class CmsControllerTest &lt; Test::Unit::TestCase
248 248 end
249 249 end
250 250  
  251 + should 'display max size of uploaded file' do
  252 + get :new, :type => UploadedFile.name, :profile => profile.identifier
  253 + assert_tag :tag => 'label', :attributes => { :for => 'article_uploaded_data' }, :content => /max size #{UploadedFile.max_size.to_humanreadable}/
  254 + end
  255 +
251 256 should 'display checkboxes for selecting categories' do
252 257 env = Environment.default
253 258 top = env.categories.build(:display_in_menu => true, :name => 'Top-Level category'); top.save!
... ...
test/unit/integer_core_ext_test.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +# tests for Integer core extension. See lib/noosfero/core_ext/integer.rb
  4 +class IntegerCoreExtTest < Test::Unit::TestCase
  5 +
  6 + should 'display bytes in human readable' do
  7 + assert_equal '2 bytes', 2.bytes.to_humanreadable
  8 + end
  9 +
  10 + should 'display kilobytes in human readable' do
  11 + assert_equal '1.0 KB', 1.kilobytes.to_humanreadable
  12 + end
  13 +
  14 + should 'display megabytes in human readable' do
  15 + assert_equal '1.0 MB', 1.megabytes.to_humanreadable
  16 + end
  17 +
  18 + should 'display gigabytes in human readable' do
  19 + assert_equal '1.0 GB', 1.gigabytes.to_humanreadable
  20 + end
  21 +
  22 + should 'display terabytes in human readable' do
  23 + assert_equal '1.0 TB', 1.terabytes.to_humanreadable
  24 + end
  25 +
  26 + should 'display petabytes in human readable' do
  27 + assert_equal '1.0 PB', 1.petabytes.to_humanreadable
  28 + end
  29 +
  30 + should 'display exabytes in human readable' do
  31 + assert_equal '1.0 EB', 1.exabytes.to_humanreadable
  32 + end
  33 +
  34 +end
... ...