simplelog to mephisto
For some reason, I’ve never ever successfully utilized the converter infrastructure found in vendors/plugins/mephisto_converters/lib/converters
, so I’ve generally had to cobble together my own kludge.
simplelog-to-mephisto.rb
require 'converters/base'
require 'converters/simplelog'
@users = User.find(:all)
default_user = @users.first
site = Site.find(1)
Simplelog::Post.find(:all).each do |sl|
puts "Importing post #{sl.id}: #{sl.title}"
mp = Article.new()
mp.title = sl.title
mp.excerpt = ''
mp.body = sl.body
mp.created_at = sl.created_at
mp.published_at = sl.created_at
mp.updated_at = sl.modified_at
mp.filter = sl.text_filter + '_filter'
mp.permalink = sl.permalink
mp.user = default_user
mp.updater = default_user
mp.site = site
mp.author_ip = '127.0.0.1'
mp.tag = sl.tag.collect() * ', '
mp.approved = true
mp.section_ids = [1]
mp.save!
end
Simplelog::Comment.find(:all).each do |sl|
mp=Comment.new()
mp.body = sl.body
mp.filter = "markdown_filter"
mp.created_at = sl.created_at
mp.updated_at = sl.modified_at
mp.published_at = sl.created_at
mp.author = sl.name
mp.author_url = sl.url
mp.author_email = sl.email
mp.author_ip = sl.ip
mp.article_id = Article.find_by_title(Simplelog::Post.find_by_id(sl.post_id).title).id
mp.approved = true
mp.save!
end
This relies on the following classes:
simplelog/author.rb
module Simplelog
class Author ActiveRecord::Base
establish_connection configurations['simplelog']
has_many :posts, :dependent => :destroy, :class_name => 'Simplelog::Post'
end
end
simplelog/comment.rb
module Simplelog
class Comment ActiveRecord::Base
establish_connection configurations['simplelog']
belongs_to :post, :class_name => 'Simplelog::Post'
end
end
simplelog/page.rb
module Simplelog
class Page ActiveRecord::Base
establish_connection configurations['simplelog']
end
end
simplelog/post.rb
module Simplelog
class Post ActiveRecord::Base
establish_connection configurations['simplelog']
has_and_belongs_to_many :tag,
:class_name => 'Simplelog::Tag',
:join_table => 'tags_posts'
belongs_to :author, :class_name => 'Simplelog::Author'
has_many :comments, :conditions => ['is_approved = ?', true], :dependent => :destroy, :class_name => 'Simplelog::Comment'
end
end
simplelog/tag.rb
module Simplelog
class Tag ActiveRecord::Base
establish_connection configurations['simplelog']
has_and_belongs_to_many :post,
:class_name => 'Simplelog::Post',
:join_table => 'tags_post'
end
end
The Post
definition and the Tag
definition have a little voodoo for dealing with the transition from Rails 1.2 to Rails 2.0. In 1.2, acts_as_taggable
names the join table backwards compared to 2.0, where it uses the normal semantics.
If this weren’t just a hack, I’d probably package it a little more nicely, but as it is, I’d appreciate any tips on how to actually get the converter infrastructure to work.