Class: EventImportFile
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EventImportFile
- Includes:
- ImportFile
- Defined in:
- app/models/event_import_file.rb
Class Method Summary collapse
Instance Method Summary collapse
Methods included from ImportFile
Class Method Details
.import ⇒ Object
156 157 158 159 160 161 162 |
# File 'app/models/event_import_file.rb', line 156 def self.import EventImportFile.not_imported.each do |file| file.import_start end rescue Rails.logger.info "#{Time.zone.now} importing events failed!" end |
Instance Method Details
#import ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/event_import_file.rb', line 56 def import self.reload num = {:imported => 0, :failed => 0} rows = open_import_file check_field(rows.first) row_num = 2 rows.each do |row| next if row['dummy'].to_s.strip.present? import_result = EventImportResult.create!(:event_import_file => self, :body => row.fields.join("\t")) event = Event.new event.name = row['name'].to_s.strip event.note = row['note'] event.start_at = row['start_at'] event.end_at = row['end_at'] category = row['category'].to_s.strip if row['all_day'].to_s.strip.downcase == 'false' event.all_day = false else event.all_day = true end library = Library.where(:name => row['library']).first library = Library.web if library.blank? event.library = library event_category = EventCategory.where(:name => category).first || EventCategory.where(:name => 'unknown').first event.event_category = event_category if event.save! import_result.event = event num[:imported] += 1 if row_num % 50 == 0 Sunspot.commit GC.start end end import_result.save! row_num += 1 end Sunspot.commit rows.close sm_complete! return num rescue => e self. = "line #{row_num}: #{e.}" sm_fail! raise e end |
#import_start ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/event_import_file.rb', line 41 def import_start executed_at = Time.zone.now sm_start! case edit_mode when 'create' import when 'update' modify when 'destroy' remove else import end end |
#modify ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/models/event_import_file.rb', line 105 def modify sm_start! rows = open_import_file check_field(rows.first) row_num = 2 rows.each do |row| next if row['dummy'].to_s.strip.present? event = Event.find(row['id'].to_s.strip) event_category = EventCategory.where(:name => row['category'].to_s.strip).first event.event_category = event_category if event_category library = Library.where(:name => row['library'].to_s.strip).first event.library = library if library event.name = row['name'] if row['name'].to_s.strip.present? event.start_at = row['start_at'] if row['start_at'].to_s.strip.present? event.end_at = row['end_at'] if row['end_at'].to_s.strip.present? event.note = row['end_at'] if row['note'].to_s.strip.present? if row['all_day'].to_s.strip.downcase == 'false' event.all_day = false else event.all_day = true end event.save! row_num += 1 end sm_complete! rescue => e self. = "line #{row_num}: #{e.}" sm_fail! raise e end |
#remove ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/models/event_import_file.rb', line 137 def remove sm_start! rows = open_import_file rows.shift row_num = 2 rows.each do |row| next if row['dummy'].to_s.strip.present? event = Event.find(row['id'].to_s.strip) event.destroy row_num += 1 end sm_complete! rescue => e self. = "line #{row_num}: #{e.}" sm_fail! raise e end |