Refactor: Moved ApplicationController#attach_files to the Attachment model
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3523 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
81d617cd5b
commit
0fd7e2d696
|
@ -257,27 +257,6 @@ class ApplicationController < ActionController::Base
|
||||||
self.class.read_inheritable_attribute('accept_key_auth_actions') || []
|
self.class.read_inheritable_attribute('accept_key_auth_actions') || []
|
||||||
end
|
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
|
# Returns the number of objects that should be displayed
|
||||||
# on the paginated list
|
# on the paginated list
|
||||||
def per_page_option
|
def per_page_option
|
||||||
|
|
|
@ -47,7 +47,8 @@ class DocumentsController < ApplicationController
|
||||||
def new
|
def new
|
||||||
@document = @project.documents.build(params[:document])
|
@document = @project.documents.build(params[:document])
|
||||||
if request.post? and @document.save
|
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)
|
flash[:notice] = l(:notice_successful_create)
|
||||||
redirect_to :action => 'index', :project_id => @project
|
redirect_to :action => 'index', :project_id => @project
|
||||||
end
|
end
|
||||||
|
@ -67,8 +68,10 @@ class DocumentsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_attachment
|
def add_attachment
|
||||||
attachments = attach_files(@document, params[:attachments])
|
attachments = Attachment.attach_files(@document, params[:attachments])
|
||||||
Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
|
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
|
redirect_to :action => 'show', :id => @document
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -158,7 +158,8 @@ class IssuesController < ApplicationController
|
||||||
@issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
|
@issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
|
||||||
call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
|
call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
|
||||||
if @issue.save
|
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)
|
flash[:notice] = l(:notice_successful_create)
|
||||||
call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
|
call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
@ -561,8 +562,6 @@ private
|
||||||
|
|
||||||
# TODO: Temporary utility method for #update. Should be split off
|
# TODO: Temporary utility method for #update. Should be split off
|
||||||
# and moved to the Issue model (accepts_nested_attributes_for maybe?)
|
# 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
|
def issue_update
|
||||||
if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, @project)
|
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)
|
@time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
|
||||||
|
@ -571,8 +570,9 @@ private
|
||||||
end
|
end
|
||||||
|
|
||||||
if @issue.valid?
|
if @issue.valid?
|
||||||
attachments = attach_files(@issue, params[:attachments])
|
attachments = Attachment.attach_files(@issue, params[:attachments])
|
||||||
attachments.each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
|
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})
|
call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal})
|
||||||
if @issue.save
|
if @issue.save
|
||||||
if !@journal.new_record?
|
if !@journal.new_record?
|
||||||
|
|
|
@ -62,7 +62,8 @@ class MessagesController < ApplicationController
|
||||||
end
|
end
|
||||||
if request.post? && @message.save
|
if request.post? && @message.save
|
||||||
call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
|
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
|
redirect_to :action => 'show', :id => @message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -75,7 +76,8 @@ class MessagesController < ApplicationController
|
||||||
@topic.children << @reply
|
@topic.children << @reply
|
||||||
if !@reply.new_record?
|
if !@reply.new_record?
|
||||||
call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
|
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
|
end
|
||||||
redirect_to :action => 'show', :id => @topic, :r => @reply
|
redirect_to :action => 'show', :id => @topic, :r => @reply
|
||||||
end
|
end
|
||||||
|
@ -88,7 +90,8 @@ class MessagesController < ApplicationController
|
||||||
@message.sticky = params[:message]['sticky']
|
@message.sticky = params[:message]['sticky']
|
||||||
end
|
end
|
||||||
if request.post? && @message.update_attributes(params[:message])
|
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)
|
flash[:notice] = l(:notice_successful_update)
|
||||||
@message.reload
|
@message.reload
|
||||||
redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
|
redirect_to :action => 'show', :board_id => @message.board, :id => @message.root, :r => (@message.parent_id && @message.id)
|
||||||
|
|
|
@ -303,9 +303,11 @@ class ProjectsController < ApplicationController
|
||||||
def add_file
|
def add_file
|
||||||
if request.post?
|
if request.post?
|
||||||
container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
|
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')
|
if !attachments.empty? && Setting.notified_events.include?('file_added')
|
||||||
Mailer.deliver_attachments_added(attachments)
|
Mailer.deliver_attachments_added(attachments[:files])
|
||||||
end
|
end
|
||||||
redirect_to :controller => 'projects', :action => 'list_files', :id => @project
|
redirect_to :controller => 'projects', :action => 'list_files', :id => @project
|
||||||
return
|
return
|
||||||
|
|
|
@ -76,7 +76,8 @@ class WikiController < ApplicationController
|
||||||
@content.version = @page.content.version
|
@content.version = @page.content.version
|
||||||
else
|
else
|
||||||
if !@page.new_record? && @content.text == params[:content][:text]
|
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
|
# don't save if text wasn't changed
|
||||||
redirect_to :action => 'index', :id => @project, :page => @page.title
|
redirect_to :action => 'index', :id => @project, :page => @page.title
|
||||||
return
|
return
|
||||||
|
@ -87,7 +88,8 @@ class WikiController < ApplicationController
|
||||||
@content.author = User.current
|
@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 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)
|
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})
|
call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
|
||||||
redirect_to :action => 'index', :id => @project, :page => @page.title
|
redirect_to :action => 'index', :id => @project, :page => @page.title
|
||||||
end
|
end
|
||||||
|
@ -211,7 +213,8 @@ class WikiController < ApplicationController
|
||||||
|
|
||||||
def add_attachment
|
def add_attachment
|
||||||
return render_403 unless editable?
|
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
|
redirect_to :action => 'index', :page => @page.title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -133,6 +133,33 @@ class Attachment < ActiveRecord::Base
|
||||||
def readable?
|
def readable?
|
||||||
File.readable?(diskfile)
|
File.readable?(diskfile)
|
||||||
end
|
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
|
private
|
||||||
def sanitize_filename(value)
|
def sanitize_filename(value)
|
||||||
|
|
Loading…
Reference in New Issue