Rails:在模型和控制器之间传递对象

时间:2013-11-12 05:23:51

标签: ruby-on-rails ruby ruby-on-rails-3 model-view-controller

在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。有什么帮助吗?

再次感谢。

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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的实际文件。希望有所帮助。