Clean up first_user rake tasks; create add_user rake task
This commit is contained in:
parent
5ae16c15a3
commit
a13136a4dc
3 changed files with 67 additions and 78 deletions
55
db/seeds/add_user.rb
Normal file
55
db/seeds/add_user.rb
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
#
|
||||
# Add a parameterized user to database.
|
||||
|
||||
require 'yaml'
|
||||
|
||||
def read_password
|
||||
begin
|
||||
system('stty -echo')
|
||||
while true do
|
||||
printf 'Enter password: '
|
||||
pw1 = $stdin.gets.chomp
|
||||
puts
|
||||
printf 'Again: '
|
||||
pw2 = $stdin.gets.chomp
|
||||
puts
|
||||
break if pw1 == pw2
|
||||
puts "They don't match, try again"
|
||||
end
|
||||
ensure
|
||||
system('stty echo')
|
||||
end
|
||||
return pw1
|
||||
end
|
||||
|
||||
username = ARGS[:username] || 'admin'
|
||||
email = ARGS[:email] || "#{username}@#{AppConfig[:pod_uri].host}"
|
||||
password = ARGS[:password] || read_password
|
||||
|
||||
user = User.build(:email => email,
|
||||
:username => username,
|
||||
:password => password,
|
||||
:password_confirmation => password,
|
||||
:person => {
|
||||
:profile => {
|
||||
:first_name => username,
|
||||
:last_name => "Unknown",
|
||||
:image_url => "/images/user/default.png"
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
user.valid?
|
||||
errors = user.errors
|
||||
errors.delete :person
|
||||
if errors.size > 0
|
||||
raise "Error(s) creating user #{username}: #{errors.full_messages.to_s}"
|
||||
end
|
||||
|
||||
user.save
|
||||
user.person.save!
|
||||
user.seed_aspects
|
||||
puts "Created user #{username} with email #{email}"
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
#
|
||||
# Add a parameterized user to database.
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
config_path = File.join (File.dirname(__FILE__), '..', '..', 'config')
|
||||
|
||||
require File.join(config_path, 'environment')
|
||||
require File.join(config_path, 'initializers', '_load_app_config.rb')
|
||||
|
||||
require 'yaml'
|
||||
|
||||
def read_password
|
||||
|
||||
begin
|
||||
system('stty -echo')
|
||||
while true do
|
||||
printf 'Enter password: '
|
||||
pw1 = $stdin.gets.chomp
|
||||
puts
|
||||
printf 'Again: '
|
||||
pw2 = $stdin.gets.chomp
|
||||
puts
|
||||
break if pw1 == pw2
|
||||
puts "They don't match, try again"
|
||||
end
|
||||
ensure
|
||||
system('stty echo')
|
||||
end
|
||||
return pw1
|
||||
end
|
||||
|
||||
username = ( ARGS[:username] == nil ? 'admin' : ARGS[:username].dup)
|
||||
|
||||
if ARGS[:email] == nil
|
||||
config = YAML.load(File.read(Rails.root.join('config',
|
||||
'app_config.yml')))
|
||||
email = username + '@' + config['default']['pod_url']
|
||||
else
|
||||
email = ARGS[:email]
|
||||
end
|
||||
|
||||
password = (ARGS[:password] == nil ? read_password : ARGS[:password])
|
||||
|
||||
user = User.build( :email => email,
|
||||
:username => username,
|
||||
:password => password,
|
||||
:password_confirmation => password,
|
||||
:person => {
|
||||
:profile => {
|
||||
:first_name => username,
|
||||
:last_name => "Unknown",
|
||||
:image_url => "/images/user/default.png"
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
errors = user.errors
|
||||
errors.delete :person
|
||||
if errors.size > 0
|
||||
raise "Error(s) creating user " + username + ": " + errors.to_s
|
||||
end
|
||||
|
||||
user.save
|
||||
user.person.save!
|
||||
user.seed_aspects
|
||||
puts "Created user " + username + ' (' + email + ')'
|
||||
|
|
@ -21,10 +21,10 @@ namespace :db do
|
|||
create
|
||||
end
|
||||
|
||||
task :first_user, :username, :password do |t, args|
|
||||
task :first_user, :username, :password do |t, args|
|
||||
puts "Setting up first user in #{Rails.env} database"
|
||||
ARGS = args
|
||||
require File.dirname(__FILE__) + '/../../db/seeds/first_user'
|
||||
require File.dirname(__FILE__) + '/../../db/seeds/add_user'
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -59,14 +59,19 @@ namespace :db do
|
|||
puts "you did it!"
|
||||
end
|
||||
|
||||
desc 'Purge database, add a new user'
|
||||
desc "Purge database and then add the first user"
|
||||
task :first_user, :username, :password do |t, args|
|
||||
puts "Purging database and adding a new user"
|
||||
username = args[:username] || 'admin'
|
||||
password = args[:password] if args[:password] != nil
|
||||
Rake::Task['db:purge'].invoke
|
||||
Rake::Task['db:seed:first_user'].invoke(username, password)
|
||||
Rake::Task['db:seed:first_user'].invoke(args[:username], args[:password])
|
||||
end
|
||||
task :first_user => :environment
|
||||
|
||||
desc "Add a new user to the database"
|
||||
task :add_user, :username, :password do |t, args|
|
||||
ARGS = args
|
||||
require File.dirname(__FILE__) + '/../../db/seeds/add_user'
|
||||
end
|
||||
task :add_user => :environment
|
||||
|
||||
task :fix_diaspora_handle do
|
||||
puts "fixing the people in this seed"
|
||||
|
|
|
|||
Loading…
Reference in a new issue