##// END OF EJS Templates
Refactor: Moved ApplicationController#attach_files to the Attachment model...
Eric Davis -
r3409:0fd7e2d696cf
parent child
Show More
@@ -257,27 +257,6 class ApplicationController < ActionController::Base
257 257 self.class.read_inheritable_attribute('accept_key_auth_actions') || []
258 258 end
259 259
260 # TODO: move to model
261 def attach_files(obj, attachments)
262 attached = []
263 unsaved = []
264 if attachments && attachments.is_a?(Hash)
265 attachments.each_value do |attachment|
266 file = attachment['file']
267 next unless file && file.size > 0
268 a = Attachment.create(:container => obj,
269 :file => file,
270 :description => attachment['description'].to_s.strip,
271 :author => User.current)
272 a.new_record? ? (unsaved << a) : (attached << a)
273 end
274 if unsaved.any?
275 flash[:warning] = l(:warning_attachments_not_saved, unsaved.size)
276 end
277 end
278 attached
279 end
280
281 260 # Returns the number of objects that should be displayed
282 261 # on the paginated list
283 262 def per_page_option
@@ -47,7 +47,8 class DocumentsController < ApplicationController
47 47 def new
48 48 @document = @project.documents.build(params[:document])
49 49 if request.post? and @document.save
50 attach_files(@document, params[:attachments])
50 attachments = Attachment.attach_files(@document, params[:attachments])
51 flash[:warning] = attachments[:flash] if attachments[:flash]
51 52 flash[:notice] = l(:notice_successful_create)
52 53 redirect_to :action => 'index', :project_id => @project
53 54 end
@@ -67,8 +68,10 class DocumentsController < ApplicationController
67 68 end
68 69
69 70 def add_attachment
70 attachments = attach_files(@document, params[:attachments])
71 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
71 attachments = Attachment.attach_files(@document, params[:attachments])
72 flash[:warning] = attachments[:flash] if attachments[:flash]
73
74 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
72 75 redirect_to :action => 'show', :id => @document
73 76 end
74 77
@@ -158,7 +158,8 class IssuesController < ApplicationController
158 158 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
159 159 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
160 160 if @issue.save
161 attach_files(@issue, params[:attachments])
161 attachments = Attachment.attach_files(@issue, params[:attachments])
162 flash[:warning] = attachments[:flash] if attachments[:flash]
162 163 flash[:notice] = l(:notice_successful_create)
163 164 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
164 165 respond_to do |format|
@@ -561,8 +562,6 private
561 562
562 563 # TODO: Temporary utility method for #update. Should be split off
563 564 # and moved to the Issue model (accepts_nested_attributes_for maybe?)
564 # TODO: move attach_files to the model so this can be moved to the
565 # model also
566 565 def issue_update
567 566 if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, @project)
568 567 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
@@ -571,8 +570,9 private
571 570 end
572 571
573 572 if @issue.valid?
574 attachments = attach_files(@issue, params[:attachments])
575 attachments.each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
573 attachments = Attachment.attach_files(@issue, params[:attachments])
574 flash[:warning] = attachments[:flash] if attachments[:flash]
575 attachments[:files].each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
576 576 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal})
577 577 if @issue.save
578 578 if !@journal.new_record?
@@ -62,7 +62,8 class MessagesController < ApplicationController
62 62 end
63 63 if request.post? && @message.save
64 64 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
65 attach_files(@message, params[:attachments])
65 attachments = Attachment.attach_files(@message, params[:attachments])
66 flash[:warning] = attachments[:flash] if attachments[:flash]
66 67 redirect_to :action => 'show', :id => @message
67 68 end
68 69 end
@@ -75,7 +76,8 class MessagesController < ApplicationController
75 76 @topic.children << @reply
76 77 if !@reply.new_record?
77 78 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
78 attach_files(@reply, params[:attachments])
79 attachments = Attachment.attach_files(@reply, params[:attachments])
80 flash[:warning] = attachments[:flash] if attachments[:flash]
79 81 end
80 82 redirect_to :action => 'show', :id => @topic, :r => @reply
81 83 end
@@ -88,7 +90,8 class MessagesController < ApplicationController
88 90 @message.sticky = params[:message]['sticky']
89 91 end
90 92 if request.post? && @message.update_attributes(params[:message])
91 attach_files(@message, params[:attachments])
93 attachments = Attachment.attach_files(@message, params[:attachments])
94 flash[:warning] = attachments[:flash] if attachments[:flash]
92 95 flash[:notice] = l(:notice_successful_update)
93 96 @message.reload
94 97 redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
@@ -303,9 +303,11 class ProjectsController < ApplicationController
303 303 def add_file
304 304 if request.post?
305 305 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
306 attachments = attach_files(container, params[:attachments])
306 attachments = Attachment.attach_files(container, params[:attachments])
307 flash[:warning] = attachments[:flash] if attachments[:flash]
308
307 309 if !attachments.empty? && Setting.notified_events.include?('file_added')
308 Mailer.deliver_attachments_added(attachments)
310 Mailer.deliver_attachments_added(attachments[:files])
309 311 end
310 312 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
311 313 return
@@ -76,7 +76,8 class WikiController < ApplicationController
76 76 @content.version = @page.content.version
77 77 else
78 78 if !@page.new_record? && @content.text == params[:content][:text]
79 attach_files(@page, params[:attachments])
79 attachments = Attachment.attach_files(@page, params[:attachments])
80 flash[:warning] = attachments[:flash] if attachments[:flash]
80 81 # don't save if text wasn't changed
81 82 redirect_to :action => 'index', :id => @project, :page => @page.title
82 83 return
@@ -87,7 +88,8 class WikiController < ApplicationController
87 88 @content.author = User.current
88 89 # if page is new @page.save will also save content, but not if page isn't a new record
89 90 if (@page.new_record? ? @page.save : @content.save)
90 attach_files(@page, params[:attachments])
91 attachments = Attachment.attach_files(@page, params[:attachments])
92 flash[:warning] = attachments[:flash] if attachments[:flash]
91 93 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
92 94 redirect_to :action => 'index', :id => @project, :page => @page.title
93 95 end
@@ -211,7 +213,8 class WikiController < ApplicationController
211 213
212 214 def add_attachment
213 215 return render_403 unless editable?
214 attach_files(@page, params[:attachments])
216 attachments = Attachment.attach_files(@page, params[:attachments])
217 flash[:warning] = attachments[:flash] if attachments[:flash]
215 218 redirect_to :action => 'index', :page => @page.title
216 219 end
217 220
@@ -133,6 +133,33 class Attachment < ActiveRecord::Base
133 133 def readable?
134 134 File.readable?(diskfile)
135 135 end
136
137 # Bulk attaches a set of files to an object
138 #
139 # Returns a Hash of the results:
140 # :files => array of the attached files
141 # :unsaved => array of the files that could not be attached
142 # :flash => warning message
143 def self.attach_files(obj, attachments)
144 attached = []
145 unsaved = []
146 flash = nil
147 if attachments && attachments.is_a?(Hash)
148 attachments.each_value do |attachment|
149 file = attachment['file']
150 next unless file && file.size > 0
151 a = Attachment.create(:container => obj,
152 :file => file,
153 :description => attachment['description'].to_s.strip,
154 :author => User.current)
155 a.new_record? ? (unsaved << a) : (attached << a)
156 end
157 if unsaved.any?
158 flash = l(:warning_attachments_not_saved, unsaved.size)
159 end
160 end
161 {:files => attached, :flash => flash, :unsaved => unsaved}
162 end
136 163
137 164 private
138 165 def sanitize_filename(value)
General Comments 0
You need to be logged in to leave comments. Login now