Class: PreviewController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/preview_controller.rb

Constant Summary collapse

Languages =
[:ruby, :python, :java, :js, :scss, :sass, :haml, :json, 
:go, :sql, :yaml, :c, :coffee, :properties]

Instance Method Summary collapse

Instance Method Details

#downloadObject



42
43
44
45
46
# File 'app/controllers/preview_controller.rb', line 42

def download
  clazz, id = params[:type].classify.constantize, params[:id] 
  r = clazz.find(id)
  render_native(r.content, r.content_type)
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/preview_controller.rb', line 6

def index
  clazz, id = params[:type].classify.constantize, params[:id] 
  r = clazz.find(id)
  type, ext = r.content_type[/^[^\/]+/].to_sym, r.content_type[/(?<=\/)(x-)?(.+)/, 2].to_sym
  if Languages.include?(ext)
    @code = CodeRay.scan(r.content, ext).div(:line_numbers => :table, :css => :class)
    render 'code', layout: 'coderay'
  elsif [:pdf, :html].include?(ext)
    render_native(r.content, r.content_type)
  else
    case type
    when :text
      render_native(r.content, 'text/plain')
    when :image
      render_native(r.content, r.content_type)
    else
      @resource = r
      render 'download', layout: 'coderay'
    end
  end
end

#openObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/preview_controller.rb', line 28

def open
  case RUBY_PLATFORM
  when /darwin/i
    open_osx_file
  when /linux/i
    open_linux_file
  when /cygwin|mswin|mingw|bccwin|wince|emx/
    open_windows_file
  else
    #other
  end
  head :no_content
end