public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
fixes to typo converter + TESTS [octopod]

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@1197 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Fri Jun 09 13:43:28 -0700 2006
commit  e34ef6b26db31a6b4d35c1a42d52b9011773591e
tree    35bc6845c359d7ebdcf114fed617dd9b74ff8487
parent  1be6e71a5ad1b3be554a30c196e25ab981abb759
...
1
2
 
3
 
 
 
 
 
4
5
 
6
7
8
...
1
 
2
3
4
5
6
7
8
9
 
10
11
12
13
0
@@ -1,7 +1,12 @@
0
 module Convert
0
- def self.from(engine)
0
+ def self.from(engine, site_title = nil)
0
     require "convert/#{engine}"
0
+ if site_title
0
+ site = Site.find_by_title(site_title)
0
+ else
0
+ site = Site.find(:first)
0
+ end
0
     puts "converting #{engine.to_s.humanize}..."
0
- engine.to_s.classify.constantize.convert
0
+ engine.to_s.classify.constantize.convert(site)
0
   end
0
 end
0
\ No newline at end of file
...
5
6
7
8
9
10
11
 
12
13
14
15
16
17
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
20
21
22
23
24
 
 
 
 
 
 
 
 
25
26
27
28
29
30
 
 
 
 
31
32
33
...
35
36
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
40
 
 
 
41
42
43
44
45
46
47
48
49
50
51
 
 
 
52
53
 
 
 
 
 
 
54
 
55
56
57
 
 
 
 
 
 
 
 
 
58
59
...
5
6
7
 
 
 
 
8
9
 
 
 
 
 
 
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
 
32
 
 
33
34
35
36
37
38
39
40
41
42
43
44
 
 
45
46
47
48
49
50
51
...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 
74
75
76
77
 
 
 
 
 
 
 
 
 
 
78
79
80
81
 
82
83
84
85
86
87
88
89
90
91
 
92
93
94
95
96
97
98
99
100
101
102
0
@@ -5,29 +5,47 @@ require 'convert/typo/comment'
0
 require 'convert/typo/category'
0
 require 'convert/typo/user'
0
 module Typo
0
- def self.convert
0
- totals = { :users => 0, :articles => 0, :comments => 0 }
0
- home_section = Section.find_by_name 'home'
0
- newpass = 'mephistomigrator'
0
+ class << self
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 => newpass,
0
- :password_confirmation => newpass
0
- totals[:users] += 1
0
+ def import_users
0
+ newpass = 'mephistomigrator'
0
+ users = 0
0
+
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 => newpass,
0
+ :password_confirmation => newpass
0
+ )
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
+ end
0
+ users += 1
0
+ end
0
+ end
0
+ users
0
     end
0
-
0
- puts "migrated #{totals[:users]} user(s)..."
0
 
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
+ end
0
+
0
+ def create_article(site, typo_article)
0
+ default_user = ::User.find(:first)
0
+
0
       user = typo_article.user_id.nil? ?
0
         default_user :
0
         ::User.find_by_login(Typo::User.find(typo_article.user_id).login)
0
 
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
+
0
+ ::Article.create! \
0
+ :site => site,
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
         :user => user,
0
+ :updater => user,
0
         :section_ids => section_ids
0
+ end
0
+
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
+ comment.save
0
+ end
0
 
0
- totals[:articles] += 1
0
+ def import_articles(site)
0
+ articles = 0
0
+ comments = 0
0
 
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
+ articles += 1
0
 
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
+ comments += 1
0
+ end
0
       end
0
+ [articles, comments]
0
     end
0
 
0
- puts "migrated #{totals[:articles]} article(s) and #{totals[:comments]} comment(s)..."
0
+ def convert(site)
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)..."
0
+ end
0
+ end
0
   end
0
 end

Comments

    No one has commented yet.