From 53343abf58efd264720391d44309142551a9e122 Mon Sep 17 00:00:00 2001 From: hukl Date: Mon, 13 Apr 2009 17:57:14 +0200 Subject: refactor of the authors importer --- lib/authors_importer.rb | 57 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/authors_importer.rb b/lib/authors_importer.rb index b0d9741..6195111 100644 --- a/lib/authors_importer.rb +++ b/lib/authors_importer.rb @@ -4,28 +4,51 @@ require 'csv' class AuthorsImporter def initialize path - if File.exists? path - @parsed_file = CSV::Reader.parse(File.open(path, "r")) - else - raise "File does not exist" - end + @parsed_file = CSV::Reader.parse(File.open(path, "r")) if File.exists? path end def import_authors - @parsed_file.each do |row| - password = generate_password + parse_csv || find_or_create_user + end + + def parse_csv + if @parsed_file + @parsed_file.each do |row| + password = generate_password + + options = { + :login => row[0], + :full_name => row[1], + :email => row[2], + :password => password, + :password_confirmation => password + } + + find_or_create_user options + end - options = { - :login => row[0], - :full_name => row[1], - :email => row[2], - :password => password, - :password_confirmation => password - } + return true + end + end + + def find_or_create_user options = {} + password = generate_password + + # default_options = { + # :login => "webmaster", + # :full_name => "Webmaster", + # :email => "webmaster@ccc.de", + # :password => password, + # :password_confirmation => password + # } + # default_options = {} + # + # options = default_options.merge(options) + puts options[:login] + + unless User.find_by_email(options[:email]) - unless user = User.find_by_email(options[:email]) - User.create options - end + User.create! options end end -- cgit v1.3