Class: EventImportFile
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- EventImportFile
- Includes:
- ImportFile, Statesman::Adapters::ActiveRecordQueries
- Defined in:
- app/models/event_import_file.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#mode ⇒ Object
Returns the value of attribute mode.
35 36 37 |
# File 'app/models/event_import_file.rb', line 35 def mode @mode end |
Class Method Details
.import ⇒ Object
165 166 167 168 169 170 171 |
# File 'app/models/event_import_file.rb', line 165 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
44 45 46 47 48 49 50 51 52 53 54 55 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 |
# File 'app/models/event_import_file.rb', line 44 def import transition_to!(:started) num = { imported: 0, failed: 0 } rows = open_import_file(create_import_temp_file(event_import)) check_field(rows.first) row_num = 1 rows.each do |row| row_num += 1 next if row['dummy'].to_s.strip.present? event_import_result = EventImportResult.new(event_import_file_id: id, body: row.fields.join("\t")) event = Event.new event.name = row['name'].to_s.strip event.display_name = row['display_name'] event.note = row['note'] event.start_at = Time.zone.parse(row['start_at']) if row['start_at'].present? event.end_at = Time.zone.parse(row['end_at']) if row['end_at'].present? category = row['event_category'].to_s.strip if %w(t true TRUE).include?(row['all_day'].to_s.strip) event.all_day = true else event.all_day = false end library = Library.find_by(name: row['library']) library = default_library || Library.web if library.blank? event.library = library event_category = EventCategory.find_by(name: row['event_category']) event_category = default_event_category if event_category.blank? event.event_category = event_category if event.save event_import_result.event = event num[:imported] += 1 if row_num % 50 == 0 Sunspot.commit GC.start end else num[:failed] += 1 end event_import_result.save! end Sunspot.commit rows.close transition_to!(:completed) mailer = EventImportMailer.completed(self) (mailer) num rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) mailer = EventImportMailer.failed(self) (mailer) raise e end |
#modify ⇒ Object
102 103 104 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 136 137 |
# File 'app/models/event_import_file.rb', line 102 def modify transition_to!(:started) rows = open_import_file(create_import_temp_file(event_import)) check_field(rows.first) row_num = 1 rows.each do |row| row_num += 1 next if row['dummy'].to_s.strip.present? event = Event.find(row['id'].to_s.strip) event_category = EventCategory.where(name: row['event_category'].to_s.strip).first event.event_category = event_category if event_category library = Library.find_by(name: row['library'].to_s.strip) event.library = library if library event.name = row['name'] if row['name'].to_s.strip.present? event.start_at = Time.zone.parse(row['start_at']) if row['start_at'].present? event.end_at = Time.zone.parse(row['end_at']) if row['end_at'].present? event.note = row['note'] if row['note'].to_s.strip.present? if %w(t true TRUE).include?(row['all_day'].to_s.strip) event.all_day = true else event.all_day = false end event.save! end transition_to!(:completed) mailer = EventImportMailer.completed(self) (mailer) rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) mailer = EventImportMailer.failed(self) (mailer) raise e end |
#remove ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/models/event_import_file.rb', line 139 def remove transition_to!(:started) rows = open_import_file(create_import_temp_file(event_import)) rows.shift row_num = 1 rows.each do |row| row_num += 1 next if row['dummy'].to_s.strip.present? event = Event.find(row['id'].to_s.strip) event.picture_files.destroy_all # workaround event.reload event.destroy end transition_to!(:completed) mailer = EventImportMailer.completed(self) (mailer) rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) mailer = EventImportMailer.failed(self) (mailer) raise e end |
#state_machine ⇒ Object
37 38 39 |
# File 'app/models/event_import_file.rb', line 37 def state_machine EventImportFileStateMachine.new(self, transition_class: EventImportFileTransition) end |