Class: Rich::FilesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/rich/files_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/rich/files_controller.rb', line 48

def create

  @file = RichFile.new(:simplified_type => params[:simplified_type])
  
  if(params[:scoped] == 'true')
    @file.owner_type = params[:scope_type]
    @file.owner_id = params[:scope_id].to_i
  end
  
  # use the file from Rack Raw Upload
  if(params[:file])
    params[:file].content_type = Mime::Type.lookup_by_extension(params[:file].original_filename.split('.').last.to_sym)
    @file.rich_file = params[:file]
  end
  
  if @file.save
    render :json => { :success => true, :rich_id => @file.id }
  else
    render :json => { :success => false, 
                      :error => "Could not upload your file:\n- "+@file.errors.to_a[-1].to_s,
                      :params => params.inspect }
  end
end

#destroyObject



72
73
74
75
76
77
# File 'app/controllers/rich/files_controller.rb', line 72

def destroy  
  if(params[:id])
    rich_file = RichFile.delete(params[:id])
    @fileid = params[:id]
  end
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/rich/files_controller.rb', line 8

def index
  @type = params[:type]
  
  if(params[:scoped] == 'true')
    if(@type == "image")
      @items = RichFile.images.order("created_at DESC").where("owner_type = ? AND owner_id = ?", params[:scope_type], params[:scope_id]).page params[:page]
    else
      @items = RichFile.files.order("created_at DESC").where("owner_type = ? AND owner_id = ?", params[:scope_type], params[:scope_id]).page params[:page]
    end        
  else
    if(@type == "image")
      @items = RichFile.images.order("created_at DESC").page params[:page]
    else
      @items = RichFile.files.order("created_at DESC").page params[:page]
    end        
  end
  
  # stub for new file
  @rich_asset = RichFile.new
  
  respond_to do |format|
    format.js
    format.html
  end
  
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/rich/files_controller.rb', line 35

def show
  # show is used to retrieve single files through XHR requests after a file has been uploaded
  
  if(params[:id])
    # list all files
    @file = RichFile.find(params[:id])
    render :layout => false
  else 
    render :text => "File not found"
  end
  
end