Created
July 31, 2014 20:38
-
-
Save jraczak/bc33d5bb8ccc2d6f3463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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