Class | AttachmentController |
In: |
app/controllers/attachment_controller.rb
|
Parent: | ApplicationController |
添付ファイル機能を実装している。
添付ファイルの編集を取り消す。
# File app/controllers/attachment_controller.rb, line 78 78: def cancel 79: if prepare_to_edit 80: session[:uploaded_attachments].clear 81: session[:deleted_attachments].clear 82: end 83: end
添付ファイルを削除する。
# File app/controllers/attachment_controller.rb, line 51 51: def delete 52: if prepare_to_edit 53: if params[:type] 54: if params[:attachment] 55: conditions = { 56: :id => params[:attachment], 57: :attachable_id => params[:id], 58: :attachable_type => params[:type], 59: } 60: session[:deleted_attachments] |= Attachment.find(:all, :conditions => conditions) 61: end 62: if params[:binary_object] 63: session[:uploaded_attachments].delete_if do |attachment| 64: attachment.file_type == "BinaryObject" && params[:binary_object].map(&:to_i).include?(attachment.file_id) && attachment.file.destroy 65: end 66: end 67: if params[:storage_object] 68: session[:uploaded_attachments].delete_if do |attachment| 69: attachment.file_type == "StorageObject" && params[:storage_object].map(&:to_i).include?(attachment.file_id) && attachment.file.destroy 70: end 71: end 72: end 73: end 74: reload 75: end
添付ファイルをダウンロードする。
# File app/controllers/attachment_controller.rb, line 86 86: def download 87: begin 88: attachment = Attachment.find(params[:id]) 89: rescue ActiveRecord::RecordNotFound 90: raise NotFoundException, "attachment not found: #{params[:id]}" 91: end 92: file = attachment.file 93: return send_data(file.content, :type => file.mime_type, :filename => file.name) if file.is_a?(BinaryObject) 94: return send_file(file.path, :type => file.mime_type, :filename => file.name) if file.is_a?(StorageObject) 95: raise NotFoundException, "file not found" 96: end
添付ファイルを編集する。
# File app/controllers/attachment_controller.rb, line 14 14: def edit 15: prepare_to_edit 16: end
添付ファイルの一覧を表示する。
# File app/controllers/attachment_controller.rb, line 4 4: def show 5: prepare 6: end
添付ファイルに関する現在のサマリを表示する。
# File app/controllers/attachment_controller.rb, line 9 9: def summary 10: prepare 11: end
(試験用)
# File app/controllers/attachment_controller.rb, line 99 99: def test 100: if prepare 101: @current_view = "view_m" 102: @sub_view = "view_ae" 103: end 104: end
(試験用)
# File app/controllers/attachment_controller.rb, line 114 114: def test_close 115: session[:deleted_attachments] = [] 116: session[:uploaded_attachments] = [] 117: redirect_to :action => "test", :id => params[:id], :type => params[:type] 118: end
(試験用)
# File app/controllers/attachment_controller.rb, line 107 107: def test_execute 108: session[:deleted_attachments].delete_if(&:destroy) 109: session[:uploaded_attachments].delete_if(&:save) 110: redirect_to :action => "test", :id => params[:id], :type => params[:type] 111: end
添付ファイルをアップロードする。
# File app/controllers/attachment_controller.rb, line 19 19: def upload 20: prepare_to_edit 21: @attachment = Attachment.new do |a| 22: a.attachable_id = params[:id] 23: a.attachable_type = params[:type] 24: a.description = params[:attachment]["description"] 25: end 26: begin 27: file_class = params[:attachment]["file_type"].constantize 28: rescue NameError 29: return reload 30: end 31: content = params[:attachment]["file_content"] 32: return reload if content.blank? 33: file = file_class.new :name => content.original_filename, :mime_type => content.content_type 34: begin 35: file_class.transaction do 36: if file.save 37: if file.store(content) 38: @attachment.file = file 39: session[:uploaded_attachments].push(@attachment) 40: return reload 41: end 42: end 43: end 44: rescue ConfigAttachment::MaximumLengthError => e 45: flash[:warning] = s_("flash|warning|Upload a file of length <= %{maximum_length}.") % {:maximum_length => ConfigAttachment.current.maximum_length} 46: end 47: reload 48: end