Commit 1e28d592176620ad7d1a9eecdd36736b04fd0c60

Authored by Daniela Feitosa
Committed by Joenio Costa
1 parent 48410898

ActionItem1144: adding error message when profile image is big

app/controllers/my_profile/profile_editor_controller.rb
@@ -14,7 +14,12 @@ class ProfileEditorController < MyProfileController @@ -14,7 +14,12 @@ class ProfileEditorController < MyProfileController
14 @possible_domains = profile.possible_domains 14 @possible_domains = profile.possible_domains
15 if request.post? 15 if request.post?
16 if profile.update_attributes(params[:profile_data]) 16 if profile.update_attributes(params[:profile_data])
17 - redirect_to :action => 'index' 17 + if profile.image && profile.image.errors.any?
  18 + @errors = profile.image.errors
  19 + render :action => 'edit'
  20 + else
  21 + redirect_to :action => 'index'
  22 + end
18 end 23 end
19 end 24 end
20 end 25 end
app/models/image.rb
1 class Image < ActiveRecord::Base 1 class Image < ActiveRecord::Base
2 belongs_to :owner, :polymorphic => true 2 belongs_to :owner, :polymorphic => true
3 - 3 +
  4 + def self.max_size
  5 + Image.attachment_options[:max_size]
  6 + end
  7 +
4 has_attachment :content_type => :image, 8 has_attachment :content_type => :image,
5 :storage => :file_system, 9 :storage => :file_system,
6 :max_size => 500.kilobytes, 10 :max_size => 500.kilobytes,
@@ -11,5 +15,5 @@ class Image &lt; ActiveRecord::Base @@ -11,5 +15,5 @@ class Image &lt; ActiveRecord::Base
11 :minor => '50x50', 15 :minor => '50x50',
12 :icon => '20x20!' } 16 :icon => '20x20!' }
13 17
14 - validates_as_attachment 18 + validates_attachment :size => N_("The file you uploaded was larger than the maximum size of %s") % Image.max_size.to_humanreadable
15 end 19 end
app/views/profile_editor/edit.rhtml
@@ -2,6 +2,18 @@ @@ -2,6 +2,18 @@
2 2
3 <%= error_messages_for :profile %> 3 <%= error_messages_for :profile %>
4 4
  5 +<% if @errors %>
  6 + <div class="errorExplanation" id="errorExplanation">
  7 + <h2><%= n_('This file couldn\'t be saved', 'These %{num} files couldn\'t be saved', @errors.size) % { :num => @errors.size } %></h2>
  8 + <p><%= _('There were problems with the following files:') %> </p>
  9 + <ul>
  10 + <% for msg in profile.image.errors.full_messages %>
  11 + <li><strong><%= profile.image.filename %></strong> : <%= msg %></li>
  12 + <% end %>
  13 + </ul>
  14 + </div>
  15 +<% end %>
  16 +
5 <% labelled_form_for :profile_data, @profile, :html => { :id => 'profile-data', :multipart => true } do |f| %> 17 <% labelled_form_for :profile_data, @profile, :html => { :id => 'profile-data', :multipart => true } do |f| %>
6 18
7 <%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %> 19 <%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %>
@@ -9,7 +21,7 @@ @@ -9,7 +21,7 @@
9 <div id="profile_change_picture"> 21 <div id="profile_change_picture">
10 <h2><%= _('Change picture') %></h2> 22 <h2><%= _('Change picture') %></h2>
11 <% f.fields_for :image_builder, @profile.image do |i| %> 23 <% f.fields_for :image_builder, @profile.image do |i| %>
12 - <%= file_field_or_thumbnail(_('Image:'), @profile.image, i) %> 24 + <%= file_field_or_thumbnail(_('Image:'), @profile.image, i) %><%= _("(max size %s)")% Image.max_size.to_humanreadable %>
13 <% end %> 25 <% end %>
14 </div> 26 </div>
15 27
test/functional/profile_editor_controller_test.rb
@@ -667,4 +667,23 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase @@ -667,4 +667,23 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
667 assert_nil Profile.find(profile.id).preferred_domain 667 assert_nil Profile.find(profile.id).preferred_domain
668 end 668 end
669 669
  670 + should 'display error message when image has more than max size' do
  671 + Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
  672 + post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
  673 + assert_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
  674 + end
  675 +
  676 + should 'not display error message when image has less than max size' do
  677 + Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] - 1024)
  678 + post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
  679 + assert_no_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
  680 + end
  681 +
  682 + should 'not redirect when some file has errors' do
  683 + Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
  684 + post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
  685 + assert_response :success
  686 + assert_template 'edit'
  687 + end
  688 +
670 end 689 end