0

我有一个类,用于渲染一些从 AbstractController 继承的 pdf,我想在视图中使用 cache_digests

class PDFExporter < AbstractController::Base
  include AbstractController::Rendering
  include ActionView::Layouts
  include AbstractController::Helpers
  include AbstractController::Translation
  include AbstractController::AssetPaths
  include AbstractController::Callbacks

  helper ApplicationHelper, FormatterHelper, FontAwesome::Rails::IconHelper
  view_paths << "app/views"

  TMP_PATH = File.join(Rails.root, "tmp")
  PDFTK_PATH = "/usr/bin/pdftk"
  LAYOUT = "pdf.html.erb"
  COVER = "shared/pdf_cover.html.haml"
  MARGINS = {top: 17, left: 0, right: 0, bottom: 9.5}

  def self.save_pdf_file(pdf)
    file = Tempfile.new("pdf", encoding: "ASCII-8BIT")
    file.write(pdf)
    file
  end

  protected
  def generate_pdf(templates)
    partials = generate_partials(templates)
    output = File.join(TMP_PATH, "#{filename}.pdf")
    paths = partials.map(&:path).join(" ")
    system("#{PDFTK_PATH} #{paths} cat output #{output}")
    partials.map(&:unlink)
    output
  end

  private
  def generate_partials(templates)
    templates.map do |template|
      partial = send("partial_#{template.shift}", *template)
      PDFExporter.save_pdf_file(partial)
    end
  end

  def partial_content(template)
    rendered = render(template: template, layout: LAYOUT)
    WickedPdf.new.pdf_from_string(rendered,
      orientation: "Landscape",
      margin: MARGINS,
      header: {
        spacing: 3,
        content: render_to_string("shared/pdf_header.pdf.haml",
        layout: LAYOUT)
      },
      footer: {
        content: render_to_string("shared/pdf_footer.pdf.haml",
        layout: LAYOUT)
      }
    )
  end

  def partial_cover(template = COVER)
    rendered = render(template: template, layout: LAYOUT)
    WickedPdf.new.pdf_from_string(rendered, orientation: "Landscape",
      margin: MARGINS, header: nil, footer: nil)
  end
end

当我尝试cache在视图中调用时

- cache [@fund, @fund_sheet.report_date] do

我得到:

undefined method `perform_caching' for #<FundSheet::Exporter:0x0000000e128e10>

我试图在其中包含一些缓存模块,但我可以让它工作

4

1 回答 1

0

我知道了!

class PDFExporter < ActionController::Metal
  <...>
  include ActionController::Caching
  config.cache_store = Rails.cache
  <...>
end
于 2014-08-01T12:32:40.790 回答