-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_error_data_seeder.rb
54 lines (50 loc) · 1.46 KB
/
app_error_data_seeder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'data_seeder'
class AppErrorDataSeeder
include ::DataSeeder::Loader
def setup
@app = App.find_or_initialize_by(name: self.path_minus_ext)
@existing_errors = {}
if @app.new_record?
logger.info { "Loading errors for new App: #{@app.name}" }
@app.save!
else
logger.info { "Loading errors for existing App: #{@app.name}" }
@app.app_errors.each do |app_error|
@existing_errors[app_error.code] = app_error
end
end
end
def teardown
unless @existing_errors.empty?
logger.info { "The following are begin removed:" }
log_indent do
@existing_errors.each do |code, app_error|
logger.info { "#{code}: #{app_error.message}" }
app_error.destroy
end
end
end
end
def load(io)
io.each_line do |line|
line.strip!
next if line.blank? || line[0] == ?#
space_i = line.index(' ')
raise "Invalid line: #{line}" unless space_i
code = line[0,space_i].strip
message = line[space_i+1..-1].strip
app_error = @existing_errors[code]
if app_error
@existing_errors.delete(code)
app_error.message = message
unless app_error.changes.empty?
logger.info { "Changing #{code}: #{app_error.changes}" }
app_error.save!
end
else
logger.info { "Creating #{code}: #{message}" }
@app.app_errors.create!(code: code, message: message)
end
end
end
end