Skip to content

Instantly share code, notes, and snippets.

@jraczak
Created July 31, 2014 20:38
Show Gist options
  • Save jraczak/bc33d5bb8ccc2d6f3463 to your computer and use it in GitHub Desktop.
Save jraczak/bc33d5bb8ccc2d6f3463 to your computer and use it in GitHub Desktop.
#import.rb (model)
def initialize(options)
#attributes.each { |name, value| send("#{name}=", value) }
@file = options[:file]
end
def save
if file.present?
cleanup_before_save
@to_process = CSV.parse(file.tempfile, row_sep: "\n", headers: true)
process_files!
else
errors.add :csv_file, "can't be blank"
end
rescue ArgumentError, CSV::MalformedCSVError => e
errors.add :csv_file, "is not valid"
end
def process_files!
@to_process.each do |row|
if Venue.exists?(Venue.find_by_factual_id(row['factual_id']))
venue = update_venue_from_csv(row)
if venue.changed?
venue.save!
@updated_venues << venue
else
@existing_venues += 1
end
else
venue = create_venue_from_csv(row)
if venue.new_record?
@unsaved_venues << venue
else
@saved_venues << venue
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment