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