class SmartImporter::Spreadsheet
Public Class Methods
new(sheet:, model:, key_attribute:)
click to toggle source
# File lib/smart_importer/spreadsheet.rb, line 3 def initialize(sheet:, model:, key_attribute:) @sheet = sheet @key_attribute = key_attribute @number_imports = 0 @looking_at_row = 1 @model = model end
Public Instance Methods
import_objects(added_attributes: {})
click to toggle source
# File lib/smart_importer/spreadsheet.rb, line 11 def import_objects(added_attributes: {}) ((right_header_row + 1)..@sheet.last_row).each do |i| unless @sheet.row(i).empty? row = Hash[[@sheet.row(right_header_row), @sheet.row(i)].transpose] if active_record = @model.where(name: row['name']).try(:first) active_record.update(valid_attributes(row)) else @model.create(valid_attributes(row)) end end @number_imports += 1 end @number_imports end
Private Instance Methods
right_header_row()
click to toggle source
# File lib/smart_importer/spreadsheet.rb, line 28 def right_header_row until valid_header_line? @looking_at_row += 1 raise "#{@sheet.last_row} sheet does not appear to have any relevant information." if sheet_is_invalid? end @looking_at_row end
sheet_is_invalid?()
click to toggle source
# File lib/smart_importer/spreadsheet.rb, line 36 def sheet_is_invalid? @looking_at_row == 10 end
valid_attributes(row)
click to toggle source
# File lib/smart_importer/spreadsheet.rb, line 44 def valid_attributes(row) formatted_row = {} row.each {|key, value| formatted_row[key.downcase.gsub(' ', '_').to_sym] = value if key.present? } attribute_keys = @model.column_names.collect(&:to_sym) & formatted_row.keys formatted_row.with_indifferent_access.slice(*attribute_keys) || {} end
valid_header_line?()
click to toggle source
# File lib/smart_importer/spreadsheet.rb, line 40 def valid_header_line? @sheet.row(@looking_at_row).any?{|cell| @model.column_names.include?(cell.to_s.underscore.try(:to_sym))} end