Created
December 13, 2012 15:44
-
-
Save thephw/4277283 to your computer and use it in GitHub Desktop.
Semi-graceful seeds...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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