##// END OF EJS Templates
Refactor: Decouple failed attachments and the flash messages...
Eric Davis -
r3414:fe1e3ccd1842
parent child
Show More
@@ -301,4 +301,9 class ApplicationController < ActionController::Base
301 def api_request?
301 def api_request?
302 %w(xml json).include? params[:format]
302 %w(xml json).include? params[:format]
303 end
303 end
304
305 # Renders a warning flash if obj has unsaved attachments
306 def render_attachment_warning_if_needed(obj)
307 flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present?
308 end
304 end
309 end
@@ -48,7 +48,7 class DocumentsController < ApplicationController
48 @document = @project.documents.build(params[:document])
48 @document = @project.documents.build(params[:document])
49 if request.post? and @document.save
49 if request.post? and @document.save
50 attachments = Attachment.attach_files(@document, params[:attachments])
50 attachments = Attachment.attach_files(@document, params[:attachments])
51 flash[:warning] = attachments[:flash] if attachments[:flash]
51 render_attachment_warning_if_needed(@document)
52 flash[:notice] = l(:notice_successful_create)
52 flash[:notice] = l(:notice_successful_create)
53 redirect_to :action => 'index', :project_id => @project
53 redirect_to :action => 'index', :project_id => @project
54 end
54 end
@@ -69,7 +69,7 class DocumentsController < ApplicationController
69
69
70 def add_attachment
70 def add_attachment
71 attachments = Attachment.attach_files(@document, params[:attachments])
71 attachments = Attachment.attach_files(@document, params[:attachments])
72 flash[:warning] = attachments[:flash] if attachments[:flash]
72 render_attachment_warning_if_needed(@document)
73
73
74 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
74 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
75 redirect_to :action => 'show', :id => @document
75 redirect_to :action => 'show', :id => @document
@@ -159,7 +159,7 class IssuesController < ApplicationController
159 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
159 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
160 if @issue.save
160 if @issue.save
161 attachments = Attachment.attach_files(@issue, params[:attachments])
161 attachments = Attachment.attach_files(@issue, params[:attachments])
162 flash[:warning] = attachments[:flash] if attachments[:flash]
162 render_attachment_warning_if_needed(@issue)
163 flash[:notice] = l(:notice_successful_create)
163 flash[:notice] = l(:notice_successful_create)
164 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
164 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
165 respond_to do |format|
165 respond_to do |format|
@@ -571,7 +571,8 private
571
571
572 if @issue.valid?
572 if @issue.valid?
573 attachments = Attachment.attach_files(@issue, params[:attachments])
573 attachments = Attachment.attach_files(@issue, params[:attachments])
574 flash[:warning] = attachments[:flash] if attachments[:flash]
574 render_attachment_warning_if_needed(@issue)
575
575 attachments[:files].each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
576 attachments[:files].each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
576 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal})
577 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal})
577 if @issue.save
578 if @issue.save
@@ -63,7 +63,7 class MessagesController < ApplicationController
63 if request.post? && @message.save
63 if request.post? && @message.save
64 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
64 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
65 attachments = Attachment.attach_files(@message, params[:attachments])
65 attachments = Attachment.attach_files(@message, params[:attachments])
66 flash[:warning] = attachments[:flash] if attachments[:flash]
66 render_attachment_warning_if_needed(@message)
67 redirect_to :action => 'show', :id => @message
67 redirect_to :action => 'show', :id => @message
68 end
68 end
69 end
69 end
@@ -77,7 +77,7 class MessagesController < ApplicationController
77 if !@reply.new_record?
77 if !@reply.new_record?
78 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
78 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
79 attachments = Attachment.attach_files(@reply, params[:attachments])
79 attachments = Attachment.attach_files(@reply, params[:attachments])
80 flash[:warning] = attachments[:flash] if attachments[:flash]
80 render_attachment_warning_if_needed(@reply)
81 end
81 end
82 redirect_to :action => 'show', :id => @topic, :r => @reply
82 redirect_to :action => 'show', :id => @topic, :r => @reply
83 end
83 end
@@ -91,7 +91,7 class MessagesController < ApplicationController
91 end
91 end
92 if request.post? && @message.update_attributes(params[:message])
92 if request.post? && @message.update_attributes(params[:message])
93 attachments = Attachment.attach_files(@message, params[:attachments])
93 attachments = Attachment.attach_files(@message, params[:attachments])
94 flash[:warning] = attachments[:flash] if attachments[:flash]
94 render_attachment_warning_if_needed(@message)
95 flash[:notice] = l(:notice_successful_update)
95 flash[:notice] = l(:notice_successful_update)
96 @message.reload
96 @message.reload
97 redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
97 redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
@@ -304,7 +304,7 class ProjectsController < ApplicationController
304 if request.post?
304 if request.post?
305 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
305 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
306 attachments = Attachment.attach_files(container, params[:attachments])
306 attachments = Attachment.attach_files(container, params[:attachments])
307 flash[:warning] = attachments[:flash] if attachments[:flash]
307 render_attachment_warning_if_needed(container)
308
308
309 if !attachments.empty? && Setting.notified_events.include?('file_added')
309 if !attachments.empty? && Setting.notified_events.include?('file_added')
310 Mailer.deliver_attachments_added(attachments[:files])
310 Mailer.deliver_attachments_added(attachments[:files])
@@ -77,7 +77,7 class WikiController < ApplicationController
77 else
77 else
78 if !@page.new_record? && @content.text == params[:content][:text]
78 if !@page.new_record? && @content.text == params[:content][:text]
79 attachments = Attachment.attach_files(@page, params[:attachments])
79 attachments = Attachment.attach_files(@page, params[:attachments])
80 flash[:warning] = attachments[:flash] if attachments[:flash]
80 render_attachment_warning_if_needed(@page)
81 # don't save if text wasn't changed
81 # don't save if text wasn't changed
82 redirect_to :action => 'index', :id => @project, :page => @page.title
82 redirect_to :action => 'index', :id => @project, :page => @page.title
83 return
83 return
@@ -89,7 +89,7 class WikiController < ApplicationController
89 # if page is new @page.save will also save content, but not if page isn't a new record
89 # if page is new @page.save will also save content, but not if page isn't a new record
90 if (@page.new_record? ? @page.save : @content.save)
90 if (@page.new_record? ? @page.save : @content.save)
91 attachments = Attachment.attach_files(@page, params[:attachments])
91 attachments = Attachment.attach_files(@page, params[:attachments])
92 flash[:warning] = attachments[:flash] if attachments[:flash]
92 render_attachment_warning_if_needed(@page)
93 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
93 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
94 redirect_to :action => 'index', :id => @project, :page => @page.title
94 redirect_to :action => 'index', :id => @project, :page => @page.title
95 end
95 end
@@ -214,7 +214,7 class WikiController < ApplicationController
214 def add_attachment
214 def add_attachment
215 return render_403 unless editable?
215 return render_403 unless editable?
216 attachments = Attachment.attach_files(@page, params[:attachments])
216 attachments = Attachment.attach_files(@page, params[:attachments])
217 flash[:warning] = attachments[:flash] if attachments[:flash]
217 render_attachment_warning_if_needed(@page)
218 redirect_to :action => 'index', :page => @page.title
218 redirect_to :action => 'index', :page => @page.title
219 end
219 end
220
220
@@ -139,11 +139,9 class Attachment < ActiveRecord::Base
139 # Returns a Hash of the results:
139 # Returns a Hash of the results:
140 # :files => array of the attached files
140 # :files => array of the attached files
141 # :unsaved => array of the files that could not be attached
141 # :unsaved => array of the files that could not be attached
142 # :flash => warning message
143 def self.attach_files(obj, attachments)
142 def self.attach_files(obj, attachments)
144 attached = []
143 attached = []
145 unsaved = []
144 unsaved = []
146 flash = nil
147 if attachments && attachments.is_a?(Hash)
145 if attachments && attachments.is_a?(Hash)
148 attachments.each_value do |attachment|
146 attachments.each_value do |attachment|
149 file = attachment['file']
147 file = attachment['file']
@@ -152,13 +150,10 class Attachment < ActiveRecord::Base
152 :file => file,
150 :file => file,
153 :description => attachment['description'].to_s.strip,
151 :description => attachment['description'].to_s.strip,
154 :author => User.current)
152 :author => User.current)
155 a.new_record? ? (unsaved << a) : (attached << a)
153 a.new_record? ? (obj.unsaved_attachments << a) : (attached << a)
156 end
157 if unsaved.any?
158 flash = l(:warning_attachments_not_saved, unsaved.size)
159 end
154 end
160 end
155 end
161 {:files => attached, :flash => flash, :unsaved => unsaved}
156 {:files => attached, :unsaved => obj.unsaved_attachments}
162 end
157 end
163
158
164 private
159 private
@@ -32,6 +32,8 module Redmine
32 has_many :attachments, options.merge(:as => :container,
32 has_many :attachments, options.merge(:as => :container,
33 :order => "#{Attachment.table_name}.created_on",
33 :order => "#{Attachment.table_name}.created_on",
34 :dependent => :destroy)
34 :dependent => :destroy)
35 attr_accessor :unsaved_attachments
36 after_initialize :initialize_unsaved_attachments
35 send :include, Redmine::Acts::Attachable::InstanceMethods
37 send :include, Redmine::Acts::Attachable::InstanceMethods
36 end
38 end
37 end
39 end
@@ -48,7 +50,11 module Redmine
48 def attachments_deletable?(user=User.current)
50 def attachments_deletable?(user=User.current)
49 user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
51 user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
50 end
52 end
51
53
54 def initialize_unsaved_attachments
55 @unsaved_attachments ||= []
56 end
57
52 module ClassMethods
58 module ClassMethods
53 end
59 end
54 end
60 end
General Comments 0
You need to be logged in to leave comments. Login now