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
。何か助けはありますか?
再度、感謝します。