]> source.dussan.org Git - redmine.git/commitdiff
Added ApplicationController#attach_files as a common method to attach files in all...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 14 Dec 2007 17:33:05 +0000 (17:33 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 14 Dec 2007 17:33:05 +0000 (17:33 +0000)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@990 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/application.rb
app/controllers/documents_controller.rb
app/controllers/issues_controller.rb
app/controllers/messages_controller.rb
app/controllers/projects_controller.rb
app/controllers/wiki_controller.rb

index e186455a3091f3de4233aebc4a7b30ec10d48ed6..ad86b6b33fa0ac5dbe5419a9b661be6838527367 100644 (file)
@@ -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
index 94532b65be8e8a40c496e23d0ec8ed314e9a5799..104cca10cd5fe08cc633cfb0595adee6a6632c57 100644 (file)
@@ -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
   
index 901e1432b499caab2292974efed5f06cf228e53b..78bcf76a7d45760c107bd6c30117b7d7ae552750 100644 (file)
@@ -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)
index 46c9adadd77bfdfdbaa0ffd53643d0a3b4f0e71c..8078abf71a126e6b487b089ab45aae8c9d03e3cb 100644 (file)
@@ -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
index c4d1b53fc7998ebe3d3d42230b86ecae737ac3bd..7b1e4ef3d9f161f89ff54acbb07743577266cf7e 100644 (file)
@@ -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
index 37a36bf5683e0f32e484866c6002606309cc69d2..2ee22167d36be53563b218bd5b70cc8915c32ecb 100644 (file)
@@ -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