Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Semi-graceful seeds...
# config/initializers/colorize.rb
## Just for prettier output
class String
def colorize(color, options = {})
background = options[:background] || options[:bg] || false
style = options[:style]
offsets = ["gray","red", "green", "yellow", "blue", "magenta", "cyan","white"]
styles = ["normal","bold","dark","italic","underline","xx","xx","underline","xx","strikethrough"]
start = background ? 40 : 30
color_code = start + (offsets.index(color) || 8)
style_code = styles.index(style) || 0
"\e[#{style_code};#{color_code}m#{self}\e[0m"
end
end
# db/seeds/seed1.rb
task_description = "Fooing all the bars"
is_successful = true
print task_description + '.'*(100-task_description.length)
begin
Bars.all.each{ |bar| bar.fooify }
rescue => e
is_successful = false
puts "[ERROR]".colorize("red")
if Rake.application.options.trace == true
puts e.backtrace.join("\n").colorize("red")
else
puts "\tSee stacktrace with option --trace".colorize("red")
end
e
end
puts " [SUCCESS]".colorize("green") if is_successful
# db/seeds.rb
start_time = Time.now
puts "Running seeds".colorize("blue")
load "#{Rails.root}/db/seeds/seed1.rb"
load "#{Rails.root}/db/seeds/seed2.rb"
load "#{Rails.root}/db/seeds/seed3.rb"
end_time = Time.now
puts "Finished running seeds in #{(end_time - start_time).to_i} seconds".colorize("blue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment