From 39b482f99f0d9245aacfdd9d393531807e74e31e Mon Sep 17 00:00:00 2001 From: Marc Remolt Date: Fri, 3 Jun 2011 11:02:25 +0200 Subject: [PATCH] Allow duplicate titles on importing blog posts * the unique post_id from WordPress now gets appended if the title is already taken --- lib/refinerycms_wordpress_import.rb | 7 ---- lib/wordpress/post.rb | 29 ++++++++-------- spec/lib/wordpress/dump_spec.rb | 52 ++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 39 deletions(-) delete mode 100644 lib/refinerycms_wordpress_import.rb diff --git a/lib/refinerycms_wordpress_import.rb b/lib/refinerycms_wordpress_import.rb deleted file mode 100644 index 9457f44..0000000 --- a/lib/refinerycms_wordpress_import.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Refinery - module WordPress - - end -end - -require 'wordpress' diff --git a/lib/wordpress/post.rb b/lib/wordpress/post.rb index 96d338c..4caa92d 100644 --- a/lib/wordpress/post.rb +++ b/lib/wordpress/post.rb @@ -36,16 +36,14 @@ module Refinery unless user begin - is_draft = draft? ? "true" : "false" - if !draft? - p "creating post " + title + " Draft status: " + is_draft - post = ::BlogPost.create! :title => title, - :body => content_formatted, - :draft => draft?, - :published_at => post_date, - :created_at => post_date, - :author => user, - :tag_list => tag_list + unless draft? + #p "creating post " + title + " Draft status: " + draft?.to_s + + post = ::BlogPost.new :title => title, :body => content_formatted, + :draft => draft?, :published_at => post_date, :created_at => post_date, + :author => user, :tag_list => tag_list + post.save! + ::BlogPost.transaction do categories.each do |category| post.categories << category.to_refinery @@ -57,13 +55,12 @@ module Refinery comment.save end end - - else - p "Skipping draft post" end - rescue Exception => e - # TODO if it's not an activerecord validation error about duplicate title then raise e - p e + rescue ActiveRecord::RecordInvalid + # if the title has already been taken (WP allows duplicates here, + # refinery doesn't) append the post_id to it, making it unique + post.title = "#{title}-#{post_id}" + post.save end post diff --git a/spec/lib/wordpress/dump_spec.rb b/spec/lib/wordpress/dump_spec.rb index 3c11766..304e554 100644 --- a/spec/lib/wordpress/dump_spec.rb +++ b/spec/lib/wordpress/dump_spec.rb @@ -227,29 +227,49 @@ describe Refinery::WordPress::Dump, :type => :model do describe "#to_refinery" 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' - - @post = post.to_refinery end - it { BlogPost.should have(1).record } - 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 + context "with a unique title" do + before do + @post = post.to_refinery + end + + it { BlogPost.should have(1).record } + + 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 - it "should assign a category for each Refinery::WordPress::Category" do - @post.categories.should have(post.categories.count).records - end + context "with a duplicate title" do + before do + 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