From: Jean-Philippe Lang Date: Fri, 14 Dec 2007 17:33:05 +0000 (+0000) Subject: Added ApplicationController#attach_files as a common method to attach files in all... X-Git-Tag: 0.6.2~15 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=86319feef23618f59da13b450a085e40019a43e0;p=redmine.git Added ApplicationController#attach_files as a common method to attach files in all actions. git-svn-id: http://redmine.rubyforge.org/svn/trunk@990 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/application.rb b/app/controllers/application.rb index e186455a3..ad86b6b33 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -144,6 +144,19 @@ class ApplicationController < ActionController::Base def accept_key_auth_actions self.class.read_inheritable_attribute('accept_key_auth_actions') || [] end + + # TODO: move to model + def attach_files(obj, files) + attachments = [] + if files && files.is_a?(Array) + files.each do |file| + next unless file.size > 0 + a = Attachment.create(:container => obj, :file => file, :author => User.current) + attachments << a unless a.new_record? + end + end + attachments + end # qvalues http header parser # code taken from webrick diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 94532b65b..104cca10c 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -45,14 +45,8 @@ class DocumentsController < ApplicationController end def add_attachment - # Save the attachments - @attachments = [] - params[:attachments].each { |file| - next unless file.size > 0 - a = Attachment.create(:container => @document, :file => file, :author => User.current) - @attachments << a unless a.new_record? - } if params[:attachments] and params[:attachments].is_a? Array - Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('document_added') + attachments = attach_files(@document, params[:attachments]) + Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added') redirect_to :action => 'show', :id => @document end diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 901e1432b..78bcf76a7 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -116,13 +116,8 @@ class IssuesController < ApplicationController def add_note journal = @issue.init_journal(User.current, params[:notes]) - params[:attachments].each { |file| - next unless file.size > 0 - a = Attachment.create(:container => @issue, :file => file, :author => User.current) - journal.details << JournalDetail.new(:property => 'attachment', - :prop_key => a.id, - :value => a.filename) unless a.new_record? - } if params[:attachments] and params[:attachments].is_a? Array + attachments = attach_files(@issue, params[:attachments]) + attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} if journal.save flash[:notice] = l(:notice_successful_update) Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') @@ -140,15 +135,8 @@ class IssuesController < ApplicationController journal = @issue.init_journal(User.current, params[:notes]) @issue.status = @new_status if @issue.update_attributes(params[:issue]) - # Save attachments - params[:attachments].each { |file| - next unless file.size > 0 - a = Attachment.create(:container => @issue, :file => file, :author => User.current) - journal.details << JournalDetail.new(:property => 'attachment', - :prop_key => a.id, - :value => a.filename) unless a.new_record? - } if params[:attachments] and params[:attachments].is_a? Array - + attachments = attach_files(@issue, params[:attachments]) + attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} # Log time if current_role.allowed_to?(:log_time) @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 46c9adadd..8078abf71 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -42,9 +42,7 @@ class MessagesController < ApplicationController @message.sticky = params[:message]['sticky'] end if request.post? && @message.save - params[:attachments].each { |file| - Attachment.create(:container => @message, :file => file, :author => User.current) if file.size > 0 - } if params[:attachments] and params[:attachments].is_a? Array + attach_files(@message, params[:attachments]) redirect_to :action => 'show', :id => @message end end @@ -56,9 +54,7 @@ class MessagesController < ApplicationController @reply.board = @board @topic.children << @reply if !@reply.new_record? - params[:attachments].each { |file| - Attachment.create(:container => @reply, :file => file, :author => User.current) if file.size > 0 - } if params[:attachments] and params[:attachments].is_a? Array + attach_files(@reply, params[:attachments]) end redirect_to :action => 'show', :id => @topic end @@ -70,9 +66,7 @@ class MessagesController < ApplicationController @message.sticky = params[:message]['sticky'] end if request.post? && @message.update_attributes(params[:message]) - params[:attachments].each { |file| - Attachment.create(:container => @message, :file => file, :author => User.current) if file.size > 0 - } if params[:attachments] and params[:attachments].is_a? Array + attach_files(@message, params[:attachments]) flash[:notice] = l(:notice_successful_update) redirect_to :action => 'show', :id => @topic end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index c4d1b53fc..7b1e4ef3d 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -181,10 +181,7 @@ class ProjectsController < ApplicationController def add_document @document = @project.documents.build(params[:document]) if request.post? and @document.save - # Save the attachments - params[:attachments].each { |a| - Attachment.create(:container => @document, :file => a, :author => User.current) unless a.size == 0 - } if params[:attachments] and params[:attachments].is_a? Array + attach_files(@document, params[:attachments]) flash[:notice] = l(:notice_successful_create) Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added') redirect_to :action => 'list_documents', :id => @project @@ -237,10 +234,7 @@ class ProjectsController < ApplicationController @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) } @issue.custom_values = @custom_values if @issue.save - if params[:attachments] && params[:attachments].is_a?(Array) - # Save attachments - params[:attachments].each {|a| Attachment.create(:container => @issue, :file => a, :author => User.current) unless a.size == 0} - end + attach_files(@issue, params[:attachments]) flash[:notice] = l(:notice_successful_create) Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added') redirect_to :controller => 'issues', :action => 'index', :project_id => @project @@ -345,14 +339,8 @@ class ProjectsController < ApplicationController def add_file if request.post? @version = @project.versions.find_by_id(params[:version_id]) - # Save the attachments - @attachments = [] - params[:attachments].each { |file| - next unless file.size > 0 - a = Attachment.create(:container => @version, :file => file, :author => User.current) - @attachments << a unless a.new_record? - } if params[:attachments] and params[:attachments].is_a? Array - Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('file_added') + attachments = attach_files(@issue, params[:attachments]) + Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added') redirect_to :controller => 'projects', :action => 'list_files', :id => @project end @versions = @project.versions.sort diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 37a36bf56..2ee22167d 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -154,11 +154,7 @@ class WikiController < ApplicationController def add_attachment @page = @wiki.find_page(params[:page]) - # Save the attachments - params[:attachments].each { |file| - next unless file.size > 0 - a = Attachment.create(:container => @page, :file => file, :author => User.current) - } if params[:attachments] and params[:attachments].is_a? Array + attach_files(@page, params[:attachments]) redirect_to :action => 'index', :page => @page.title end