Commit 59b1154f654a11cff9b2d796a607169344676ecd

Authored by Joenio Costa
1 parent 1e28d592

ActionItem1144: validating image when save profile

app/controllers/my_profile/profile_editor_controller.rb
@@ -13,14 +13,17 @@ class ProfileEditorController < MyProfileController @@ -13,14 +13,17 @@ class ProfileEditorController < MyProfileController
13 @profile_data = profile 13 @profile_data = profile
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])  
17 - if profile.image && profile.image.errors.any?  
18 - @errors = profile.image.errors  
19 - render :action => 'edit'  
20 - else  
21 - redirect_to :action => 'index' 16 + begin
  17 + Profile.transaction do
  18 + Image.transaction do
  19 + if profile.update_attributes!(params[:profile_data])
  20 + redirect_to :action => 'index'
  21 + end
22 end 22 end
23 - end 23 + end
  24 + rescue
  25 + flash[:notice] = _('Cannot update profile')
  26 + end
24 end 27 end
25 end 28 end
26 29
app/models/community.rb
@@ -19,6 +19,7 @@ class Community < Organization @@ -19,6 +19,7 @@ class Community < Organization
19 end 19 end
20 20
21 def validate 21 def validate
  22 + super
22 self.required_fields.each do |field| 23 self.required_fields.each do |field|
23 if self.send(field).blank? 24 if self.send(field).blank?
24 self.errors.add(field, _('%{fn} is mandatory')) 25 self.errors.add(field, _('%{fn} is mandatory'))
app/models/enterprise.rb
@@ -40,9 +40,10 @@ class Enterprise < Organization @@ -40,9 +40,10 @@ class Enterprise < Organization
40 end 40 end
41 41
42 def validate 42 def validate
  43 + super
43 self.required_fields.each do |field| 44 self.required_fields.each do |field|
44 if self.send(field).blank? 45 if self.send(field).blank?
45 - self.errors.add(field, _('%{fn} is mandatory')) 46 + self.errors.add(field, _('%{fn} is mandatory'))
46 end 47 end
47 end 48 end
48 end 49 end
app/models/image.rb
@@ -15,5 +15,5 @@ class Image < ActiveRecord::Base @@ -15,5 +15,5 @@ class Image < ActiveRecord::Base
15 :minor => '50x50', 15 :minor => '50x50',
16 :icon => '20x20!' } 16 :icon => '20x20!' }
17 17
18 - validates_attachment :size => N_("The file you uploaded was larger than the maximum size of %s") % Image.max_size.to_humanreadable 18 + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of %s") % Image.max_size.to_humanreadable
19 end 19 end
app/models/person.rb
@@ -73,6 +73,7 @@ class Person < Profile @@ -73,6 +73,7 @@ class Person < Profile
73 end 73 end
74 74
75 def validate 75 def validate
  76 + super
76 self.required_fields.each do |field| 77 self.required_fields.each do |field|
77 if self.send(field).blank? 78 if self.send(field).blank?
78 unless (field == 'custom_area_of_study' && self.area_of_study != 'Others') || (field == 'custom_formation' && self.formation != 'Others') 79 unless (field == 'custom_area_of_study' && self.area_of_study != 'Others') || (field == 'custom_formation' && self.formation != 'Others')
app/models/profile.rb
@@ -624,4 +624,14 @@ class Profile < ActiveRecord::Base @@ -624,4 +624,14 @@ class Profile < ActiveRecord::Base
624 [] 624 []
625 end 625 end
626 626
  627 + def validate
  628 + unless self.image.nil?
  629 + self.image.valid?
  630 + self.image.errors.delete(:empty) # dont validate here if exists uploaded data
  631 + self.image.errors.each do |attr,msg|
  632 + self.errors.add(attr, msg)
  633 + end
  634 + end
  635 + end
  636 +
627 end 637 end
app/models/uploaded_file.rb
@@ -18,7 +18,7 @@ class UploadedFile < Article @@ -18,7 +18,7 @@ class UploadedFile < Article
18 UploadedFile.attachment_options[:max_size] 18 UploadedFile.attachment_options[:max_size]
19 end 19 end
20 20
21 - validates_attachment :size => N_("The file you uploaded was larger than the maximum size of %s") % UploadedFile.max_size.to_humanreadable 21 + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of %s") % UploadedFile.max_size.to_humanreadable
22 22
23 def icon_name 23 def icon_name
24 self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-') 24 self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-')
app/views/profile_editor/edit.rhtml
@@ -2,18 +2,6 @@ @@ -2,18 +2,6 @@
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 -  
17 <% labelled_form_for :profile_data, @profile, :html => { :id => 'profile-data', :multipart => true } do |f| %> 5 <% labelled_form_for :profile_data, @profile, :html => { :id => 'profile-data', :multipart => true } do |f| %>
18 6
19 <%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %> 7 <%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %>
test/functional/profile_editor_controller_test.rb
@@ -238,14 +238,14 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase @@ -238,14 +238,14 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
238 238
239 should 'back when update community info fail' do 239 should 'back when update community info fail' do
240 org = Community.create!(:name => 'test org', :identifier => 'testorg', :contact_person => 'my contact', :environment => Environment.default) 240 org = Community.create!(:name => 'test org', :identifier => 'testorg', :contact_person => 'my contact', :environment => Environment.default)
241 - Community.any_instance.stubs(:update_attributes).returns(false) 241 + Community.any_instance.stubs(:update_attributes!).returns(false)
242 post :edit, :profile => 'testorg' 242 post :edit, :profile => 'testorg'
243 assert_template 'edit' 243 assert_template 'edit'
244 end 244 end
245 245
246 should 'back when update enterprise info fail' do 246 should 'back when update enterprise info fail' do
247 org = Enterprise.create!(:name => 'test org', :identifier => 'testorg', :contact_person => 'my contact', :environment => Environment.default) 247 org = Enterprise.create!(:name => 'test org', :identifier => 'testorg', :contact_person => 'my contact', :environment => Environment.default)
248 - Enterprise.any_instance.stubs(:update_attributes).returns(false) 248 + Enterprise.any_instance.stubs(:update_attributes!).returns(false)
249 post :edit, :profile => 'testorg' 249 post :edit, :profile => 'testorg'
250 assert_template 'edit' 250 assert_template 'edit'
251 end 251 end
@@ -667,6 +667,14 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase @@ -667,6 +667,14 @@ 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 'not be able to upload an image bigger than max size' do
  671 + Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
  672 + person = create_user('test_profile').person
  673 + assert_nil person.image
  674 + post :edit, :profile => person.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
  675 + assert_nil person.image
  676 + end
  677 +
670 should 'display error message when image has more than max size' do 678 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) 679 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') } } 680 post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
test/unit/profile_test.rb
@@ -1346,6 +1346,19 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -1346,6 +1346,19 @@ class ProfileTest &lt; Test::Unit::TestCase
1346 assert !profile.folders.include?(child) 1346 assert !profile.folders.include?(child)
1347 end 1347 end
1348 1348
  1349 + should 'validates profile image when save' do
  1350 + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
  1351 + profile.image.expects(:valid?)
  1352 + assert profile.valid?
  1353 + end
  1354 +
  1355 + should 'profile is invalid when image not valid' do
  1356 + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
  1357 + profile.image.expects(:valid?)
  1358 + profile.image.errors.add(:size, "fake error")
  1359 + assert !profile.valid?
  1360 + end
  1361 +
1349 private 1362 private
1350 1363
1351 def assert_invalid_identifier(id) 1364 def assert_invalid_identifier(id)
vendor/plugins/attachment_fu_validates_attachment/init.rb
@@ -5,12 +5,12 @@ Technoweenie::AttachmentFu::InstanceMethods.module_eval do @@ -5,12 +5,12 @@ Technoweenie::AttachmentFu::InstanceMethods.module_eval do
5 protected 5 protected
6 def attachment_valid? 6 def attachment_valid?
7 if self.filename.nil? 7 if self.filename.nil?
8 - errors.add_to_base attachment_validation_options[:empty] 8 + errors.add :empty, attachment_validation_options[:empty]
9 return 9 return
10 end 10 end
11 [:content_type, :size].each do |option| 11 [:content_type, :size].each do |option|
12 if attachment_validation_options[option] && attachment_options[option] && !attachment_options[option].include?(self.send(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] 13 + errors.add option, attachment_validation_options[option]
14 end 14 end
15 end 15 end
16 end 16 end