Class: CheckoutProcessor

Inherits:
Struct
  • Object
show all
Defined in:
app/models/job/checkout_processor.rb

Overview

This class excapsulate asynchrnous things we can do during or immediately following checkout usually involving the payment or customer objects (which aren’t available after checkout) Examples: Updating a patron phone or address

This class is not for order-related processing things. See OrderProcessor for that

This class WILL NOT RUN ASYNC in test envs

Constant Summary collapse

QUEUE =
"checkout"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(person, customer, address, phone, time_zone, checkout_name = "checkout") ⇒ CheckoutProcessor

Returns a new instance of CheckoutProcessor.



13
14
15
16
17
18
19
20
21
# File 'app/models/job/checkout_processor.rb', line 13

def initialize(person, customer, address, phone, time_zone, checkout_name = "checkout")
  self.person         = person
  self.first_name     = customer.first_name
  self.last_name      = customer.last_name
  self.address_hash   = address.attributes.to_options unless address.nil?
  self.phone          = phone
  self.time_zone      = time_zone
  self.checkout_name  = checkout_name
end

Instance Attribute Details

#address_hashObject

Returns the value of attribute address_hash

Returns:

  • (Object)

    the current value of address_hash



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def address_hash
  @address_hash
end

#checkout_nameObject

Returns the value of attribute checkout_name

Returns:

  • (Object)

    the current value of checkout_name



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def checkout_name
  @checkout_name
end

#first_nameObject

Returns the value of attribute first_name

Returns:

  • (Object)

    the current value of first_name



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name

Returns:

  • (Object)

    the current value of last_name



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def last_name
  @last_name
end

#personObject

Returns the value of attribute person

Returns:

  • (Object)

    the current value of person



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def person
  @person
end

#phoneObject

Returns the value of attribute phone

Returns:

  • (Object)

    the current value of phone



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def phone
  @phone
end

#time_zoneObject

Returns the value of attribute time_zone

Returns:

  • (Object)

    the current value of time_zone



10
11
12
# File 'app/models/job/checkout_processor.rb', line 10

def time_zone
  @time_zone
end

Class Method Details

.process(person, customer, address, phone, time_zone, checkout_name) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'app/models/job/checkout_processor.rb', line 23

def self.process(person, customer, address, phone, time_zone, checkout_name)
  job = CheckoutProcessor.new(person, customer, address, phone, time_zone, checkout_name)
  
  if job.run_now?
    job.perform
  else
    Delayed::Job.enqueue job, :queue => QUEUE
  end
end

Instance Method Details

#address_exists?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/job/checkout_processor.rb', line 43

def address_exists?
  self.address_hash.present? && self.address_hash.values.select{|values| values.present?}.any?
end

#performObject



33
34
35
36
37
# File 'app/models/job/checkout_processor.rb', line 33

def perform
  self.person.update_name(self.first_name, self.last_name)
  self.person.update_address(self.address_hash, self.time_zone, nil, checkout_name) if address_exists?
  self.person.add_phone_if_missing(self.phone)
end

#run_now?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/job/checkout_processor.rb', line 39

def run_now?
  Rails.env.test?
end