Class: Plus2Seeder::Seeder::Spreadsheet

Inherits:
Base
  • Object
show all
Defined in:
lib/plus2_seeder/seeder/spreadsheet.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after, #before, creator_class, #creator_class, creator_class=, #debug, #debug?, dependencies, #reset, #run, #run_dependencies

Class Method Details

.columns(columns = nil) ⇒ Object

The names of the columns - use the active record attribute names. These will be zipped up with the row values by row_to_hash to create an attributes hash that can be used to create a new ActiveRecord instance.

To skip a column use ‘_’, it will be deleted from the resulting hash



38
39
40
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 38

def self.columns(columns=nil)
  @columns ||= columns
end

.sheet(sheetname = nil) ⇒ Object

The sheet name to import from



28
29
30
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 28

def self.sheet(sheetname=nil)
  @sheetname ||= sheetname
end

.skip_rows(rows = nil) ⇒ Object

Skip (n) rows before starting to import



44
45
46
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 44

def self.skip_rows(rows=nil)
  @skip_rows ||= rows || 0
end

.source(*filenames) ⇒ Object

Used to specify the spreadsheet to import. Assumed to be in db/seeds



22
23
24
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 22

def self.source(*filenames)
  @filenames ||= filenames
end

Instance Method Details

#can_import?(row) ⇒ Boolean

Determines whether or not the row can be imported. Override this in your subclass if desired

Returns:

  • (Boolean)


89
90
91
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 89

def can_import?(row)
  true
end

#import(row) ⇒ Object

Imports the row



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 75

def import(row)
  if can_import?(row)
    if object = object_available_to_update(row)
      debug("Updating #{object}")
      object.update_attributes(row)
    else
      debug("Creating new #{creator_class.name}")
      creator_class.create!(row)
    end
  end
end

#object_available_to_update(row) ⇒ Object

Override this in your subclass if you wish to update existing data from your seeder. It will need to return an instance of the AR object to be updated



96
97
98
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 96

def object_available_to_update(row)
  nil
end

#row_to_hash(row) ⇒ Object

Creates a hash from the row values and column names



64
65
66
67
68
69
70
71
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 64

def row_to_hash(row)
  Hash[self.class.columns.zip(row)].tap do |h|
    pre_process(h, row) if respond_to?(:pre_process)

    # Delete skipped columns
    h.delete('_')
  end
end

#seed_from_spreadsheetObject

Opens the spreadsheet and calls import for each row



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/plus2_seeder/seeder/spreadsheet.rb', line 50

def seed_from_spreadsheet
  self.class.source.each do |source|
    book = ::Spreadsheet.open source

    sheet = book.worksheet self.class.sheet

    sheet.each self.class.skip_rows do |row|
      import(row_to_hash(row))
    end
  end
end