Class: EventImportFile

Inherits:
ApplicationRecord
  • Object
show all
Includes:
ImportFile, Statesman::Adapters::ActiveRecordQueries
Defined in:
app/models/event_import_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modeObject

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

.importObject



167
168
169
170
171
172
173
# File 'app/models/event_import_file.rb', line 167

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

#importObject



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
101
102
# 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_result.assign_attributes({ event_import_file_id: id, body: row.fields.join("\t") })
    event_import_result.save!

    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.where(name: row['library']).first
    library = default_library || Library.web if library.blank?
    event.library = library
    event_category = EventCategory.where(name: row['event_category']).first
    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)
  send_message(mailer)
  num
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = EventImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#modifyObject



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
138
139
# File 'app/models/event_import_file.rb', line 104

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.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 = 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)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = EventImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#removeObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/event_import_file.rb', line 141

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)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  save
  transition_to!(:failed)
  mailer = EventImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#state_machineObject



37
38
39
# File 'app/models/event_import_file.rb', line 37

def state_machine
  EventImportFileStateMachine.new(self, transition_class: EventImportFileTransition)
end