Class: SpreeCmCommissioner::Exports::ExportGuestCsvService

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(export_id) ⇒ ExportGuestCsvService

Returns a new instance of ExportGuestCsvService.



6
7
8
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 6

def initialize(export_id)
  @export_id = export_id
end

Instance Attribute Details

#export_idObject (readonly)

Returns the value of attribute export_id.



4
5
6
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 4

def export_id
  @export_id
end

Instance Method Details

#attach_csv_fileObject



35
36
37
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 35

def attach_csv_file
  export.exported_file.attach(io: File.open(export.file_path), filename: export.file_name)
end

#callObject



10
11
12
13
14
15
16
17
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 10

def call
  update_export_status_when_start
  generate_csv_file
  update_export_status_when_finish(:done)
rescue StandardError => e
  update_export_status_when_finish(:failed)
  raise e
end

#exportObject



19
20
21
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 19

def export
  @export ||= SpreeCmCommissioner::Exports::ExportGuestCsv.find(export_id)
end

#generate_csv_fileObject



23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 23

def generate_csv_file
  CSV.open(export.file_path, 'w') do |csv|
    csv << export.construct_header

    export.scope.find_each do |resource|
      csv << export.construct_row(resource)
    end
  end

  attach_csv_file
end

#update_export_status_when_finish(status) ⇒ Object



43
44
45
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 43

def update_export_status_when_finish(status)
  export.update(status: status, finished_at: Time.zone.now)
end

#update_export_status_when_startObject



39
40
41
# File 'app/services/spree_cm_commissioner/exports/export_guest_csv_service.rb', line 39

def update_export_status_when_start
  export.update(status: :progress, started_at: Time.zone.now)
end