Class: Gemgento::Import

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gemgento/import.rb

Direct Known Subclasses

ImageImport, InventoryImport, ProductImport

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#header_rowObject

Returns the value of attribute header_row.



24
25
26
# File 'app/models/gemgento/import.rb', line 24

def header_row
  @header_row
end

#rowObject

Returns the value of attribute row.



24
25
26
# File 'app/models/gemgento/import.rb', line 24

def row
  @row
end

#spreadsheetObject

Returns the value of attribute spreadsheet.



24
25
26
# File 'app/models/gemgento/import.rb', line 24

def spreadsheet
  @spreadsheet
end

Instance Method Details

#content_index_rangeRange

Returns:

  • (Range)


106
107
108
# File 'app/models/gemgento/import.rb', line 106

def content_index_range
  ((spreadsheet.first_row + 1)..spreadsheet.last_row)
end

#default_optionsObject



47
48
49
# File 'app/models/gemgento/import.rb', line 47

def default_options
  {}
end

#percentage_completeObject



55
56
57
# File 'app/models/gemgento/import.rb', line 55

def percentage_complete
  total_rows > 0 ? ((current_row.to_f / total_rows.to_f) * 100.0) : 0.0
end

#processObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/gemgento/import.rb', line 73

def process
  Rails.logger.debug "Start #{self.class}.process"
  Rails.logger.debug "  header_row: #{self.header_row}"

  self.current_row = 0
  self.processing!

  while self.current_row < self.total_rows do
    self.current_row += 1

    self.row = spreadsheet.row(self.current_row + spreadsheet.first_row)
    Rails.logger.debug "  Working on Row #{self.current_row} - #{self.row}"

    self.process_row
    self.save!
  end

  self.complete!

  Rails.logger.debug "Complete #{self.class}.process"

rescue Exception => e
  # don't raise so that we have a record of the import and don't cause stacking with retries
  self.process_errors << e.message
  self.failed!
  Rails.logger.error e
end

#process_laterObject



101
102
103
# File 'app/models/gemgento/import.rb', line 101

def process_later
  Gemgento::ImportJob.perform_later(self)
end

#set_default_optionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/gemgento/import.rb', line 33

def set_default_options
  self.options = default_options.merge(self.options || {})

  self.options.keys.each do |key|
    self.class.send :define_method, key do |*args|
      self.options[key.to_sym]
    end

    self.class.send :define_method, "#{key}=" do |*args|
      self.options[key.to_sym] = args.first
    end
  end
end

#set_default_process_errorsObject



29
30
31
# File 'app/models/gemgento/import.rb', line 29

def set_default_process_errors
  self.process_errors ||= []
end

#set_total_rowsObject



51
52
53
# File 'app/models/gemgento/import.rb', line 51

def set_total_rows
  self.total_rows = spreadsheet.last_row - spreadsheet.first_row
end

#value(code, type = :string) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/gemgento/import.rb', line 110

def value(code, type = :string)
  index = header_row.index(code)
  value = row[index]

  case type
    when :boolean
      return value.to_bool
    when :string
      return value.to_s.strip
    else
      return value
  end
end