0
@@ -5,29 +5,47 @@ require 'convert/typo/comment'
0
require 'convert/typo/category'
0
require 'convert/typo/user'
0
- totals = { :users => 0, :articles => 0, :comments => 0 }
0
- home_section = Section.find_by_name 'home'
0
- newpass = 'mephistomigrator'
0
# migrate users over, sorta ...
0
- Typo::User.find(:all).each do |typo_user|
0
- ::User.find_or_create_by_email typo_user.email || 'foo@bar.com',
0
- :login => typo_user.login,
0
- :password_confirmation => newpass
0
+ newpass = 'mephistomigrator'
0
+ Typo::User.find(:all).each do |typo_user|
0
+ ActiveRecord::Base.logger.info "Creating new user for #{typo_user.login}"
0
+ unless ::User.find_by_email(typo_user.email)
0
+ new_user = ::User.create(
0
+ :email => typo_user.email || 'foo@bar.com',
0
+ :login => typo_user.login,
0
+ :password_confirmation => newpass
0
+ unless new_user.valid?
0
+ ActiveRecord::Base.logger.info "New user errors: #{new_user.errors.to_yaml}"
0
+ raise "User creation failed (see log for details)"
0
- puts "migrated #{totals[:users]} user(s)..."
0
- default_user = ::User.find(:first)
0
- Typo::Article.find_all_by_type('Article').each do |typo_article|
0
+ def find_or_create_sections(typo_article)
0
+ home_section = Section.find_by_name 'home'
0
+ section_ids = typo_article.categories.inject([home_section.id]) { |a, c| a << ::Section.find_or_create_by_name(c.name).id }
0
+ def create_article(site, typo_article)
0
+ default_user = ::User.find(:first)
0
user = typo_article.user_id.nil? ?
0
::User.find_by_login(Typo::User.find(typo_article.user_id).login)
0
- section_ids = typo_article.categories.inject([home_section.id]) { |a, c| a << ::Section.find_or_create_by_name(c.name).id }
0
- article = ::Article.create \
0
+ section_ids = find_or_create_sections(typo_article)
0
:title => typo_article.title,
0
:excerpt => typo_article.excerpt,
0
:body => typo_article.body,
0
@@ -35,25 +53,50 @@ module Typo
0
:published_at => typo_article.created_at,
0
:updated_at => typo_article.updated_at,
0
:section_ids => section_ids
0
+ def create_comment(article, typo_comment)
0
+ comment = article.comments.create! \
0
+ :body => typo_comment.body,
0
+ :created_at => typo_comment.created_at,
0
+ :updated_at => typo_comment.updated_at,
0
+ :published_at => typo_comment.created_at,
0
+ :author => typo_comment.author,
0
+ :author_url => typo_comment.url,
0
+ :author_email => typo_comment.email,
0
+ :author_ip => typo_comment.ip
0
+ comment.approved = true
0
- totals[:articles] += 1
0
+ def import_articles(site)
0
- typo_article.comments.each do |typo_comment|
0
- article.comments.create \
0
- :body => typo_comment.body,
0
- :created_at => typo_comment.created_at,
0
- :updated_at => typo_comment.updated_at,
0
- :published_at => typo_comment.created_at,
0
- :author => typo_comment.author,
0
- :author_url => typo_comment.url,
0
- :author_email => typo_comment.email,
0
- :author_ip => typo_comment.ip
0
+ Typo::Article.find_all_by_type('Article').each do |typo_article|
0
+ article = create_article(site, typo_article)
0
- totals[:comments] += 1
0
+ ActiveRecord::Base.logger.info "Creating article comments"
0
+ typo_article.comments.each do |typo_comment|
0
+ ActiveRecord::Base.logger.info "adding comment"
0
+ create_comment(article, typo_comment)
0
- puts "migrated #{totals[:articles]} article(s) and #{totals[:comments]} comment(s)..."
0
+ totals = { :users => 0, :articles => 0, :comments => 0 }
0
+ ActiveRecord::Base.transaction do
0
+ totals[:users] = import_users
0
+ puts "migrated #{totals[:users]} user(s)..."
0
+ totals[:articles], totals[:comments] = import_articles(site)
0
+ puts "migrated #{totals[:articles]} article(s) and #{totals[:comments]} comment(s)..."
Comments
No one has commented yet.