Class: PrintProfile

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

Overview

== Schema Information

Table name: print_profiles
Database name: primary

id :integer not null, primary key
bin :string
duplex :string
fit_to_page :boolean
job_type :integer
name :string not null
paper :string
rotate :integer
employee_record_id :integer
printer_id :integer not null

Indexes

index_print_profiles_on_employee_record_id (employee_record_id)
index_print_profiles_on_printer_id (printer_id)

Foreign Keys

fk_rails_... (employee_record_id => employee_records.id)
fk_rails_... (printer_id => printers.id)

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 Models::EventPublishable

#publish_event

Class Method Details

.in_store_idActiveRecord::Relation<PrintProfile>

A relation of PrintProfiles that are in store id. Active Record Scope

Returns:

See Also:



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

scope :in_store_id, -> (store_id) { joins(:printer).where(printers: { store_id: store_id })}

.onlineActiveRecord::Relation<PrintProfile>

A relation of PrintProfiles that are online. Active Record Scope

Returns:

See Also:



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

scope :online, -> { joins(:printer).where(printers: { state: 'online' }) }

.select_optionsObject



40
41
42
# File 'app/models/print_profile.rb', line 40

def self.select_options
  all.eager_load(:printer).order(Printer[:name], :name).map{|pp| ["#{pp.printer.name} > #{pp.name}", pp.id]}
end

.warehouseActiveRecord::Relation<PrintProfile>

A relation of PrintProfiles that are warehouse. Active Record Scope

Returns:

See Also:



33
# File 'app/models/print_profile.rb', line 33

scope :warehouse, -> { where(job_type: %w(shipping_document shipping_label upc_label)) }

Instance Method Details

#bin_options_for_selectObject



53
54
55
# File 'app/models/print_profile.rb', line 53

def bin_options_for_select
  [['Default','']] + printer.bins.map{|b| [b,b]}
end

#duplex_options_for_selectObject



48
49
50
51
# File 'app/models/print_profile.rb', line 48

def duplex_options_for_select
  return [] unless printer.duplex?
  [['Default','']] + ['long-edge','short-edge'].map{|o| [o.titleize, o]}
end

#employee_recordEmployeeRecord



29
# File 'app/models/print_profile.rb', line 29

belongs_to :employee_record, optional: true

#job_type_for_selectObject



57
58
59
# File 'app/models/print_profile.rb', line 57

def job_type_for_select
  [['Default', '']] + PrintProfile.job_types.keys.map{|k| [k.titleize, k]}
end

#paper_options_for_selectObject



44
45
46
# File 'app/models/print_profile.rb', line 44

def paper_options_for_select
  printer.capabilities.dig('papers','table')&.keys || []
end


65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/print_profile.rb', line 65

def print(content, content_type: :pdf_base64, quantity: 1)
  #https://www.printnode.com/en/docs/api/curl#printjobs
  # normally printer should listen to the copies option but with dymo label that's not the case
  print_options = to_print_options #.merge(copies: quantity.to_i)
  payload = {
    options: print_options,
    expireAfter: 120000,
    qty: quantity.to_i
  }
  printer.print(content, content_type: content_type, payload: payload)

end


61
62
63
# File 'app/models/print_profile.rb', line 61

def print_upload(upload, quantity: 1)
  printer.print_upload(upload, bin: bin, duplex: duplex, quantity: quantity, rotate: rotate, fit_to_page: fit_to_page)
end

#printerPrinter

Returns:

See Also:



28
# File 'app/models/print_profile.rb', line 28

belongs_to :printer, optional: true

#to_print_optionsObject



78
79
80
81
82
83
84
85
86
# File 'app/models/print_profile.rb', line 78

def to_print_options
  {
    bin: bin.presence,
    duplex: duplex.presence,
    rotate: rotate.presence,
    fit_to_page: fit_to_page.to_b,
    paper: paper.presence
  }.compact
end

#to_sObject



88
89
90
# File 'app/models/print_profile.rb', line 88

def to_s
  "#{printer.computer_name} > #{printer.name} > #{name}"
end