self.class.read_inheritable_attribute('accept_key_auth_actions') || []
end
- # TODO: move to model
- def attach_files(obj, attachments)
- attached = []
- unsaved = []
- if attachments && attachments.is_a?(Hash)
- attachments.each_value do |attachment|
- file = attachment['file']
- next unless file && file.size > 0
- a = Attachment.create(:container => obj,
- :file => file,
- :description => attachment['description'].to_s.strip,
- :author => User.current)
- a.new_record? ? (unsaved << a) : (attached << a)
- end
- if unsaved.any?
- flash[:warning] = l(:warning_attachments_not_saved, unsaved.size)
- end
- end
- attached
- end
-
# Returns the number of objects that should be displayed
# on the paginated list
def per_page_option
def new
@document = @project.documents.build(params[:document])
if request.post? and @document.save
- attach_files(@document, params[:attachments])
+ attachments = Attachment.attach_files(@document, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
flash[:notice] = l(:notice_successful_create)
redirect_to :action => 'index', :project_id => @project
end
end
def add_attachment
- attachments = attach_files(@document, params[:attachments])
- Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
+ attachments = Attachment.attach_files(@document, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
+
+ Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
redirect_to :action => 'show', :id => @document
end
@issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
if @issue.save
- attach_files(@issue, params[:attachments])
+ attachments = Attachment.attach_files(@issue, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
flash[:notice] = l(:notice_successful_create)
call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
respond_to do |format|
# TODO: Temporary utility method for #update. Should be split off
# and moved to the Issue model (accepts_nested_attributes_for maybe?)
- # TODO: move attach_files to the model so this can be moved to the
- # model also
def issue_update
if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, @project)
@time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
end
if @issue.valid?
- attachments = attach_files(@issue, params[:attachments])
- attachments.each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
+ attachments = Attachment.attach_files(@issue, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
+ 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
if !@journal.new_record?
end
if request.post? && @message.save
call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
- attach_files(@message, params[:attachments])
+ attachments = Attachment.attach_files(@message, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
redirect_to :action => 'show', :id => @message
end
end
@topic.children << @reply
if !@reply.new_record?
call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
- attach_files(@reply, params[:attachments])
+ attachments = Attachment.attach_files(@reply, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
end
redirect_to :action => 'show', :id => @topic, :r => @reply
end
@message.sticky = params[:message]['sticky']
end
if request.post? && @message.update_attributes(params[:message])
- attach_files(@message, params[:attachments])
+ attachments = Attachment.attach_files(@message, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
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)
def add_file
if request.post?
container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
- attachments = attach_files(container, params[:attachments])
+ attachments = Attachment.attach_files(container, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
+
if !attachments.empty? && Setting.notified_events.include?('file_added')
- Mailer.deliver_attachments_added(attachments)
+ Mailer.deliver_attachments_added(attachments[:files])
end
redirect_to :controller => 'projects', :action => 'list_files', :id => @project
return
@content.version = @page.content.version
else
if !@page.new_record? && @content.text == params[:content][:text]
- attach_files(@page, params[:attachments])
+ attachments = Attachment.attach_files(@page, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
# don't save if text wasn't changed
redirect_to :action => 'index', :id => @project, :page => @page.title
return
@content.author = User.current
# 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)
- attach_files(@page, params[:attachments])
+ attachments = Attachment.attach_files(@page, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
redirect_to :action => 'index', :id => @project, :page => @page.title
end
def add_attachment
return render_403 unless editable?
- attach_files(@page, params[:attachments])
+ attachments = Attachment.attach_files(@page, params[:attachments])
+ flash[:warning] = attachments[:flash] if attachments[:flash]
redirect_to :action => 'index', :page => @page.title
end
def readable?
File.readable?(diskfile)
end
+
+ # Bulk attaches a set of files to an object
+ #
+ # 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']
+ next unless file && file.size > 0
+ a = Attachment.create(:container => obj,
+ :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)
+ end
+ end
+ {:files => attached, :flash => flash, :unsaved => unsaved}
+ end
private
def sanitize_filename(value)