Commit 1e28d592176620ad7d1a9eecdd36736b04fd0c60
Committed by
Joenio Costa
1 parent
48410898
Exists in
master
and in
28 other branches
ActionItem1144: adding error message when profile image is big
Showing
4 changed files
with
44 additions
and
4 deletions
Show diff stats
app/controllers/my_profile/profile_editor_controller.rb
... | ... | @@ -14,7 +14,12 @@ class ProfileEditorController < MyProfileController |
14 | 14 | @possible_domains = profile.possible_domains |
15 | 15 | if request.post? |
16 | 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 | 23 | end |
19 | 24 | end |
20 | 25 | end | ... | ... |
app/models/image.rb
1 | 1 | class Image < ActiveRecord::Base |
2 | 2 | belongs_to :owner, :polymorphic => true |
3 | - | |
3 | + | |
4 | + def self.max_size | |
5 | + Image.attachment_options[:max_size] | |
6 | + end | |
7 | + | |
4 | 8 | has_attachment :content_type => :image, |
5 | 9 | :storage => :file_system, |
6 | 10 | :max_size => 500.kilobytes, |
... | ... | @@ -11,5 +15,5 @@ class Image < ActiveRecord::Base |
11 | 15 | :minor => '50x50', |
12 | 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 | 19 | end | ... | ... |
app/views/profile_editor/edit.rhtml
... | ... | @@ -2,6 +2,18 @@ |
2 | 2 | |
3 | 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 | 17 | <% labelled_form_for :profile_data, @profile, :html => { :id => 'profile-data', :multipart => true } do |f| %> |
6 | 18 | |
7 | 19 | <%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %> |
... | ... | @@ -9,7 +21,7 @@ |
9 | 21 | <div id="profile_change_picture"> |
10 | 22 | <h2><%= _('Change picture') %></h2> |
11 | 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 | 25 | <% end %> |
14 | 26 | </div> |
15 | 27 | ... | ... |
test/functional/profile_editor_controller_test.rb
... | ... | @@ -667,4 +667,23 @@ class ProfileEditorControllerTest < Test::Unit::TestCase |
667 | 667 | assert_nil Profile.find(profile.id).preferred_domain |
668 | 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 | 689 | end | ... | ... |