diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c2fd2c2f5dc28da36f05554377187cb50274fcd0..58d87e7ed0a2a555df6e87fa3bbaeb851c730247 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -301,4 +301,9 @@ class ApplicationController < ActionController::Base def api_request? %w(xml json).include? params[:format] end + + # Renders a warning flash if obj has unsaved attachments + def render_attachment_warning_if_needed(obj) + flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? + end end diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 135ae2198960c872c135d2d62c9374f495727bb7..526bf8123de545a7412126c3348d82f5ea727132 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -48,7 +48,7 @@ class DocumentsController < ApplicationController @document = @project.documents.build(params[:document]) if request.post? and @document.save attachments = Attachment.attach_files(@document, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@document) flash[:notice] = l(:notice_successful_create) redirect_to :action => 'index', :project_id => @project end @@ -69,7 +69,7 @@ class DocumentsController < ApplicationController def add_attachment attachments = Attachment.attach_files(@document, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@document) Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added') redirect_to :action => 'show', :id => @document diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 1bbd9b739577c99f89eba516fbe6f200a8374b09..aa28ebe81854102d320bdba20153965e0826e44c 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -159,7 +159,7 @@ class IssuesController < ApplicationController call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) if @issue.save attachments = Attachment.attach_files(@issue, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@issue) flash[:notice] = l(:notice_successful_create) call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) respond_to do |format| @@ -571,7 +571,8 @@ private if @issue.valid? attachments = Attachment.attach_files(@issue, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@issue) + attachments[:files].each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal}) if @issue.save diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 70226a7114bca459707857f49cee1a7c62569efd..5ad8ea58deabc93180237d295002177999a552dd 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -63,7 +63,7 @@ class MessagesController < ApplicationController if request.post? && @message.save call_hook(:controller_messages_new_after_save, { :params => params, :message => @message}) attachments = Attachment.attach_files(@message, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@message) redirect_to :action => 'show', :id => @message end end @@ -77,7 +77,7 @@ class MessagesController < ApplicationController if !@reply.new_record? call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply}) attachments = Attachment.attach_files(@reply, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@reply) end redirect_to :action => 'show', :id => @topic, :r => @reply end @@ -91,7 +91,7 @@ class MessagesController < ApplicationController end if request.post? && @message.update_attributes(params[:message]) attachments = Attachment.attach_files(@message, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@message) flash[:notice] = l(:notice_successful_update) @message.reload redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 58790c28f22972d7d4c5bb61f7a0d7a6bfb0383d..a8ab59bd8970841f29da76614472db9cd959ba43 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -304,7 +304,7 @@ class ProjectsController < ApplicationController if request.post? container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id])) attachments = Attachment.attach_files(container, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(container) if !attachments.empty? && Setting.notified_events.include?('file_added') Mailer.deliver_attachments_added(attachments[:files]) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index e0e159a006989726b7ea3a1b34b41f9a481ba6b0..60b59ff5f00b25ed19c47f7d68ae92318b882185 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -77,7 +77,7 @@ class WikiController < ApplicationController else if !@page.new_record? && @content.text == params[:content][:text] attachments = Attachment.attach_files(@page, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@page) # don't save if text wasn't changed redirect_to :action => 'index', :id => @project, :page => @page.title return @@ -89,7 +89,7 @@ class WikiController < ApplicationController # if page is new @page.save will also save content, but not if page isn't a new record if (@page.new_record? ? @page.save : @content.save) attachments = Attachment.attach_files(@page, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@page) call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) redirect_to :action => 'index', :id => @project, :page => @page.title end @@ -214,7 +214,7 @@ class WikiController < ApplicationController def add_attachment return render_403 unless editable? attachments = Attachment.attach_files(@page, params[:attachments]) - flash[:warning] = attachments[:flash] if attachments[:flash] + render_attachment_warning_if_needed(@page) redirect_to :action => 'index', :page => @page.title end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 221618c6a85bac04a33dbf9587797969806c93d3..c752e0e5250ed785efcf8807182e3552529281af 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -139,11 +139,9 @@ class Attachment < ActiveRecord::Base # Returns a Hash of the results: # :files => array of the attached files # :unsaved => array of the files that could not be attached - # :flash => warning message def self.attach_files(obj, attachments) attached = [] unsaved = [] - flash = nil if attachments && attachments.is_a?(Hash) attachments.each_value do |attachment| file = attachment['file'] @@ -152,13 +150,10 @@ class Attachment < ActiveRecord::Base :file => file, :description => attachment['description'].to_s.strip, :author => User.current) - a.new_record? ? (unsaved << a) : (attached << a) - end - if unsaved.any? - flash = l(:warning_attachments_not_saved, unsaved.size) + a.new_record? ? (obj.unsaved_attachments << a) : (attached << a) end end - {:files => attached, :flash => flash, :unsaved => unsaved} + {:files => attached, :unsaved => obj.unsaved_attachments} end private diff --git a/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb b/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb index 78d42c2152a90ed9f1d2ed853cc05ace6717998d..e5acdc4997e74f76d3be1781603488703c538c65 100644 --- a/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb +++ b/vendor/plugins/acts_as_attachable/lib/acts_as_attachable.rb @@ -32,6 +32,8 @@ module Redmine has_many :attachments, options.merge(:as => :container, :order => "#{Attachment.table_name}.created_on", :dependent => :destroy) + attr_accessor :unsaved_attachments + after_initialize :initialize_unsaved_attachments send :include, Redmine::Acts::Attachable::InstanceMethods end end @@ -48,7 +50,11 @@ module Redmine def attachments_deletable?(user=User.current) user.allowed_to?(self.class.attachable_options[:delete_permission], self.project) end - + + def initialize_unsaved_attachments + @unsaved_attachments ||= [] + end + module ClassMethods end end