Class: OrderProcessor

Inherits:
Struct
  • Object
show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/job/order_processor.rb

Overview

This class manages all the patron-related things we need to do after an order is placed

  • Create donation, purchase actions

  • Process memberships

  • Send confirmation emails if skip_email is true

options can include:

:skip_actions. If set to true, no actions will be generated for this action
:skip_email.   If set to true, no email confirmation will be sent

Direct Known Subclasses

RefundOrderProcessor

Constant Summary collapse

QUEUE =
"order"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, options = {}) ⇒ OrderProcessor

Returns a new instance of OrderProcessor.



16
17
18
19
# File 'app/models/job/order_processor.rb', line 16

def initialize(order, options = {})
  self.options  = options
  self.order    = order
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



12
13
14
# File 'app/models/job/order_processor.rb', line 12

def options
  @options
end

#orderObject

Returns the value of attribute order

Returns:

  • (Object)

    the current value of order



12
13
14
# File 'app/models/job/order_processor.rb', line 12

def order
  @order
end

Class Method Details

.process(order, options = {}) ⇒ Object



21
22
23
24
# File 'app/models/job/order_processor.rb', line 21

def self.process(order, options = {})
  job = order.processor_class.new(order, options)
  Delayed::Job.enqueue job, :queue => QUEUE
end

Instance Method Details

#generate_pdfObject



87
88
89
90
91
92
93
# File 'app/models/job/order_processor.rb', line 87

def generate_pdf
  pdf = PdfGeneration.new(order).generate
  file = Tempfile.new(["#{order.id}", '.pdf'])
  file << pdf.force_encoding("UTF-8")
  order.pdf = file
  order.save
end

#generate_qr_codes_and_pdfObject



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/job/order_processor.rb', line 38

def generate_qr_codes_and_pdf
  begin
    order.tickets.each do |item|
      item.product.generate_qr_code_without_delay
      generate_pdf if order.organization.can? :access, :scannable_tickets
    end
  rescue Exception => e
    Exceptional.context(:order_id => order.id)
    Exceptional.handle(e, "Could not generate PDF for order")
  end
end

#performObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/job/order_processor.rb', line 26

def perform
  ActiveRecord::Base.transaction do
    self.order.create_donation_actions unless skip_actions
    self.order.create_purchase_action  unless skip_actions
    process_memberships
    process_passes
  end

  generate_qr_codes_and_pdf
  send_confirmation
end

#process_membershipsObject



67
68
69
70
71
72
73
74
# File 'app/models/job/order_processor.rb', line 67

def process_memberships
  unless self.order.memberships.empty?
    self.order.memberships.each do |membership_item|
      Member.for(membership_item.product, self.order.person)
    end
    self.order.create_generic_action("memberships")
  end
end

#process_passesObject



76
77
78
79
80
81
82
83
84
85
# File 'app/models/job/order_processor.rb', line 76

def process_passes
  unless self.order.passes.empty? || order.is_a?(RefundOrder)
    self.order.passes.each do |pass_item|
      pass_item.product.person = self.order.person
      pass_item.product.save
    end
    self.order.create_generic_action("passes")
    PassMailer.pass_info_for(self.order.person, self.order.organization.email,self.order.passes.collect(&:product)).deliver
  end
end

#send_confirmationObject



50
51
52
53
54
55
56
57
# File 'app/models/job/order_processor.rb', line 50

def send_confirmation
  begin
    OrderMailer.confirmation_for(order).deliver unless skip_email
  rescue Exception => e
    Exceptional.context(:order_id => order.id)
    Exceptional.handle(e, "Could not send order confirmation for order")
  end
end

#skip_actionsObject



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

def skip_actions
  self.options[:skip_actions] == true
end

#skip_emailObject



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

def skip_email
  self.options[:skip_email] == true
end