Class: ActionPrinter

Inherits:
ActionView::Base
  • Object
show all
Includes:
Exceptions
Defined in:
lib/action_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(printer, paper) ⇒ ActionPrinter

Returns a new instance of ActionPrinter.



8
9
10
11
12
# File 'lib/action_printer.rb', line 8

def initialize(printer,paper)
  @printer=printer
  @paper=paper
  super(Rails.configuration.paths['app/views'])
end

Instance Attribute Details

#paperObject

Returns the value of attribute paper.



6
7
8
# File 'lib/action_printer.rb', line 6

def paper
  @paper
end

#printerObject

Returns the value of attribute printer.



6
7
8
# File 'lib/action_printer.rb', line 6

def printer
  @printer
end

Instance Method Details

#commandObject



14
15
16
# File 'lib/action_printer.rb', line 14

def command
  @printer.command
end

#do_print(copies, usr = nil) ⇒ Object

the default print is sending a link to the user who ordered the print



50
51
52
53
54
55
56
57
58
# File 'lib/action_printer.rb', line 50

def do_print copies, usr=nil
  # move pdf_file_path to a static place where it will not be accidentally deleted from
  # static_path = File.join(Rails.root,'public','files','1.pdf')
  # var = %x[ mv #{pdf_file_path} #{static_path}  2>&1 ]
  # send email to usr with link
  # raise MovingFileFailedError.new(var) unless var.blank?
  PrinterMailer.send_print(usr,file_path).deliver_now if usr
  true
end

#do_render(print_job, *args) ⇒ Object

the default rendering is from a string of chars to an html file - statically placed for who ever knows the exact path to the file, to see



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/action_printer.rb', line 22

def do_render(print_job,*args)
  path = print_job.view_template_path
  # TODO vi får en fejl her når vi begynder at mixe modellerne f.eks. Employee.find_by_sql('select * from products')
rc = print_job.printing_class.constantize
coll = rc.find_by_sql( print_job.print_sql)
  locals = { resource_class: rc, collection: coll, resource: coll.first  }
  #
  # TODO args must be mergeable ;)
  # if args.flatten.compact.any?
  #   locals.merge! args
  # end

#
# this is WorkInProgress
#
  if (path =~/^---/) # this will return falsy if found
    of = html_file render_string_in path.gsub( /^---/, ''), locals
  else
    of = html_file render( file: path, formats: [:html], handlers: [:haml,:erb], locals: locals)
  end
  #
#
  logit :info, "created a html file: #{of.path}"

  of.path
end

#file_pathObject



81
82
83
# File 'lib/action_printer.rb', line 81

def file_path
  raise "must be implemented on the driver!!"
end

#get_file_typeObject



93
94
95
# File 'lib/action_printer.rb', line 93

def get_file_type
  raise "you must implement this on the driver!!"
end

#html_file(str, type = 'html') ⇒ Object

build a temp file with the HTML string rendered and provided as argument str



69
70
71
72
73
74
75
# File 'lib/action_printer.rb', line 69

def html_file str, type='html'
  f = Tempfile.new(['print', ".#{type}"])
  f.puts str
  f.close
  Rails.logger.info "oxen: wrote #{f.path}"
  f
end

#label_file_pathObject



85
86
87
# File 'lib/action_printer.rb', line 85

def label_file_path
  @label_file_path ||= `mktemp -t lblXXXX`[0..-2]
end

#logit(log_type, msg) ⇒ Object



105
106
107
# File 'lib/action_printer.rb', line 105

def logit( log_type, msg )
  Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}")
end

#pdf_file_pathObject



89
90
91
# File 'lib/action_printer.rb', line 89

def pdf_file_path
  @pdf_file_path ||= `mktemp -t pdfXXXX`[0..-2]
end

#render_string_in(content, locals) ⇒ Object

this is WorkInProgress !!



63
64
65
# File 'lib/action_printer.rb', line 63

def render_string_in content, locals
  render body: Haml::Engine.new( ERB.new(content).result ).render
end

#send_print_file(context) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/action_printer.rb', line 97

def send_print_file context
  context.send_file pdf_file_path, filename: 'oxen_file', type: get_file_type, disposition: "attachment"
  # context.cookies['fileDownload'] = 'true'
  # File.open(pdf_file_path, 'r') do |fp|
  #   context.send_data fp.read.force_encoding('BINARY'), filename: 'oxen_file', type: "application/pdf", disposition: "attachment"
  # end
end

#text_file(str) ⇒ Object



77
78
79
# File 'lib/action_printer.rb', line 77

def text_file str
  html_file str, 'label'
end