##// END OF EJS Templates
Added ApplicationController#attach_files as a common method to attach files in all actions....
Jean-Philippe Lang -
r977:86319feef236
parent child
Show More
@@ -145,6 +145,19 class ApplicationController < ActionController::Base
145 145 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
146 146 end
147 147
148 # TODO: move to model
149 def attach_files(obj, files)
150 attachments = []
151 if files && files.is_a?(Array)
152 files.each do |file|
153 next unless file.size > 0
154 a = Attachment.create(:container => obj, :file => file, :author => User.current)
155 attachments << a unless a.new_record?
156 end
157 end
158 attachments
159 end
160
148 161 # qvalues http header parser
149 162 # code taken from webrick
150 163 def parse_qvalues(value)
@@ -45,14 +45,8 class DocumentsController < ApplicationController
45 45 end
46 46
47 47 def add_attachment
48 # Save the attachments
49 @attachments = []
50 params[:attachments].each { |file|
51 next unless file.size > 0
52 a = Attachment.create(:container => @document, :file => file, :author => User.current)
53 @attachments << a unless a.new_record?
54 } if params[:attachments] and params[:attachments].is_a? Array
55 Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('document_added')
48 attachments = attach_files(@document, params[:attachments])
49 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
56 50 redirect_to :action => 'show', :id => @document
57 51 end
58 52
@@ -116,13 +116,8 class IssuesController < ApplicationController
116 116
117 117 def add_note
118 118 journal = @issue.init_journal(User.current, params[:notes])
119 params[:attachments].each { |file|
120 next unless file.size > 0
121 a = Attachment.create(:container => @issue, :file => file, :author => User.current)
122 journal.details << JournalDetail.new(:property => 'attachment',
123 :prop_key => a.id,
124 :value => a.filename) unless a.new_record?
125 } if params[:attachments] and params[:attachments].is_a? Array
119 attachments = attach_files(@issue, params[:attachments])
120 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
126 121 if journal.save
127 122 flash[:notice] = l(:notice_successful_update)
128 123 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
@@ -140,15 +135,8 class IssuesController < ApplicationController
140 135 journal = @issue.init_journal(User.current, params[:notes])
141 136 @issue.status = @new_status
142 137 if @issue.update_attributes(params[:issue])
143 # Save attachments
144 params[:attachments].each { |file|
145 next unless file.size > 0
146 a = Attachment.create(:container => @issue, :file => file, :author => User.current)
147 journal.details << JournalDetail.new(:property => 'attachment',
148 :prop_key => a.id,
149 :value => a.filename) unless a.new_record?
150 } if params[:attachments] and params[:attachments].is_a? Array
151
138 attachments = attach_files(@issue, params[:attachments])
139 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
152 140 # Log time
153 141 if current_role.allowed_to?(:log_time)
154 142 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
@@ -42,9 +42,7 class MessagesController < ApplicationController
42 42 @message.sticky = params[:message]['sticky']
43 43 end
44 44 if request.post? && @message.save
45 params[:attachments].each { |file|
46 Attachment.create(:container => @message, :file => file, :author => User.current) if file.size > 0
47 } if params[:attachments] and params[:attachments].is_a? Array
45 attach_files(@message, params[:attachments])
48 46 redirect_to :action => 'show', :id => @message
49 47 end
50 48 end
@@ -56,9 +54,7 class MessagesController < ApplicationController
56 54 @reply.board = @board
57 55 @topic.children << @reply
58 56 if !@reply.new_record?
59 params[:attachments].each { |file|
60 Attachment.create(:container => @reply, :file => file, :author => User.current) if file.size > 0
61 } if params[:attachments] and params[:attachments].is_a? Array
57 attach_files(@reply, params[:attachments])
62 58 end
63 59 redirect_to :action => 'show', :id => @topic
64 60 end
@@ -70,9 +66,7 class MessagesController < ApplicationController
70 66 @message.sticky = params[:message]['sticky']
71 67 end
72 68 if request.post? && @message.update_attributes(params[:message])
73 params[:attachments].each { |file|
74 Attachment.create(:container => @message, :file => file, :author => User.current) if file.size > 0
75 } if params[:attachments] and params[:attachments].is_a? Array
69 attach_files(@message, params[:attachments])
76 70 flash[:notice] = l(:notice_successful_update)
77 71 redirect_to :action => 'show', :id => @topic
78 72 end
@@ -181,10 +181,7 class ProjectsController < ApplicationController
181 181 def add_document
182 182 @document = @project.documents.build(params[:document])
183 183 if request.post? and @document.save
184 # Save the attachments
185 params[:attachments].each { |a|
186 Attachment.create(:container => @document, :file => a, :author => User.current) unless a.size == 0
187 } if params[:attachments] and params[:attachments].is_a? Array
184 attach_files(@document, params[:attachments])
188 185 flash[:notice] = l(:notice_successful_create)
189 186 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
190 187 redirect_to :action => 'list_documents', :id => @project
@@ -237,10 +234,7 class ProjectsController < ApplicationController
237 234 @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]) }
238 235 @issue.custom_values = @custom_values
239 236 if @issue.save
240 if params[:attachments] && params[:attachments].is_a?(Array)
241 # Save attachments
242 params[:attachments].each {|a| Attachment.create(:container => @issue, :file => a, :author => User.current) unless a.size == 0}
243 end
237 attach_files(@issue, params[:attachments])
244 238 flash[:notice] = l(:notice_successful_create)
245 239 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
246 240 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
@@ -345,14 +339,8 class ProjectsController < ApplicationController
345 339 def add_file
346 340 if request.post?
347 341 @version = @project.versions.find_by_id(params[:version_id])
348 # Save the attachments
349 @attachments = []
350 params[:attachments].each { |file|
351 next unless file.size > 0
352 a = Attachment.create(:container => @version, :file => file, :author => User.current)
353 @attachments << a unless a.new_record?
354 } if params[:attachments] and params[:attachments].is_a? Array
355 Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('file_added')
342 attachments = attach_files(@issue, params[:attachments])
343 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added')
356 344 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
357 345 end
358 346 @versions = @project.versions.sort
@@ -154,11 +154,7 class WikiController < ApplicationController
154 154
155 155 def add_attachment
156 156 @page = @wiki.find_page(params[:page])
157 # Save the attachments
158 params[:attachments].each { |file|
159 next unless file.size > 0
160 a = Attachment.create(:container => @page, :file => file, :author => User.current)
161 } if params[:attachments] and params[:attachments].is_a? Array
157 attach_files(@page, params[:attachments])
162 158 redirect_to :action => 'index', :page => @page.title
163 159 end
164 160
General Comments 0
You need to be logged in to leave comments. Login now