diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb
index badf707..73bd32f 100644
--- a/app/controllers/my_profile/profile_editor_controller.rb
+++ b/app/controllers/my_profile/profile_editor_controller.rb
@@ -14,7 +14,12 @@ class ProfileEditorController < MyProfileController
@possible_domains = profile.possible_domains
if request.post?
if profile.update_attributes(params[:profile_data])
- redirect_to :action => 'index'
+ if profile.image && profile.image.errors.any?
+ @errors = profile.image.errors
+ render :action => 'edit'
+ else
+ redirect_to :action => 'index'
+ end
end
end
end
diff --git a/app/models/image.rb b/app/models/image.rb
index 6c4aa5e..4a3ff67 100644
--- a/app/models/image.rb
+++ b/app/models/image.rb
@@ -1,6 +1,10 @@
class Image < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
-
+
+ def self.max_size
+ Image.attachment_options[:max_size]
+ end
+
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
@@ -11,5 +15,5 @@ class Image < ActiveRecord::Base
:minor => '50x50',
:icon => '20x20!' }
- validates_as_attachment
+ validates_attachment :size => N_("The file you uploaded was larger than the maximum size of %s") % Image.max_size.to_humanreadable
end
diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml
index 4a23ae0..3024e5c 100644
--- a/app/views/profile_editor/edit.rhtml
+++ b/app/views/profile_editor/edit.rhtml
@@ -2,6 +2,18 @@
<%= error_messages_for :profile %>
+<% if @errors %>
+
+
<%= n_('This file couldn\'t be saved', 'These %{num} files couldn\'t be saved', @errors.size) % { :num => @errors.size } %>
+
<%= _('There were problems with the following files:') %>
+
+ <% for msg in profile.image.errors.full_messages %>
+ - <%= profile.image.filename %> : <%= msg %>
+ <% end %>
+
+
+<% end %>
+
<% labelled_form_for :profile_data, @profile, :html => { :id => 'profile-data', :multipart => true } do |f| %>
<%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %>
@@ -9,7 +21,7 @@
<%= _('Change picture') %>
<% f.fields_for :image_builder, @profile.image do |i| %>
- <%= file_field_or_thumbnail(_('Image:'), @profile.image, i) %>
+ <%= file_field_or_thumbnail(_('Image:'), @profile.image, i) %><%= _("(max size %s)")% Image.max_size.to_humanreadable %>
<% end %>
diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb
index 7dc1269..b835e02 100644
--- a/test/functional/profile_editor_controller_test.rb
+++ b/test/functional/profile_editor_controller_test.rb
@@ -667,4 +667,23 @@ class ProfileEditorControllerTest < Test::Unit::TestCase
assert_nil Profile.find(profile.id).preferred_domain
end
+ should 'display error message when image has more than max size' do
+ Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
+ post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
+ assert_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
+ end
+
+ should 'not display error message when image has less than max size' do
+ Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] - 1024)
+ post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
+ assert_no_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
+ end
+
+ should 'not redirect when some file has errors' do
+ Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
+ post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
+ assert_response :success
+ assert_template 'edit'
+ end
+
end
--
libgit2 0.21.2