Class: Printer

Inherits:
ApplicationRecord show all
Defined in:
app/models/printer.rb

Overview

== Schema Information

Table name: printers
Database name: primary

id :integer not null, primary key
capabilities :jsonb
computer_name :string not null
name :string not null
state :string not null
created_at :datetime not null
updated_at :datetime not null
print_node_id :integer not null
store_id :integer

Indexes

index_printers_on_print_node_id (print_node_id)

Constant Summary

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Has many collapse

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details



45
46
47
48
49
# File 'app/models/printer.rb', line 45

def self.print_node_client
  auth_key = Heatwave::Configuration.fetch(:print_node_auth_key)
  auth = PrintNode::Auth.new(auth_key)
  PrintNode::Client.new(auth)
end

.refresh_printer_listObject



41
42
43
# File 'app/models/printer.rb', line 41

def self.refresh_printer_list
  Printers::RefreshPrinters.new.process
end

.select_optionsObject



37
38
39
# File 'app/models/printer.rb', line 37

def self.select_options
  order(:name).pluck(:name, :id)
end

Instance Method Details

#binsObject



51
52
53
# File 'app/models/printer.rb', line 51

def bins
  capabilities['bins']
end

#collate?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/printer.rb', line 59

def collate?
  capabilities['collate'].to_b
end

#color?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/printer.rb', line 63

def color?
  capabilities['color'].to_b
end

#duplex?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/printer.rb', line 67

def duplex?
  capabilities['duplex'].to_b
end

#full_name_arrayObject



71
72
73
# File 'app/models/printer.rb', line 71

def full_name_array
  [print_node_id, store&.name, computer_name, name, state].filter_map(&:presence)
end

#mediasObject



55
56
57
# File 'app/models/printer.rb', line 55

def medias
  capabilities['medias']
end

see https://www.printnode.com/docs/api/curl/
title: A title to give the PrintJob. This is the name which will appear in the operating system's print queue.
content_type: Either 'pdf_uri', 'pdf_base64', 'raw_uri', 'raw_base64'
content: A uri accessible by the client when contentType is 'pdf_uri'.
or A base64 encoded representation of the pdf when contentType is 'pdf_base64'.
source: A text description of how the printjob was created or where the printjob originated.
options: An object describing various options which can be set for this PrintJob. See options. Printing options have no effect when raw printing.
https://github.com/PrintNode/PrintNode-Ruby/blob/master/examples/creating_a_printjob.rb



83
84
85
86
87
88
# File 'app/models/printer.rb', line 83

def print(content, content_type: :pdf_uri, name: nil, source: nil, payload: {})
  # raise "Invalid pdf_uri" if content_type != :pdf_uri && (URI.parse(content) rescue nil).nil?
  source ||= "heatwave"
  print_job = PrintNode::PrintJob.new(print_node_id, name, content_type.to_s, content, source)
  self.class.print_node_client.create_printjob(print_job, payload)
end

Returns:

See Also:



34
# File 'app/models/printer.rb', line 34

has_many :print_profiles, dependent: :destroy


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/printer.rb', line 90

def print_upload(upload, bin: nil, quantity: 1, duplex: 'long-edge', rotate: nil, fit_to_page: nil)
  t = upload.active_download_token
  pdf_uri = "https://#{CRM_HOSTNAME}/en-US/downloads/#{t}"
  # pdf_data = upload.attachment.data
  options = {}
  options['bin'] = bin if bin.present?
  options['duplex'] = duplex if duplex.present?
  options['rotate'] = rotate if rotate.present?
  options['fit_to_page'] = fit_to_page unless fit_to_page.nil?
  options['copies'] = quantity

  payload = {
    options: options,
    expireAfter: 120_000
  }
  ext = File.extname(upload.attachment_name)
  name = "#{upload.category}_#{upload.id}#{ext}"
  print(pdf_uri, name: name, payload: payload, content_type: :pdf_uri)
end

#storeStore

Returns:

See Also:



35
# File 'app/models/printer.rb', line 35

belongs_to :store, optional: true

#to_partial_pathObject

Partial lives under app/views/crm/printers/, not the conventional
app/views/printers/. Override so render @printers (or
render printer) resolves from any controller, not just
Crm::PrintersController.



28
29
30
# File 'app/models/printer.rb', line 28

def to_partial_path
  'crm/printers/printer'
end