Created
June 6, 2011 23:01
-
-
Save danielevans/1011309 to your computer and use it in GitHub Desktop.
A create admin rake task using the highline gem
This file contains hidden or 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
namespace :admin do | |
desc "creates and admin user" | |
task :create => :environment do | |
line = HighLine.new # a command line lib that uses erb? Bizarre. | |
line.say "<%= color('Creating a new administrative user', BOLD) %>" | |
email = line.ask("Email: ") | |
pass = line.ask("Password: " ) { |q| q.echo = "*" } | |
admin = Admin.new(:email => email, :password => pass) | |
puts "\n************************************" | |
if admin.save | |
line.say "<%= color('Admin user created', GREEN) %>" | |
line.say "<%= color('Please visit /admins/sign_in in a browser to log in', GREEN + BOLD) %>" | |
else | |
line.say "<%= color('The admin user could not be created', RED + BOLD) %>" | |
admin.errors.each_pair do |key, value| | |
error = "#{key}: #{value}".gsub(/[\'\"]/, '') | |
line.say "<%= color('#{error}', RED) %>" | |
end | |
end | |
puts "\n************************************" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment