Class: Worker::Export

Inherits:
Base
  • Object
show all
Defined in:
lib/spree_batch_capture/worker/export.rb

Class Method Summary collapse

Class Method Details

.after_run(success, options) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/spree_batch_capture/worker/export.rb', line 75

def self.after_run(success, options)
  if success && options[:mark_as_shipped]
    options[:successful_orders].each do |order_id|
      Worker::Ship.enqueue({ :order_id => order_id })
    end
  end
  return true
end

.default_queue_nameObject



109
110
111
# File 'lib/spree_batch_capture/worker/export.rb', line 109

def self.default_queue_name
  return "export"
end

.filename_prefixObject

Allows overriding of the default filename prefix. All filenames are as follows: prefix_sha1.csv



117
118
119
# File 'lib/spree_batch_capture/worker/export.rb', line 117

def self.filename_prefix
  "export"
end

.get_orders(order_criteria) ⇒ Object

Gets the orders based on a set of params. see run for more information



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spree_batch_capture/worker/export.rb', line 86

def self.get_orders(order_criteria)
  orders = []
  order_criteria.symbolize_keys!
  if order_criteria[:scope]
    orders = ScopeSerializer.to_scope(order_criteria[:scope])
  elsif order_criteria[:orders]
    orders = Order.where("id in (?)", order_criteria[:orders])
  elsif order_criteria[:where]
    orders = Order.where(order_criteria[:where])
  elsif order_criteria[:sql]
    orders = Order.find_by_sql(order_criteria[:sql])
  else
    return false
  end

  return orders
  
end

.run(options) ⇒ Object

expected options: :orders :user_id :requested_at :mark_as_shipped (optional, defaults to false)

:orders is a hash that can be any of the following => {:joins => [], :where => [], … ## There is a helper for this (see self.from_scope and self.to_scope) => [] ## Array of ids => [] ## Conditional where clause => “select * from orders” ## Any valid sql If more than one key is specified, only the first will be used in the order above.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spree_batch_capture/worker/export.rb', line 19

def self.run(options)
  Rails.logger.debug "Starting Export Worker..."

  orders = get_orders(options[:orders])

  default_attrs = {
    :requested_at => options[:requested_at],
    :user_id => options[:user_id]
  }

  if orders && orders.count > 0
    export_file = export(orders, Formatter::SimpleCsv)
    Rails.logger.debug "Exported orders to #{export_file}"
    begin
      oe = OrderExport.create(default_attrs.merge({
        :count_of_orders => orders.count
      }))

      if export_file
        oe.exported_orders = File.open(export_file)
      else
        oe.export_errors = I18n.t(:unable_to_export)
        oe.count_of_orders = 0
      end

      oe.completed_at = Time.now
      oe.save!

      options[:successful_orders] = orders.map(&:id) if options[:mark_as_shipped]

    rescue => e
      Rails.logger.error "Error creating order export: #{e.message}" 
      if oe
        oe.count_of_orders = 0
        oe.export_file = nil
        oe.export_errors = I18n.t(:unable_to_export)
        oe.completed_at = Time.now
        oe.save!
      end
    ensure
      Rails.logger.debug "DELETING FILE: #{export_file}"
      FileUtils.rm(export_file) if export_file && File.exist?(export_file)
    end

  else
    OrderExport.create(default_attrs.merge({
      :count_of_orders => 0,
      :export_errors => I18n.t(:no_orders_for_export)
    }))

  end

  return true
end

.temp_directoryObject

Temporary directory used for creating the export files. Can be overridden to provide a custom path



123
124
125
# File 'lib/spree_batch_capture/worker/export.rb', line 123

def self.temp_directory
  File.join(Rails.root.to_s, 'tmp', 'export')
end

.worker_klass_nameObject



105
106
107
# File 'lib/spree_batch_capture/worker/export.rb', line 105

def self.worker_klass_name
  return "Export"
end