Class: FileAttachmentsController

Inherits:
FileShare::ApplicationController show all
Defined in:
app/controllers/file_attachments_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/file_attachments_controller.rb', line 52

def create
  if params[:file]
    file_params = {
      :uploaded_file => params[:file]
    }
    if params[:attachable_id] && params[:attachable_type]
      file_params.merge!({
        :attachable_id   => params[:attachable_id],
        :attachable_type => params[:attachable_type]
      })
    end
    @file_attachment = FileAttachment.new file_params
  elsif params[:file_attachment] && params[:file_attachment][:uploaded_file]
    @file_attachment = FileAttachment.new params[:file_attachment]
  end
  
  if @file_attachment && @file_attachment.save
    flash[:notice] = "File uploaded."
  else
    if @file_attachment
      flash[:warning] = "Unable to save file attachment: #{@file_attachment.errors.full_messages.join('; ')}"
    else
      flash[:warning] = "No files were selected for upload."
    end
  end
  unless params[:file] # request.xhr? # html5 based multiple uploads are not xhr ?
    redirect_to_index_or_attachable(:std => 1) # {:std => 1} - make sure the std html form displays
  else
    flash.discard
    render :partial => 'file_attachments/file_attachment', :object => @file_attachment
  end
end

#destroyObject



107
108
109
110
111
112
# File 'app/controllers/file_attachments_controller.rb', line 107

def destroy
  @file_attachment = FileAttachment.find(params[:id])
  @file_attachment.destroy
  flash[:notice] = "Deleted File: #{@file_attachment.name}"
  redirect_to_index_or_attachable
end

#downloadObject



44
45
46
47
48
49
50
# File 'app/controllers/file_attachments_controller.rb', line 44

def download
  file_attachment = FileAttachment.find(params[:id])
  file_itself     = File.open(file_attachment.full_path, 'r')
  logger.debug("FILE ATTACHMENT INFO: #{file_attachment.inspect}")
  logger.debug("FILE INFO: #{file_itself.inspect}")
  send_data(file_itself.read, :filename => File.basename(file_itself.path), :stream => true, :buffer_size => 1.megabyte)
end

#editObject



85
86
87
# File 'app/controllers/file_attachments_controller.rb', line 85

def edit
  @file_attachment = FileAttachment.find(params[:id])
end

#indexObject



39
40
41
42
# File 'app/controllers/file_attachments_controller.rb', line 39

def index
  @orphans = FileAttachment.orphans
  @files   = FileAttachment.attached
end

#newObject



34
35
# File 'app/controllers/file_attachments_controller.rb', line 34

def new
end

#showObject



36
37
38
# File 'app/controllers/file_attachments_controller.rb', line 36

def show
  @file_attachment = FileAttachment.find(params[:id])
end

#updateObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/file_attachments_controller.rb', line 89

def update
  @file_attachment = FileAttachment.find(params[:id])
  respond_to do |format|
    if @file_attachment.update_attributes(params[:file_attachment])
      format.html do
        flash[:notice] = "Updated File: #{@file_attachment.name}"
        redirect_to_index_or_attachable
      end
    else
      format.html do
        flash.now[:warning] = "The description could not be saved, please try again."
        render :edit
      end
    end
    format.js
  end
end