Skip to content

Instantly share code, notes, and snippets.

@pat
Last active July 25, 2016 19:10
Show Gist options
  • Save pat/81101346d188aac078988b0f7cd0e85e to your computer and use it in GitHub Desktop.
Save pat/81101346d188aac078988b0f7cd0e85e to your computer and use it in GitHub Desktop.
Radiant to Refinery migration

Some rake tasks to help migrate Radiant (v1.1.4) sites to Refinery (v3.0.4).

  • Put these Ruby files in place in the respective Radiant and (new) Refinery apps
  • You'll need to manually bring your layouts across and adapt accordingly, as well as images, javascripts and stylesheets.
  • Run rake export:json in your Radiant app
  • Copy the resulting dump.json file into the Refinery app (or put it online somewhere - perhaps a private gist - if you're going to need to import in the production environment)
  • Add the salt column to your Refinery app's user model: rails g migration add_salt_to_refinery_authentication_devise_users salt:string
  • Run rake db:migrate in your Refinery app.
  • Run rake import:pages import:assets import:users in your Refinery app.
  • In a Rails console in your Refinery app, change your home page to have the root path. If it's the first page in the app (likely), this will do the trick: Refinery::Page.first.update_attributes :link_url => '/'

Yes, the code in these files could be refactored to be elegant. My focus was on getting something to work - the fact that this could be potentially re-used by others is just a nice surprise :) Please, if you are going to use this, read and understand the code. It suited my needs, but it may not suit yours.

# For the Radiant app:
# lib/tasks/export.rake
#
namespace :export do
task :json => :environment do
pages = Page.all.collect { |page|
{
:id => page.id,
:title => page.title,
:slug => page.slug,
:parent_id => page.parent_id,
:draft => (page.status.name == 'Draft'),
:parts => page.parts.collect { |part|
{
:name => part.name,
:content => part.content
}
}
}
}
assets = Asset.all.collect { |asset|
{
:id => asset.id,
:caption => asset.caption,
:title => asset.title,
:file_name => asset.asset_file_name,
:mime_type => asset.asset_content_type,
:file_size => asset.asset_file_size,
:url => asset.asset.url
}
}
users = User.all.collect { |user|
{
:name => user.name,
:login => user.login,
:encrypted_password => user.password,
:salt => user.salt,
:email => user.email,
:admin => user.admin
}
}
File.write "dump.json", JSON.dump(
:pages => pages,
:assets => assets,
:users => users
)
end
end
# For the Refinery app:
# lib/tasks/import.rake
#
namespace :import do
task :pages => :environment do
Refinery::Page.all.each &:destroy!
json = JSON.parse json_input
json['pages'].each do |hash|
next unless hash['parent_id'].nil?
import_page hash, json['pages']
end
end
task :assets => :environment do
Refinery::Image.all.each &:destroy!
Refinery::Resource.all.each &:destroy!
json = JSON.parse json_input
json['assets'].each do |hash|
if ::Refinery::Images.whitelisted_mime_types.include?(hash['mime_type'])
object = Refinery::Image.create!(
:image => Dragonfly.app.fetch_url(hash['url']),
:image_title => hash['title'],
:image_alt => hash['caption']
).image
else
object = Refinery::Resource.create!(
:file => Dragonfly.app.fetch_url(hash['url']),
:resource_title => hash['title']
).file
end
Refinery::PagePart.all.each do |part|
part.content = part.content.gsub(hash['url'], object.url)
part.save!
end
end
end
task :users => :environment do
Refinery::Authentication::Devise::User.destroy_all
json = JSON.parse json_input
json['users'].each do |hash|
user = Refinery::Authentication::Devise::User.create!(
:username => hash['login'],
:email => hash['email'],
:encrypted_password => hash['encrypted_password'],
:salt => hash['salt'],
:importing => true
)
user.roles << Refinery::Authentication::Devise::Role.find_or_create_by(
title: 'Refinery'
)
user.roles << Refinery::Authentication::Devise::Role.find_or_create_by(
title: 'Superuser'
) if hash['admin']
user.plugins.create :name => 'refinery_files'
user.plugins.create :name => 'refinery_images'
user.plugins.create :name => 'refinery_pages'
end
end
def import_page(hash, hashes, parent_id = nil)
page = Refinery::Page.create!(
:title => hash['title'],
:slug => hash['slug'],
:draft => hash['draft'],
:parent_id => parent_id
)
hash['parts'].each do |part|
page.parts.create!(
:title => part['name'],
:body => part['content']
)
end
hashes.each do |inner|
next unless inner['parent_id'] == hash['id']
import_page inner, hashes, page.id
end
end
def json_input
if ENV['IMPORT_JSON']
require 'open-uri'
open(ENV['IMPORT_JSON']).read
else
File.read 'dump.json'
end
end
end
# For the Refinery app:
# config/initializers/radiant_authentication.rb
#
# Note: you will also need to add a salt column to your model:
# rails g migration add_salt_to_refinery_authentication_devise_users salt:string
#
module RadiantAuthentication
attr_accessor :importing
def self.included(base)
base.validates :salt, :presence => true
base.before_validation :set_salt, :on => :create
end
# Verifies whether a password (ie from sign in) is the user password.
def valid_password?(unencrypted_password)
encrypted_password == password_digest(unencrypted_password)
end
protected
def password_digest(unencrypted_password)
Digest::SHA1.hexdigest("--#{salt}--#{unencrypted_password}--")
end
def password_required?
return false if importing
end
def set_salt
self.salt ||= Digest::SHA1.hexdigest(
"--#{Time.now}--#{username}--sweet harmonious biscuits--"
)
end
end
Refinery::Authentication::Devise::User.include RadiantAuthentication
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment