Class: Importable::Spreadsheet

Inherits:
Importer
  • Object
show all
Defined in:
app/models/importable/spreadsheet.rb

Instance Attribute Summary

Attributes inherited from Importer

#import_params

Attributes included from MultiStep::ImportHelpers

#current_step

Instance Method Summary collapse

Methods inherited from Importer

#import!, #invalid_items, #mapper, #mapper_class, #mapper_name_with_module, #plural_mapper_class, #singular_mapper_class

Methods included from MultiStep::ImportHelpers

#first_step?, #last_step?, #next_step, #previous_step, #steps

Instance Method Details

#default_sheetObject



18
19
20
# File 'app/models/importable/spreadsheet.rb', line 18

def default_sheet
  spreadsheet.default_sheet
end

#default_sheet=(sheet) ⇒ Object



13
14
15
16
# File 'app/models/importable/spreadsheet.rb', line 13

def default_sheet=(sheet)
  @imported_items_ready = true
  spreadsheet.default_sheet = sheet
end

#headersObject



9
10
11
# File 'app/models/importable/spreadsheet.rb', line 9

def headers
  @headers ||= spreadsheet.row(first_row)
end

#imported_items_ready?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/importable/spreadsheet.rb', line 32

def imported_items_ready?
  @imported_items_ready or (file.current_path and sheets.length == 1)
end

#rowsObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/importable/spreadsheet.rb', line 36

def rows
  @rows ||= begin
    (first_row + 1).upto(last_row).map do |index|
      data = {}
      raw_data = row(index)
      headers.zip(raw_data) do |key, val|
        data[key] = val
      end
      data
    end
  end
end

#spreadsheetObject



22
23
24
# File 'app/models/importable/spreadsheet.rb', line 22

def spreadsheet
  @spreadsheet ||= spreadsheet_class.new(file.current_path)
end

#spreadsheet_classObject



26
27
28
29
30
# File 'app/models/importable/spreadsheet.rb', line 26

def spreadsheet_class
  extension = file.current_path.split('.').last
  return Excel if extension == 'xls'
  return Excelx if extension == 'xlsx'
end