From 59b1154f654a11cff9b2d796a607169344676ecd Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Sat, 20 Jun 2009 02:01:34 -0300 Subject: [PATCH] ActionItem1144: validating image when save profile --- app/controllers/my_profile/profile_editor_controller.rb | 17 ++++++++++------- app/models/community.rb | 1 + app/models/enterprise.rb | 3 ++- app/models/image.rb | 2 +- app/models/person.rb | 1 + app/models/profile.rb | 10 ++++++++++ app/models/uploaded_file.rb | 2 +- app/views/profile_editor/edit.rhtml | 12 ------------ test/functional/profile_editor_controller_test.rb | 12 ++++++++++-- test/unit/profile_test.rb | 13 +++++++++++++ vendor/plugins/attachment_fu_validates_attachment/init.rb | 4 ++-- 11 files changed, 51 insertions(+), 26 deletions(-) diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb index 73bd32f..d4a0330 100644 --- a/app/controllers/my_profile/profile_editor_controller.rb +++ b/app/controllers/my_profile/profile_editor_controller.rb @@ -13,14 +13,17 @@ class ProfileEditorController < MyProfileController @profile_data = profile @possible_domains = profile.possible_domains if request.post? - if profile.update_attributes(params[:profile_data]) - if profile.image && profile.image.errors.any? - @errors = profile.image.errors - render :action => 'edit' - else - redirect_to :action => 'index' + begin + Profile.transaction do + Image.transaction do + if profile.update_attributes!(params[:profile_data]) + redirect_to :action => 'index' + end end - end + end + rescue + flash[:notice] = _('Cannot update profile') + end end end diff --git a/app/models/community.rb b/app/models/community.rb index d123ab5..a94a442 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -19,6 +19,7 @@ class Community < Organization end def validate + super self.required_fields.each do |field| if self.send(field).blank? self.errors.add(field, _('%{fn} is mandatory')) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c6d0664..d3a0bd1 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -40,9 +40,10 @@ class Enterprise < Organization end def validate + super self.required_fields.each do |field| if self.send(field).blank? - self.errors.add(field, _('%{fn} is mandatory')) + self.errors.add(field, _('%{fn} is mandatory')) end end end diff --git a/app/models/image.rb b/app/models/image.rb index 4a3ff67..46018c6 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -15,5 +15,5 @@ class Image < ActiveRecord::Base :minor => '50x50', :icon => '20x20!' } - validates_attachment :size => N_("The file you uploaded was larger than the maximum size of %s") % Image.max_size.to_humanreadable + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of %s") % Image.max_size.to_humanreadable end diff --git a/app/models/person.rb b/app/models/person.rb index 7ca25ad..dd1d8ba 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -73,6 +73,7 @@ class Person < Profile end def validate + super self.required_fields.each do |field| if self.send(field).blank? unless (field == 'custom_area_of_study' && self.area_of_study != 'Others') || (field == 'custom_formation' && self.formation != 'Others') diff --git a/app/models/profile.rb b/app/models/profile.rb index 520c104..3479fde 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -624,4 +624,14 @@ class Profile < ActiveRecord::Base [] end + def validate + unless self.image.nil? + self.image.valid? + self.image.errors.delete(:empty) # dont validate here if exists uploaded data + self.image.errors.each do |attr,msg| + self.errors.add(attr, msg) + end + end + end + end diff --git a/app/models/uploaded_file.rb b/app/models/uploaded_file.rb index a17ec93..7cf2af2 100644 --- a/app/models/uploaded_file.rb +++ b/app/models/uploaded_file.rb @@ -18,7 +18,7 @@ class UploadedFile < Article UploadedFile.attachment_options[:max_size] end - validates_attachment :size => N_("The file you uploaded was larger than the maximum size of %s") % UploadedFile.max_size.to_humanreadable + validates_attachment :size => N_("%{fn} of uploaded file was larger than the maximum size of %s") % UploadedFile.max_size.to_humanreadable def icon_name self.image? ? public_filename(:icon) : self.content_type.gsub('/', '-') diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml index 3024e5c..5ce7d5d 100644 --- a/app/views/profile_editor/edit.rhtml +++ b/app/views/profile_editor/edit.rhtml @@ -2,18 +2,6 @@ <%= 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:') %>

- -
-<% 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 } %> diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index b835e02..8f20f2b 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -238,14 +238,14 @@ class ProfileEditorControllerTest < Test::Unit::TestCase should 'back when update community info fail' do org = Community.create!(:name => 'test org', :identifier => 'testorg', :contact_person => 'my contact', :environment => Environment.default) - Community.any_instance.stubs(:update_attributes).returns(false) + Community.any_instance.stubs(:update_attributes!).returns(false) post :edit, :profile => 'testorg' assert_template 'edit' end should 'back when update enterprise info fail' do org = Enterprise.create!(:name => 'test org', :identifier => 'testorg', :contact_person => 'my contact', :environment => Environment.default) - Enterprise.any_instance.stubs(:update_attributes).returns(false) + Enterprise.any_instance.stubs(:update_attributes!).returns(false) post :edit, :profile => 'testorg' assert_template 'edit' end @@ -667,6 +667,14 @@ class ProfileEditorControllerTest < Test::Unit::TestCase assert_nil Profile.find(profile.id).preferred_domain end + should 'not be able to upload an image bigger than max size' do + Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024) + person = create_user('test_profile').person + assert_nil person.image + post :edit, :profile => person.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } } + assert_nil person.image + 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') } } diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index b087b61..0c5a861 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1346,6 +1346,19 @@ class ProfileTest < Test::Unit::TestCase assert !profile.folders.include?(child) end + should 'validates profile image when save' do + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) + profile.image.expects(:valid?) + assert profile.valid? + end + + should 'profile is invalid when image not valid' do + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) + profile.image.expects(:valid?) + profile.image.errors.add(:size, "fake error") + assert !profile.valid? + end + private def assert_invalid_identifier(id) diff --git a/vendor/plugins/attachment_fu_validates_attachment/init.rb b/vendor/plugins/attachment_fu_validates_attachment/init.rb index 0db131d..1e8a949 100644 --- a/vendor/plugins/attachment_fu_validates_attachment/init.rb +++ b/vendor/plugins/attachment_fu_validates_attachment/init.rb @@ -5,12 +5,12 @@ Technoweenie::AttachmentFu::InstanceMethods.module_eval do protected def attachment_valid? if self.filename.nil? - errors.add_to_base attachment_validation_options[:empty] + errors.add :empty, attachment_validation_options[:empty] return end [:content_type, :size].each do |option| if attachment_validation_options[option] && attachment_options[option] && !attachment_options[option].include?(self.send(option)) - errors.add_to_base attachment_validation_options[option] + errors.add option, attachment_validation_options[option] end end end -- libgit2 0.21.2