Class: ActionController::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rghost_rails.rb

Instance Method Summary collapse

Instance Method Details

#rghost_render(format, options) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rghost_rails.rb', line 8

def rghost_render(format, options)
  report = options.delete(:report)

  template = case report
  when Hash
    File.join(report[:controller] || controller_name, report[:action].to_s)
  when String, Symbol
    File.join(controller_path, report.to_s)
  when NilClass
    File.join(controller_path, action_name)
  end
  template_file = begin
    template = view_paths.find_template(template, "rghost.rb", false)
    template.filename
  rescue NoMethodError 
    fname = "#{template}.rghost.rb"
    template_dir = view_paths.detect do |path|
      File.exists? File.join(path.to_s,fname)
    end
    File.join(template_dir.to_s,fname)
  end
  
  ActionView::Helpers.included_modules.each do |m|
    extend m
  end
  lines = File.readlines(template_file.to_s)
  
  doc = eval(lines.join)

  filename = options.delete(:filename)
  disposition=options.delete(:disposition) || 'attachment'
  rghost = doc.render(format, options)
  output = rghost.output
  
  raise "RGhost::Error #{rghost.errors} - #{output}" if rghost.error?

  data = output.readlines.join
  rghost.clear_output

  send_data(data, :filename => filename, :disposition => disposition, :type => Mime::Type.lookup_by_extension(format.to_s))
end