Allow duplicate titles on importing blog posts

* the unique post_id from WordPress now gets appended if the title is already taken
This commit is contained in:
Marc Remolt 2011-06-03 11:02:25 +02:00
parent e367498ef0
commit 39b482f99f
3 changed files with 49 additions and 39 deletions

View File

@ -1,7 +0,0 @@
module Refinery
module WordPress
end
end
require 'wordpress'

View File

@ -36,16 +36,14 @@ module Refinery
unless user unless user
begin begin
is_draft = draft? ? "true" : "false" unless draft?
if !draft? #p "creating post " + title + " Draft status: " + draft?.to_s
p "creating post " + title + " Draft status: " + is_draft
post = ::BlogPost.create! :title => title, post = ::BlogPost.new :title => title, :body => content_formatted,
:body => content_formatted, :draft => draft?, :published_at => post_date, :created_at => post_date,
:draft => draft?, :author => user, :tag_list => tag_list
:published_at => post_date, post.save!
:created_at => post_date,
:author => user,
:tag_list => tag_list
::BlogPost.transaction do ::BlogPost.transaction do
categories.each do |category| categories.each do |category|
post.categories << category.to_refinery post.categories << category.to_refinery
@ -57,13 +55,12 @@ module Refinery
comment.save comment.save
end end
end end
else
p "Skipping draft post"
end end
rescue Exception => e rescue ActiveRecord::RecordInvalid
# TODO if it's not an activerecord validation error about duplicate title then raise e # if the title has already been taken (WP allows duplicates here,
p e # refinery doesn't) append the post_id to it, making it unique
post.title = "#{title}-#{post_id}"
post.save
end end
post post

View File

@ -227,29 +227,49 @@ describe Refinery::WordPress::Dump, :type => :model do
describe "#to_refinery" do describe "#to_refinery" do
before do before do
User.create! :username => 'admin', :email => 'admin@example.com', @user = User.create! :username => 'admin', :email => 'admin@example.com',
:password => 'password', :password_confirmation => 'password' :password => 'password', :password_confirmation => 'password'
@post = post.to_refinery
end end
it { BlogPost.should have(1).record }
it "should copy the attributes from Refinery::WordPress::Post" do context "with a unique title" do
@post.title.should == post.title before do
@post.body.should == post.content_formatted @post = post.to_refinery
@post.draft.should == post.draft? end
@post.published_at.should == post.post_date
@post.created_at.should == post.post_date it { BlogPost.should have(1).record }
@post.author.username.should == post.creator
it "should copy the attributes from Refinery::WordPress::Post" do
@post.title.should == post.title
@post.body.should == post.content_formatted
@post.draft.should == post.draft?
@post.published_at.should == post.post_date
@post.created_at.should == post.post_date
@post.author.username.should == post.creator
end
it "should assign a category for each Refinery::WordPress::Category" do
@post.categories.should have(post.categories.count).records
end
it "should assign a comment for each Refinery::WordPress::Comment" do
@post.comments.should have(post.comments.count).records
end
end end
it "should assign a category for each Refinery::WordPress::Category" do context "with a duplicate title" do
@post.categories.should have(post.categories.count).records before do
end BlogPost.create! :title => post.title, :body => 'Lorem', :author => @user
@post = post.to_refinery
end
it { BlogPost.should have(2).records }
it "should create the BlogPost with #post_id attached" do
@post.title.should == "#{post.title}-#{post.post_id}"
end
it "should assign a comment for each Refinery::WordPress::Comment" do
@post.comments.should have(post.comments.count).records
end end
end end