0

在 Rails 中管理模型和控制器之间的对象是否有最佳实践?我的代码笨拙地将文件从控制器传递到模型并返回,如下所示:

document_controller.rb

tmp_file = Tempfile.new(Digest::MD5.hexdigest(rand(12).to_s))
pdf = Prawn::Document.generate(tmp_file.path(), :page_size => "A4", :skip_page_creation => true) do |posting|
  @document.process(posting)
  send_data posting.render, :filename => "#{@document.id}", :type => "application/pdf"
end

并在document.rb

def process(pdf)
  pdf.start_new_page
  pdf.image self.user.logo.path, :width => pdf.bounds.width
  pdf.fill_color '#444444'
  pdf.image self.component.image_snapshot.path, :width => pdf.bounds.width
  pdf.move_down 20
  pdf.indent 20 do
    pdf.text self.component.name, :size => 24
  end
  box_start = pdf.cursor
  pdf.reflow_column_box([20, box_start-15], :width => pdf.bounds.width-20) do
    format_for_prawn(pdf, self.component.body, self.user.colour1)
  end
end

我怎样才能重写这段代码,这样我就不必笨拙地传递pdf/posting对象?感觉非常不像Rails。

感谢您的输入。

编辑:更糟糕的例子。

再次在控制器中:

tempfile = Magick::Image.read(@document.component.image_newsletter.path).first
overlay = Magick::Image.read(@document.user.logo.path).first.resize_to_fit(tempfile.columns)
rec = Magick::Draw.new
rec.stroke = "##{@document.user.colour1}"
rec.fill = "##{@document.user.colour1}"
rec.rectangle 0, 0, tempfile.columns, 5
lank = tempfile.extent(tempfile.columns, tempfile.rows+overlay.rows, 0 ,0)
final = lank.composite(overlay, Magick::SouthGravity, 0, 0, Magick::OverCompositeOp)
rec.draw(final)
send_data final.to_blob, :filename => "#{@document.user.name}-#{@document.id}.jpg", :type => "image/jpg"

这是在控制器中,否则send_data无法访问final. 有什么帮助吗?

再次感谢。

4

2 回答 2

0

为了将凌乱的控制器代码放入模型中,我这样做了:

def process_media
  temp = "#{::Rails.root}/public/temporary/#{temp_file_name}"
  tempfile = Magick::Image.read(self.component.image_newsletter.path).first
  overlay = Magick::Image.read(self.user.logo.path).first.resize_to_fit(tempfile.columns)
  rec = Magick::Draw.new
  rec.stroke = "##{self.user.colour1}"
  rec.fill = "##{self.user.colour1}"
  rec.rectangle 0, 0, tempfile.columns, 5
  lank = tempfile.extent(tempfile.columns, tempfile.rows+overlay.rows, 0 ,0)
  final = lank.composite(overlay, Magick::SouthGravity, 0, 0, Magick::OverCompositeOp)
  rec.draw(final)
  final.to_blob
  final.write("#{temp}.jpg")
end

我正在send_file从控制器发送实际文件。希望对某人有所帮助。

于 2013-11-14T05:19:41.310 回答
0

编辑

您可以通过以下方式为内置类创建扩展方法

#lib/ext/prawn/document.rb

class Prawn::Document
def process(pdf)
  pdf = self if pdf.blank?
  pdf.start_new_page
  pdf.image self.user.logo.path, :width => pdf.bounds.width
  pdf.fill_color '#444444'
  pdf.image self.component.image_snapshot.path, :width => pdf.bounds.width
  pdf.move_down 20
  pdf.indent 20 do
    pdf.text self.component.name, :size => 24
  end
  box_start = pdf.cursor
  pdf.reflow_column_box([20, box_start-15], :width => pdf.bounds.width-20) do
    format_for_prawn(pdf, self.component.body, self.user.colour1)
  end
end
end

在你的控制器中

tmp_file = Tempfile.new(Digest::MD5.hexdigest(rand(12).to_s))
pdf = Prawn::Document.generate(tmp_file.path(), :page_size => "A4", :skip_page_creation => true) do |posting|
  posting.process
  send_data posting.render, :filename => "#{@document.id}", :type => "application/pdf"
end

要加载此类,您需要在config/application.rb文件或初始化程序中要求它。

require 'ext/prawn/document'

于 2013-11-12T06:00:03.797 回答