diff options
| -rw-r--r-- | lib/authors_importer.rb | 57 |
1 files changed, 40 insertions, 17 deletions
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' | |||
| 4 | class AuthorsImporter | 4 | class AuthorsImporter |
| 5 | 5 | ||
| 6 | def initialize path | 6 | def initialize path |
| 7 | if File.exists? path | 7 | @parsed_file = CSV::Reader.parse(File.open(path, "r")) if File.exists? path |
| 8 | @parsed_file = CSV::Reader.parse(File.open(path, "r")) | ||
| 9 | else | ||
| 10 | raise "File does not exist" | ||
| 11 | end | ||
| 12 | end | 8 | end |
| 13 | 9 | ||
| 14 | def import_authors | 10 | def import_authors |
| 15 | @parsed_file.each do |row| | 11 | parse_csv || find_or_create_user |
| 16 | password = generate_password | 12 | end |
| 13 | |||
| 14 | def parse_csv | ||
| 15 | if @parsed_file | ||
| 16 | @parsed_file.each do |row| | ||
| 17 | password = generate_password | ||
| 18 | |||
| 19 | options = { | ||
| 20 | :login => row[0], | ||
| 21 | :full_name => row[1], | ||
| 22 | :email => row[2], | ||
| 23 | :password => password, | ||
| 24 | :password_confirmation => password | ||
| 25 | } | ||
| 26 | |||
| 27 | find_or_create_user options | ||
| 28 | end | ||
| 17 | 29 | ||
| 18 | options = { | 30 | return true |
| 19 | :login => row[0], | 31 | end |
| 20 | :full_name => row[1], | 32 | end |
| 21 | :email => row[2], | 33 | |
| 22 | :password => password, | 34 | def find_or_create_user options = {} |
| 23 | :password_confirmation => password | 35 | password = generate_password |
| 24 | } | 36 | |
| 37 | # default_options = { | ||
| 38 | # :login => "webmaster", | ||
| 39 | # :full_name => "Webmaster", | ||
| 40 | # :email => "webmaster@ccc.de", | ||
| 41 | # :password => password, | ||
| 42 | # :password_confirmation => password | ||
| 43 | # } | ||
| 44 | # default_options = {} | ||
| 45 | # | ||
| 46 | # options = default_options.merge(options) | ||
| 47 | puts options[:login] | ||
| 48 | |||
| 49 | unless User.find_by_email(options[:email]) | ||
| 25 | 50 | ||
| 26 | unless user = User.find_by_email(options[:email]) | 51 | User.create! options |
| 27 | User.create options | ||
| 28 | end | ||
| 29 | end | 52 | end |
| 30 | end | 53 | end |
| 31 | 54 | ||
