# encoding: UTF-8 #/* - - - - - - - - - - - - - - - - - - - - - # # Title : WEB Appliances Crest Inc, Web Form Framework # Author : Enrique Phillips # URL : http://www.wac.bz # #- - - - - - - - - - - - - - - - - - - - - */ # the PrintEngine module ... # TODO build the print_engine as a Gem # http://rarlindseysmash.com/posts/config-and-generators-in-gems # # module PrintEngine include Exceptions def self.included(base) # # find the best printer for the job def base.default_printer usr, printer_name, paper=nil if printer_name=="default" if paper.nil? printer = (usr.printers.empty? ? nil : usr.printers.active.preferred_printer.first) or raise NoPreferredPrintersFound.new('No preferred printers were found!') else 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!') end else printer = Printer.active.find_by( 'name like ?', "%#{printer_name.downcase}%") end # usr.printers.where{ (printownerables.preferred==true) & (printownerables.preferred==true) } # raise PrintJobPrinterNotAvailableError printer || Printer.first # rescue # Printer.new end # # def base.default_print_template_path(params,usr) # # TODO 2013-09-02 - select template according to the user/employee # params[:print_job][:view_template_path] || "/test.html.haml" # end # Creates the PrintJob for any model in need of printing # If creating the PrintJob is successful, cycle will be called in print_job.rb # and the PrintJob will be enqueued in the delayed_job queue called 'printing'. # cycle_error will be called in print_job.rb if it cannot be enqueued sucessfully. def base.print( resources, params ) begin pj = create_print_job( resources, params ) return false unless pj rescue PrintJobResourceError base.logit :error, 'PrintJob could not be created - no records!?' return false rescue PrintJobPrinterNotAvailableError logit :error, 'PrintJob could not be created - the user has no printers attached!!' return false end begin if Rails.env=='development' or pj.download return pj if pj.perform params else user = User.find(pj.printed_by_id) BackgroundPrinterJob.perform_later pj, queue: "printing", account_id: user.account.id # Delayed::Job.enqueue pj, :queue => 'printing' # pj.cycle if Delayed::Job.enqueue pj, :queue => 'printing' # 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 # pj.perform return pj end rescue => e # self.logit :error, 'PrintJob could not be enqueued' pj.update_columns( state: "could not be queued for printing - #{e}" ) return nil end end # # def base.create_print_job( resources, params ) printer_name = params.delete(:printer_name) || "default" params = ActionController::Parameters.new( params) unless params.class == ActionController::Parameters params = set_print_job_defaults(resources,params, printer_name) return nil if params[:print_job][:printer_id].nil? 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) ) end # Sets print_job defaults using the provided params # and looks up any print_job settings to be tweaked # as per klass and user def base.set_print_job_defaults(resources, params, printer_name) klass = resources.first.class.to_s rescue "class not found!" params[:print_job] ||= {} params[:user] ||= User.first user = params[:user] params[:print_job][:account_id] ||= user.account.id params[:print_job][:paper] ||= nil params[:print_job][:printer_id] ||= self.default_printer(user,printer_name,params[:print_job][:paper]).id params[:print_job][:printed_by_id] = user.id params[:print_job][:printed_by_type] = user.class.to_s params[:print_job][:name] ||= "#{klass} print at #{I18n.l Time.now, format: :short_date }" params[:print_job][:printing_class] ||= klass params[:print_job][:print_driver] ||= :pdf params[:print_job][:copies] ||= 1 params[:print_job][:print_format] ||= "list" params[:print_job][:state] = "drafted" params[:print_job][:snap_shot] ||= false params[:print_job][:print_sql] = set_resource_sql(resources,params) # this is what we ultimately return params end def base.set_resource_sql(resources,params) return resources if resources.class == String && resources.downcase =~ /select/ raise PrintJobResourceError unless resources.respond_to?(:any?) and resources.any? params[:print_job][:snap_shot] ? resources.to_yaml : (resources.class==Array ? array_to_arel(resources) : resources.to_sql) end def base.array_to_arel resources arel_instance = Arel::Table.new(resources.first.class.table_name) ar_rel = resources.first.class ar_rel.where(arel_instance[:id].in(resources.map(&:id)).to_sql).to_sql end end end