Class: ActionPrinter
- Inherits:
-
ActionView::Base
- Object
- ActionView::Base
- ActionPrinter
show all
- Includes:
- Exceptions
- Defined in:
- lib/action_printer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#build_print_cmd(prn, args) ⇒ Object
-
#command ⇒ Object
-
#command=(val) ⇒ Object
-
#do_print(print_job, args) ⇒ Object
the default print is sending a link to the user who ordered the print.
-
#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.
-
#file_path ⇒ Object
-
#get_file_type ⇒ Object
-
#html_file(str, type = 'html') ⇒ Object
build a temp file with the HTML string rendered and provided as argument str.
-
#initialize(printer, paper) ⇒ ActionPrinter
constructor
A new instance of ActionPrinter.
-
#label_file_path ⇒ Object
-
#logit(log_type, msg) ⇒ Object
-
#mail_print(print_job, args) ⇒ Object
-
#pdf_file_path ⇒ Object
-
#print_print(prn, print_job, args) ⇒ Object
-
#render_string_in(content, locals) ⇒ Object
this is WorkInProgress !!.
-
#send_print_file(context) ⇒ Object
-
#text_file(str) ⇒ Object
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
#paper ⇒ Object
Returns the value of attribute paper.
6
7
8
|
# File 'lib/action_printer.rb', line 6
def paper
@paper
end
|
#printer ⇒ Object
Returns the value of attribute printer.
6
7
8
|
# File 'lib/action_printer.rb', line 6
def printer
@printer
end
|
Instance Method Details
#build_print_cmd(prn, args) ⇒ Object
145
146
147
148
149
150
|
# File 'lib/action_printer.rb', line 145
def build_print_cmd prn, args
cups = !prn.printserver ? prn.cups_printer : "#{prn.cups_printer} -h 10.0.0.26:4#{prn.printserver.port}"
pdf_path = pdf_file_path.split("/")[-1]
paper = args.delete(:paper) || prn.paper
prn.command.gsub( /\$1/, cups ).gsub( /\$2/, paper ).gsub( /\$3/, pdf_file_path ).gsub( /\$4/, pdf_path )
end
|
#command ⇒ Object
14
15
16
|
# File 'lib/action_printer.rb', line 14
def command
@printer.command
end
|
#command=(val) ⇒ Object
18
19
20
|
# File 'lib/action_printer.rb', line 18
def command=val
@printer.command=val
end
|
#do_print(print_job, args) ⇒ Object
the default print is sending a link to the user who ordered the print
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/action_printer.rb', line 55
def do_print print_job, args
result = nil
prn = printer
if prn.nil? or prn.command.blank? or prn.command=="email"
mail_print print_job, args
else
print_print prn, print_job, args
end
rescue => e
logit :error, "Printing failed! The error was #{e.message}"
false
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/action_printer.rb', line 26
def do_render(print_job,*args)
path = print_job.view_template_path
rc = print_job.printing_class.constantize
coll = rc.find_by_sql( print_job.print_sql)
locals = { resource_class: rc, collection: coll, resource: coll.first, resources: coll }
if (path =~/^---/)
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_path ⇒ Object
89
90
91
|
# File 'lib/action_printer.rb', line 89
def file_path
raise "must be implemented on the driver!!"
end
|
#get_file_type ⇒ Object
101
102
103
|
# File 'lib/action_printer.rb', line 101
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
77
78
79
80
81
82
83
|
# File 'lib/action_printer.rb', line 77
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_path ⇒ Object
93
94
95
|
# File 'lib/action_printer.rb', line 93
def label_file_path
@label_file_path ||= `mktemp -t lblXXXX`[0..-2]
end
|
#logit(log_type, msg) ⇒ Object
152
153
154
|
# File 'lib/action_printer.rb', line 152
def logit( log_type, msg )
Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}")
end
|
#mail_print(print_job, args) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/action_printer.rb', line 114
def mail_print print_job, args
msg = args[:message] || "Der er ikke defineret nogen printere til dette job, for dig - så jeg er nødt til at sende dig filen vedhæftet denne email! ;)Du bør kontakte din system administrator og bede om at der bliver oprettet en printer definition til denne printer jobtype, for dig!"
if print_job.printed_by.nil?
raise PrintJobPrintingError.new t('action_printer.user_not_found')
else
PrinterMailer.send_print(print_job.printed_by,file_path,msg,t('print_delivered_by_email')).deliver_now
end
end
|
#pdf_file_path ⇒ Object
97
98
99
|
# File 'lib/action_printer.rb', line 97
def pdf_file_path
@pdf_file_path ||= `mktemp -t pdfXXXX`[0..-2]
end
|
#print_print(prn, print_job, args) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/action_printer.rb', line 128
def print_print prn, print_job, args
args[:copies] ||= 1
cmd = build_print_cmd prn, args
if args[:copies].to_i > 1
cmdargs = cmd.split(" ")
f = cmdargs.pop
cmdargs.push "-n", args[:copies].to_i, f
cmd = cmdargs.join(" ")
end
return `#{cmd}`
end
|
#render_string_in(content, locals) ⇒ Object
this is WorkInProgress !!
71
72
73
|
# File 'lib/action_printer.rb', line 71
def render_string_in content, locals
render body: Haml::Engine.new( ERB.new(content).result ).render
end
|
#send_print_file(context) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/action_printer.rb', line 105
def send_print_file context
context.send_file pdf_file_path, filename: 'oxen_file', type: get_file_type, disposition: "attachment"
end
|
#text_file(str) ⇒ Object
85
86
87
|
# File 'lib/action_printer.rb', line 85
def text_file str
html_file str, 'label'
end
|