class PeopleFile

Excel or text file of People. Assumes that the first row is a header row. Updates membership to current year. If there are no more events in the current year, updates membership to next year. See trac.butlerpress.com/racing_on_rails/wiki/SampleImportFiles for format details and examples.

Constants

COLUMN_MAP

‘club’ …this is often team in USAC download. How handle? Use club for team if no team? and if both, ignore club?

'NCCA club' ...can have this in addition to club and team. should team be many to many?

Attributes

created[R]
duplicates[R]
updated[R]

Public Class Methods

new(source, *options) click to toggle source
# File app/models/people_file.rb, line 100
def initialize(source, *options)
  if options.empty?
    options = Hash.new
  else
    options = options.first
  end
  options = {
    :delimiter => ',',
    :quoted => true,
    :header_row => true,
    :row_class => Person,
    :column_map => COLUMN_MAP
  }.merge(options)

  super(source, options)

  @created = 0
  @updated = 0
  @duplicates = []
end

Public Instance Methods

import(update_membership, year = nil) click to toggle source

year for RaceNumbers New memberships start on today, but really should start on January 1st of next year, if year is next year

# File app/models/people_file.rb, line 123
def import(update_membership, year = nil)
  logger.debug("PeopleFile import update_membership: #{update_membership}")
  @update_membership = update_membership
  @has_print_column = columns.any? do |column|
    column.field == :print_card
  end
  year = year.to_i if year
  
  logger.debug("#{rows.size} rows")
  created = 0
  updated = 0
  if @update_membership
    if year && year > Time.zone.today.year
      @member_from_imported_people = Date.new(year, 1, 1)
    else
      @member_from_imported_people = Time.zone.today
    end
    @member_to_for_imported_people = Date.new(year || Time.zone.today.year, 12, 31)
  end
  
  Person.transaction do
    begin
      rows.each do |row|
        row_hash = row.to_hash
        row_hash[:year] = year if year
        row_hash[:updated_by] = "Membership import: #{import_file.name}"
        logger.debug(row_hash.inspect) if logger.debug?
        next if row_hash[:first_name].blank? && row_hash[:first_name].blank? && row_hash[:name].blank?
        
        combine_categories(row_hash)
        row_hash.delete(:date_of_birth) if row_hash[:date_of_birth] == 'xx'

        # TODO or by USAC license number
        people = []

        if row_hash[:license].present?
          people = Person.find_all_by_license(row_hash[:license])
        end

        if people.empty?
          people = Person.find_all_by_name_or_alias(:first_name => row_hash[:first_name], :last_name => row_hash[:last_name])
        end
        
        person = nil
        row_hash[:member_to] = @member_to_for_imported_people if @update_membership
        if people.empty?
          delete_unwanted_member_from(row_hash, person)
          add_print_card_and_label(row_hash)
          row_hash[:created_by] = import_file
          person = Person.new(row_hash)
          person.save!
          @created = @created + 1
        elsif people.size == 1
          # Don't want to overwrite existing categories
          delete_blank_categories(row_hash)
          person = people.first
          delete_unwanted_member_from(row_hash, person)
          unless person.notes.blank?
            row_hash[:notes] = "#{people.last.notes}#{$INPUT_RECORD_SEPARATOR}#{row_hash[:notes]}"
          end
          add_print_card_and_label(row_hash, person)
          
          person = Person.update(people.last.id, row_hash)
          unless person.valid?
            raise ActiveRecord::RecordNotSaved.new(person.errors.full_messages.join(', '))
          end
          @updated = @updated + 1
        else
          logger.warn("PeopleFile Found #{people.size} people for '#{row_hash[:first_name]} #{row_hash[:last_name]}'") 
          delete_blank_categories(row_hash)
          person = Person.new(row_hash)
          delete_unwanted_member_from(row_hash, person)
          unless person.notes.blank?
            row_hash[:notes] = "#{people.last.notes}#{$INPUT_RECORD_SEPARATOR}#{row_hash[:notes]}"
          end
          add_print_card_and_label(row_hash, person)
          duplicates << Duplicate.create!(:new_attributes => row_hash, :people => people)
        end
      end
    rescue Exception => e
      logger.error("PeopleFile #{e}")
      raise
    end
  end
  return @created, @updated
end