Skip to content

Instantly share code, notes, and snippets.

@danielevans
Created June 6, 2011 23:01
Show Gist options
  • Save danielevans/1011309 to your computer and use it in GitHub Desktop.
Save danielevans/1011309 to your computer and use it in GitHub Desktop.
A create admin rake task using the highline gem
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