lib/alego_pdf/merging.rb in alego_pdf-1.0.0 vs lib/alego_pdf/merging.rb in alego_pdf-2.0.0
- old
+ new
@@ -1,20 +1,28 @@
require 'hexapdf'
require 'open-uri'
module AlegoPdf
class Merging
- attr_reader :filepaths, :filename
+ attr_reader :ids, :model, :attribute, :filename
- def initialize(filepaths, filename)
- @filepaths = filepaths
+ def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf')
+ @ids = ids
+ @model = model
+ @attribute = attribute
@filename = filename
end
def call
+ array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids)
target = HexaPDF::Document.new
- filepaths.each do |filepath|
+ array.each do |object|
+ file = object[attribute]
+ filepath = file.try(:url) || object.try(:arquivo_url)
+
+ next if filepath.blank?
+
uri = URI.parse(filepath).open(&:read)
tempfile = Tempfile.new
tempfile.write(uri.force_encoding('UTF-8'))
tempfile.close