oxen_printer 0.3.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2856583ac970726480283ef1ffcbc0443e0a902a
4
- data.tar.gz: 5481a1caeae750b7caf77783d6533572b0f799f1
3
+ metadata.gz: b3926f0dc3bad41ffe690545fb03beea76eb5482
4
+ data.tar.gz: ed071ff53e49d90317bf37744b1bd9049cb05ec0
5
5
  SHA512:
6
- metadata.gz: da1d19e301d9253df75b949376925d96c25be759385bbcfcf29b30aa5be020decd301dcc7c026b6cd567b4bc462f7d8aede878a20582a7bb09406fe75533edeb
7
- data.tar.gz: 52dd2b0e5d641f417ba286f078f45fe41fbea96db9f6d31a85cbe293d1efae7f250c120f10fd2c4417ee18d3ef2c0499aeac390caac003b10ab50442ad6abadc
6
+ metadata.gz: 510a1ea5d4a260c6563a808778defa8da6eb6507191e34123c6e9669c62af4fb4be5515dd79ae795fe22890e388ff56fe9f2026a5ea5ef9c04b48105db0ee8d8
7
+ data.tar.gz: efb4fc3b9c431b712bf426bd424aae5cf3088a2c93986204ceea360d05d240038f743ad07e50e599cdbc7b04a4c29ce30f311a6cff8d70ed7c1174fa993b8a9b
@@ -15,9 +15,9 @@ class Oxen::PrintersController < AbstractResourcesController
15
15
 
16
16
  opt = { items: {}}
17
17
  case params[:action]
18
- when 'edit','update'; opt[:items].merge! list: { ajax: 'get', icon: 'list', class: 'blue', url: "/admin/accounts/#{resource.account.id}" }
19
- when 'show'; opt[:items].merge! list: { ajax: 'get', icon: 'list', class: 'blue', url: "/admin/accounts/#{resource.account.id}" }
20
- when 'index'; opt[:items].merge! list: { ajax: 'get', icon: 'list', class: 'blue', url: "/admin/accounts/#{resource.account.id}" }
18
+ when 'edit','update';# opt[:items].merge! list: { ajax: 'get', icon: 'list', class: 'blue', url: "/admin/accounts/#{resource.account.id}" }
19
+ when 'show'; # opt[:items].merge! list: { ajax: 'get', icon: 'list', class: 'blue', url: "/admin/accounts/#{resource.account.id}" }
20
+ when 'index'; # opt[:items].merge! list: { ajax: 'get', icon: 'list', class: 'blue', url: "/admin/accounts/#{resource.account.id}" }
21
21
  end
22
22
 
23
23
  @fab_button_options = opt
@@ -1,7 +1,7 @@
1
1
  class PrinterMailer < ActionMailer::Base
2
2
  layout 'mailer'
3
3
 
4
- def send_print usr, file
4
+ def send_print usr, file, msg=nil
5
5
  @usr = User.find(usr)
6
6
  fname = case file
7
7
  when /\/pdf.*$/; 'print%s.pdf' % DateTime.now.nsec
@@ -9,7 +9,7 @@ class PrinterMailer < ActionMailer::Base
9
9
  end
10
10
  attachments[fname] = File.read(file) unless file.blank?
11
11
  @logo = @usr.account.logos.first.image.url rescue ""
12
- @msg = "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!"
12
+ @msg = msg || "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!"
13
13
 
14
14
  mail from: 'no-reply@wheelstore.space',
15
15
  to: @usr.email,
@@ -9,3 +9,9 @@ da:
9
9
  cups_printer: 'Angiv navnet på den CUPS printer, der skal håndtere print'
10
10
  cups_cmd: 'Indfør den kommando som skal starte printjobbet ("lp -d $1 -o media=$2 $3" eller "lp -d $1 -o raw $2")'
11
11
  paper: 'Angiv papirformatet - exvis Letter, A4, 30x57mm, eller lignende'
12
+ output:
13
+ email:
14
+ email_sent: 'Udskriften dannet korrekt og email sendt!'
15
+ email_address_missing: 'Email adresse ikke fundet - udskrift ikke sendt!'
16
+ medium:
17
+ no_medium_found: 'Intet medie valgt?! Udskriften kunne ikke dannes!'
@@ -1,6 +1,6 @@
1
1
  #encoding: utf-8
2
2
  class ActionPrinter < ActionView::Base
3
-
3
+
4
4
  include Exceptions
5
5
 
6
6
  attr_accessor :printer, :paper
@@ -17,11 +17,14 @@ class PrinterGenerator < Rails::Generators::NamedBase
17
17
  def create_migration_file
18
18
  migration_template 'printer.rb', 'db/migrate/create_printers.rb'
19
19
  # allow the clock to advance 1sec
20
- sleep 1
20
+ sleep 2
21
21
  migration_template 'print_job.rb', 'db/migrate/create_print_jobs.rb'
22
22
  # allow the clock to advance 1sec
23
- sleep 1
23
+ sleep 2
24
24
  migration_template 'printable.rb', 'db/migrate/create_printables.rb'
25
+ # allow the clock to advance 1sec
26
+ sleep 2
27
+ migration_template 'template.rb', 'db/migrate/create_templates.rb'
25
28
  end
26
29
 
27
30
  end
@@ -1,3 +1,3 @@
1
1
  module OxenPrinter
2
- VERSION = "0.3.4"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/print_drivers.rb CHANGED
@@ -16,36 +16,32 @@ module PrintDrivers
16
16
 
17
17
  # build PDF or somehow prepare printer output
18
18
  def rendered_on(*args)
19
- begin
20
- update_column(:state, "rendering")
19
+ update_column(:state, "rendering")
21
20
 
22
- # TODO 2013-06-08 set the action_printer with respect to the selected output & printer
23
- action_printer = case print_driver
24
- when :pdf, "pdf"
25
- require 'oxen_printer/pdf_printer'
26
- PdfPrinter.new(printer, paper)
27
- when :cab, "cab"
28
- require 'oxen_printer/cab_printer'
29
- CabPrinter.new(printer, paper)
30
- when :label, "label"
31
- require 'oxen_printer/label_printer'
32
- LabelPrinter.new(printer, paper)
33
- when :html, "html"
34
- require 'oxen_printer/html_printer'
35
- HtmlPrinter.new(printer, paper)
36
- when :csv, "csv"
37
- require 'oxen_printer/csv_printer'
38
- CsvPrinter.new(printer, paper)
39
- end
40
- rescue => e
41
- logit :error, "assigning printer driver failed - #{e.message}"
42
- return nil
21
+ # TODO 2013-06-08 set the action_printer with respect to the selected output & printer
22
+ action_printer = case print_driver
23
+ when :pdf, "pdf"
24
+ require 'oxen_printer/pdf_printer'
25
+ PdfPrinter.new(printer, paper)
26
+ when :cab, "cab"
27
+ require 'oxen_printer/cab_printer'
28
+ CabPrinter.new(printer, paper)
29
+ when :label, "label"
30
+ require 'oxen_printer/label_printer'
31
+ LabelPrinter.new(printer, paper)
32
+ when :html, "html"
33
+ require 'oxen_printer/html_printer'
34
+ HtmlPrinter.new(printer, paper)
35
+ when :csv, "csv"
36
+ require 'oxen_printer/csv_printer'
37
+ CsvPrinter.new(printer, paper)
43
38
  end
44
39
 
45
40
  # print_job.action_printer.printer = print_job.printer
46
41
  # print_job.cycle if
47
42
  return action_printer if action_printer.do_render(self,args)
48
- nil
43
+ update_column(:state, 'error rendering')
44
+ raise PrintJobRenderingError
49
45
  end
50
46
 
51
47
  #
@@ -65,16 +61,13 @@ module PrintDrivers
65
61
  end
66
62
 
67
63
  def send_with action_printer, context
68
- begin
69
- update_column(:state, 'sending')
70
- if action_printer && action_printer.send_print_file( context)
71
- update_column(:state, 'done')
72
- action_printer.file_path
73
- else
74
- update_column(:state, 'error sending')
75
- raise PrintJobPrintingError
76
- end
77
- rescue => e
64
+ update_column(:state, 'sending')
65
+ if action_printer && action_printer.send_print_file( context)
66
+ update_column(:state, 'done')
67
+ action_printer.file_path
68
+ else
69
+ update_column(:state, 'error sending')
70
+ raise PrintJobPrintingError
78
71
  end
79
72
  end
80
73
 
data/lib/print_engine.rb CHANGED
@@ -11,6 +11,43 @@
11
11
  # http://rarlindseysmash.com/posts/config-and-generators-in-gems
12
12
  #
13
13
 
14
+ #
15
+ #
16
+ # ARGS # first value is default
17
+ #
18
+ # collation # 'list' | 'record'
19
+ # paper # 'A4' | 'label' | ...
20
+ # template # '' | 'slip' | 'quote'
21
+ # cmd # '' | 'print_label' - a particular method on the printing_class
22
+ #
23
+ # PRINTPROMPT
24
+ # print[medium] # 'display' | 'email' | 'printer' | 'download'
25
+ # print[output_type] # 'html' | 'pdf' | 'text'
26
+ # print[printer] # what printer to send output to
27
+ # print[email_to] # email address
28
+ # print[message] # body of email
29
+ # print[range] # which pages should print
30
+ # print[copies] # number of copies
31
+ #
32
+ # PRINTJOB
33
+ # id,
34
+ # account_id, # what 'system' / customer
35
+ # printer_id, # on what printer
36
+ # printed_by_id, # what id has the printed_by entity
37
+ # printed_by_type, # what entity - like user
38
+ # view_template_path, # what template
39
+ # name, # label the job
40
+ # printing_class, # what entity provides the data
41
+ # print_driver, # what 'driver' - like :pdf, :cab, :zebra, :csv, :html, etc
42
+ # print_format, # data collation - like 'record', 'list'
43
+ # state, # record the progress
44
+ # paper, # what material - like 'label32x42', 'A4', etc
45
+ # copies, # number of identical prints
46
+ # print_sql, # how to find what will be printed
47
+ # created_at,
48
+ # updated_at
49
+ #
50
+ #
14
51
 
15
52
  #
16
53
  module PrintEngine
@@ -20,24 +57,10 @@ module PrintEngine
20
57
  def self.included(base)
21
58
 
22
59
  #
23
- # find the best printer for the job
24
- def base.default_printer usr, printer_name, paper=nil
25
- if printer_name=="default"
26
- if paper.nil?
27
- printer = (usr.printers.empty? ? nil : usr.printers.active.preferred_printer.first) or raise NoPreferredPrintersFound.new('No preferred printers were found!')
28
- else
29
- printer = (usr.printers.empty? ? nil : (usr.printers.active.preferred_printer.on_paper(paper).first || usr.printers.on_paper(paper).first ) ) or raise NoPreferredPrintersFound.new('No preferred printers were found!')
30
- end
31
- else
32
- printer = Printer.active.find_by( 'name like ?', "%#{printer_name.downcase}%")
33
- end
34
- # usr.printers.where{ (printownerables.preferred==true) & (printownerables.preferred==true) }
35
-
36
- # raise PrintJobPrinterNotAvailableError
37
- printer || Printer.first
38
- rescue NoPreferredPrintersFound
39
- logit :error, 'No preferred printers found! Sending PDF by email to %s (%s) ' % [usr.name,usr.email]
40
- Printer.create paper: paper, command: 'email', name: 'tmp printer - will be deleted shortly - DONT TOUCH!!', account_id: usr.account.id
60
+ def base.print_list(options={})
61
+ options = set_print_job_defaults options[:resources], options
62
+ options[:print_job][:print_format] = options[:print][:collation] || 'list'
63
+ pj = print options[:resources], options
41
64
  end
42
65
 
43
66
  #
@@ -51,77 +74,82 @@ module PrintEngine
51
74
  # and the PrintJob will be enqueued in the delayed_job queue called 'printing'.
52
75
  # cycle_error will be called in print_job.rb if it cannot be enqueued sucessfully.
53
76
  def base.print( resources, params )
54
- begin
55
- pj = create_print_job( resources, params )
56
- return false unless pj
57
- rescue PrintJobResourceError
58
- base.logit :error, 'PrintJob could not be created - no records!?'
59
- return false
60
-
61
- rescue PrintJobPrinterNotAvailableError
62
- logit :error, 'PrintJob could not be created - the user has no printers attached!!'
63
- return false
64
-
65
- end
66
-
67
- begin
68
- if Rails.env=='development' or pj.download
69
- return pj if pj.perform params
70
- else
71
- user = User.find(pj.printed_by_id)
72
- BackgroundPrinterJob.perform_later pj, queue: "printing", account_id: user.account.id
73
- # Delayed::Job.enqueue pj, :queue => 'printing'
74
- # pj.cycle if Delayed::Job.enqueue pj, :queue => 'printing'
75
- # If you comment out above line and use below line with pj.perform instead, you thereby surpass delayed_job, and thus don't have to wait for it
76
- # pj.perform
77
- return pj
78
- end
79
- rescue => e
80
- # self.logit :error, 'PrintJob could not be enqueued'
81
- pj.update_columns( state: "could not be queued for printing - #{e}" )
82
- return nil
77
+ pj = create_print_job( resources, params )
78
+ if Rails.env=='development' or pj.download
79
+ return pj.perform params
80
+ else
81
+ user = User.find(pj.printed_by_id)
82
+ BackgroundPrinterJob.perform_later pj, queue: "printing", account_id: user.account.id
83
+ # Delayed::Job.enqueue pj, :queue => 'printing'
84
+ # pj.cycle if Delayed::Job.enqueue pj, :queue => 'printing'
85
+ # If you comment out above line and use below line with pj.perform instead, you thereby surpass delayed_job, and thus don't have to wait for it
86
+ # pj.perform
87
+ return pj
83
88
  end
84
89
  end
85
90
 
86
91
  #
87
92
  #
88
93
  def base.create_print_job( resources, params )
89
- printer_name = params.delete(:printer_name) || "default"
90
94
  params = ActionController::Parameters.new( params) unless params.class == ActionController::Parameters
91
- params = set_print_job_defaults(resources,params, printer_name)
92
- return nil if params[:print_job][:printer_id].nil?
93
- PrintJob.create( params[:print_job].permit(:download, :snap_shot, :account_id, :printer_id, :printed_by_id, :printed_by_type, :view_template_path, :name, :printing_class, :print_driver, :print_format, :paper, :copies, :state, :print_sql) )
95
+ pj=PrintJob.create( params_permit(params) )
96
+ raise PrintJobNotCreatedError.new('arguments not permitted!') unless pj
97
+ pj
98
+ end
99
+
100
+ def base.params_permit(params)
101
+ params[:print_job].permit(:download, :snap_shot, :account_id, :printer_id, :printed_by_id,
102
+ :printed_by_type, :view_template_path, :printing_class, :name, :print_driver, :print_format, :paper, :copies, :state, :print_sql)
94
103
  end
95
104
 
96
105
  # Sets print_job defaults using the provided params
97
106
  # and looks up any print_job settings to be tweaked
98
107
  # as per klass and user
99
- def base.set_print_job_defaults(resources, params, printer_name)
100
- klass = resources.first.class.to_s rescue "class not found!"
101
- params[:print_job] ||= {}
102
- params[:user] ||= User.first
103
- user = params[:user]
104
- params[:print_job][:account_id] ||= user.account.id
105
- params[:print_job][:paper] ||= nil
106
- params[:print_job][:printer_id] ||= self.default_printer(user,printer_name,params[:print_job][:paper]).id
107
- params[:print_job][:printed_by_id] = user.id
108
- params[:print_job][:printed_by_type] = user.class.to_s
109
- params[:print_job][:name] ||= "#{klass} print at #{I18n.l Time.now, format: :short_date }"
110
- params[:print_job][:printing_class] ||= klass
111
- params[:print_job][:print_driver] ||= :pdf
112
- params[:print_job][:copies] ||= 1
113
- params[:print_job][:print_format] ||= "list"
114
- params[:print_job][:state] = "drafted"
115
- params[:print_job][:snap_shot] ||= false
116
- params[:print_job][:print_sql] = set_resource_sql(resources,params)
117
-
108
+ def base.set_print_job_defaults(resources, options)
109
+ klass = self.to_s #resources.first.class.to_s rescue "class not found!"
110
+ pb = options[:printed_by]
111
+ printer_name = options[:print].delete(:printer) || "default"
112
+
113
+ options[:print_job][:printer_id] ||= self.default_printer(pb,printer_name,options[:print_job][:paper]).id
114
+ options[:print_job][:state] = "drafted"
115
+ options[:print_job][:snap_shot] ||= false
116
+ options[:print_job][:print_sql] = set_resource_sql(resources,options)
117
+ options[:print_job] ||= {}
118
+ options[:print_job][:printing_class] ||= klass
119
+ options[:print_job][:account_id] ||= pb.account.id
120
+ options[:print_job][:printed_by_id] = pb.id
121
+ options[:print_job][:printed_by_type] = pb.class.to_s
122
+ options[:print_job][:name] ||= "#{klass} print at #{I18n.l Time.now, format: :short_date }"
123
+ options[:print_job][:copies] ||= 1
124
+ options[:context] ||= ""
125
+ options[:print_job][:download] = options[:print][:medium]=="download" || false
126
+ # options[:print_job][:view_template_path] = find_template( options[:print][:template], options[:print][:paper] ) || options[:print_job][:view_template_path]
127
+ #
118
128
  # this is what we ultimately return
119
- params
129
+ options
130
+ end
131
+
132
+ #
133
+ # find the best printer for the job
134
+ def base.default_printer usr, printer_name, paper=nil
135
+ if printer_name=="default"
136
+ if paper.nil?
137
+ printer = (usr.printers.empty? ? nil : usr.printers.active.preferred_printer.first) or raise NoPreferredPrintersFound.new('No preferred printers found!')
138
+ else
139
+ printer = (usr.printers.empty? ? nil : (usr.printers.active.preferred_printer.on_paper(paper).first || usr.printers.on_paper(paper).first ) ) or raise NoPreferredPrintersFound.new('No preferred printers found!')
140
+ end
141
+ else
142
+ printer = Printer.active.find_by( 'name like ?', "%#{printer_name.downcase}%")
143
+ end
144
+ # usr.printers.where{ (printownerables.preferred==true) & (printownerables.preferred==true) }
145
+
146
+ # raise PrintJobPrinterNotAvailableError
147
+ printer || Printer.first
120
148
  end
121
149
 
122
150
  def base.set_resource_sql(resources,params)
123
151
  return resources if resources.class == String && resources.downcase =~ /select/
124
- raise PrintJobResourceError unless resources.respond_to?(:any?) and resources.any?
152
+ raise PrintJobResourceError.new('No items found to print?!') unless resources.respond_to?(:any?) and resources.any?
125
153
  params[:print_job][:snap_shot] ? resources.to_yaml : (resources.class==Array ? array_to_arel(resources) : resources.to_sql)
126
154
  end
127
155
 
@@ -132,5 +160,56 @@ module PrintEngine
132
160
  end
133
161
 
134
162
  end
163
+ #
164
+ #
165
+ # # list_title
166
+ # def list_title
167
+ # self.respond_to?( "name") ? self.name : "please define list_title on model (#{self.class.to_s})!"
168
+ # end
169
+ #
170
+ # # implement on relevant models
171
+ def find_template template, paper="A4"
172
+ raise 'you have to implement "def find_template(paper)" on your Model'
173
+ # 'label.html.haml'
174
+ end
175
+ #
176
+ #
177
+ #
178
+ # def set_print_defaults options
179
+ # options[:context] ||= ""
180
+ # options[:print_job][:download] = options[:print][:medium]=="download" || false
181
+ # options[:print_job][:print_driver] = spot_the_driver(options)
182
+ # options[:print_job][:paper] = options[:paper] || "A4"
183
+ # options[:print_job][:view_template_path] = find_template( options[:template], options[:paper] ) || params[:print_job][:view_template_path]
184
+ # options
185
+ # end
186
+ #
187
+
188
+ def print_label(options={})
189
+ options = self.class.set_print_job_defaults [self], options
190
+ options[:print_job][:print_driver] = options[:print][:print_driver] || :cab
191
+ options[:print_job][:paper] = options[:print][:paper] || "label"
192
+ options[:print_job][:print_format] = options[:print][:collation] || 'record'
193
+ options[:print_job][:view_template_path] = "stock_items/print/zebra_stock_items_label.html.haml"
194
+
195
+ pj = self.class.print [self], options
196
+ end
197
+
198
+
199
+ # options[:print][:printer_name] = options[:print][:printer_name] || ''
200
+ # options[:user] = options[:user] || current_user
201
+
202
+
203
+ def print_record(options={})
204
+ options = self.class.set_print_job_defaults [self], options
205
+ options[:print_job][:print_driver] = options[:print][:print_driver] || :pdf
206
+ options[:print_job][:paper] = options[:print][:paper] || "A4"
207
+ options[:print_job][:print_format] = options[:print][:collation] || 'record'
208
+ options[:print_job][:view_template_path] = "stock_items/print/slip.html.haml"
209
+
210
+ pj = self.class.print [self], options
211
+ end
212
+
213
+
135
214
 
136
215
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxen_printer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Walther H Diechmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-13 00:00:00.000000000 Z
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oxen_job