Commit 86319fee authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

Added ApplicationController#attach_files as a common method to attach files in all actions.

git-svn-id: http://redmine.rubyforge.org/svn/trunk@990 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent eb7cbd48
...@@ -145,6 +145,19 @@ class ApplicationController < ActionController::Base ...@@ -145,6 +145,19 @@ 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, files)
attachments = []
if files && files.is_a?(Array)
files.each do |file|
next unless file.size > 0
a = Attachment.create(:container => obj, :file => file, :author => User.current)
attachments << a unless a.new_record?
end
end
attachments
end
# qvalues http header parser # qvalues http header parser
# code taken from webrick # code taken from webrick
def parse_qvalues(value) def parse_qvalues(value)
......
...@@ -45,14 +45,8 @@ class DocumentsController < ApplicationController ...@@ -45,14 +45,8 @@ class DocumentsController < ApplicationController
end end
def add_attachment def add_attachment
# Save the attachments attachments = attach_files(@document, params[:attachments])
@attachments = [] Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
params[:attachments].each { |file|
next unless file.size > 0
a = Attachment.create(:container => @document, :file => file, :author => User.current)
@attachments << a unless a.new_record?
} if params[:attachments] and params[:attachments].is_a? Array
Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('document_added')
redirect_to :action => 'show', :id => @document redirect_to :action => 'show', :id => @document
end end
......
...@@ -116,13 +116,8 @@ class IssuesController < ApplicationController ...@@ -116,13 +116,8 @@ class IssuesController < ApplicationController
def add_note def add_note
journal = @issue.init_journal(User.current, params[:notes]) journal = @issue.init_journal(User.current, params[:notes])
params[:attachments].each { |file| attachments = attach_files(@issue, params[:attachments])
next unless file.size > 0 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
a = Attachment.create(:container => @issue, :file => file, :author => User.current)
journal.details << JournalDetail.new(:property => 'attachment',
:prop_key => a.id,
:value => a.filename) unless a.new_record?
} if params[:attachments] and params[:attachments].is_a? Array
if journal.save if journal.save
flash[:notice] = l(:notice_successful_update) flash[:notice] = l(:notice_successful_update)
Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
...@@ -140,15 +135,8 @@ class IssuesController < ApplicationController ...@@ -140,15 +135,8 @@ class IssuesController < ApplicationController
journal = @issue.init_journal(User.current, params[:notes]) journal = @issue.init_journal(User.current, params[:notes])
@issue.status = @new_status @issue.status = @new_status
if @issue.update_attributes(params[:issue]) if @issue.update_attributes(params[:issue])
# Save attachments attachments = attach_files(@issue, params[:attachments])
params[:attachments].each { |file| attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
next unless file.size > 0
a = Attachment.create(:container => @issue, :file => file, :author => User.current)
journal.details << JournalDetail.new(:property => 'attachment',
:prop_key => a.id,
:value => a.filename) unless a.new_record?
} if params[:attachments] and params[:attachments].is_a? Array
# Log time # Log time
if current_role.allowed_to?(:log_time) if current_role.allowed_to?(:log_time)
@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)
......
...@@ -42,9 +42,7 @@ class MessagesController < ApplicationController ...@@ -42,9 +42,7 @@ class MessagesController < ApplicationController
@message.sticky = params[:message]['sticky'] @message.sticky = params[:message]['sticky']
end end
if request.post? && @message.save if request.post? && @message.save
params[:attachments].each { |file| attach_files(@message, params[:attachments])
Attachment.create(:container => @message, :file => file, :author => User.current) if file.size > 0
} if params[:attachments] and params[:attachments].is_a? Array
redirect_to :action => 'show', :id => @message redirect_to :action => 'show', :id => @message
end end
end end
...@@ -56,9 +54,7 @@ class MessagesController < ApplicationController ...@@ -56,9 +54,7 @@ class MessagesController < ApplicationController
@reply.board = @board @reply.board = @board
@topic.children << @reply @topic.children << @reply
if !@reply.new_record? if !@reply.new_record?
params[:attachments].each { |file| attach_files(@reply, params[:attachments])
Attachment.create(:container => @reply, :file => file, :author => User.current) if file.size > 0
} if params[:attachments] and params[:attachments].is_a? Array
end end
redirect_to :action => 'show', :id => @topic redirect_to :action => 'show', :id => @topic
end end
...@@ -70,9 +66,7 @@ class MessagesController < ApplicationController ...@@ -70,9 +66,7 @@ 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])
params[:attachments].each { |file| attach_files(@message, params[:attachments])
Attachment.create(:container => @message, :file => file, :author => User.current) if file.size > 0
} if params[:attachments] and params[:attachments].is_a? Array
flash[:notice] = l(:notice_successful_update) flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'show', :id => @topic redirect_to :action => 'show', :id => @topic
end end
......
...@@ -181,10 +181,7 @@ class ProjectsController < ApplicationController ...@@ -181,10 +181,7 @@ class ProjectsController < ApplicationController
def add_document def add_document
@document = @project.documents.build(params[:document]) @document = @project.documents.build(params[:document])
if request.post? and @document.save if request.post? and @document.save
# Save the attachments attach_files(@document, params[:attachments])
params[:attachments].each { |a|
Attachment.create(:container => @document, :file => a, :author => User.current) unless a.size == 0
} if params[:attachments] and params[:attachments].is_a? Array
flash[:notice] = l(:notice_successful_create) flash[:notice] = l(:notice_successful_create)
Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added') Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
redirect_to :action => 'list_documents', :id => @project redirect_to :action => 'list_documents', :id => @project
...@@ -237,10 +234,7 @@ class ProjectsController < ApplicationController ...@@ -237,10 +234,7 @@ class ProjectsController < ApplicationController
@custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) } @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
@issue.custom_values = @custom_values @issue.custom_values = @custom_values
if @issue.save if @issue.save
if params[:attachments] && params[:attachments].is_a?(Array) attach_files(@issue, params[:attachments])
# Save attachments
params[:attachments].each {|a| Attachment.create(:container => @issue, :file => a, :author => User.current) unless a.size == 0}
end
flash[:notice] = l(:notice_successful_create) flash[:notice] = l(:notice_successful_create)
Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added') Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
redirect_to :controller => 'issues', :action => 'index', :project_id => @project redirect_to :controller => 'issues', :action => 'index', :project_id => @project
...@@ -345,14 +339,8 @@ class ProjectsController < ApplicationController ...@@ -345,14 +339,8 @@ class ProjectsController < ApplicationController
def add_file def add_file
if request.post? if request.post?
@version = @project.versions.find_by_id(params[:version_id]) @version = @project.versions.find_by_id(params[:version_id])
# Save the attachments attachments = attach_files(@issue, params[:attachments])
@attachments = [] Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added')
params[:attachments].each { |file|
next unless file.size > 0
a = Attachment.create(:container => @version, :file => file, :author => User.current)
@attachments << a unless a.new_record?
} if params[:attachments] and params[:attachments].is_a? Array
Mailer.deliver_attachments_added(@attachments) if !@attachments.empty? && Setting.notified_events.include?('file_added')
redirect_to :controller => 'projects', :action => 'list_files', :id => @project redirect_to :controller => 'projects', :action => 'list_files', :id => @project
end end
@versions = @project.versions.sort @versions = @project.versions.sort
......
...@@ -154,11 +154,7 @@ class WikiController < ApplicationController ...@@ -154,11 +154,7 @@ class WikiController < ApplicationController
def add_attachment def add_attachment
@page = @wiki.find_page(params[:page]) @page = @wiki.find_page(params[:page])
# Save the attachments attach_files(@page, params[:attachments])
params[:attachments].each { |file|
next unless file.size > 0
a = Attachment.create(:container => @page, :file => file, :author => User.current)
} if params[:attachments] and params[:attachments].is_a? Array
redirect_to :action => 'index', :page => @page.title redirect_to :action => 'index', :page => @page.title
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment