First shot at attachment import
* only images for now * only blog posts for now * thumbnailed images are not yet replaced in blog posts body
This commit is contained in:
		
							parent
							
								
									9a2b5acef6
								
							
						
					
					
						commit
						7550dfa164
					
				@ -19,6 +19,7 @@ namespace :wordpress do
 | 
			
		||||
    dump = Refinery::WordPress::Dump.new(params[:file_name])
 | 
			
		||||
 | 
			
		||||
    dump.authors.each(&:to_refinery)
 | 
			
		||||
    attachments = dump.attachments.each(&:to_refinery)
 | 
			
		||||
    
 | 
			
		||||
    only_published = ENV['ONLY_PUBLISHED'] == 'true' ? true : false
 | 
			
		||||
    dump.posts(only_published).each(&:to_refinery)
 | 
			
		||||
@ -28,6 +29,12 @@ namespace :wordpress do
 | 
			
		||||
    ENV["MODEL"] = 'BlogPost'
 | 
			
		||||
    Rake::Task["friendly_id:redo_slugs"].invoke
 | 
			
		||||
    ENV.delete("MODEL")
 | 
			
		||||
 | 
			
		||||
    # parse all created BlogPosts bodys and replace the old wordpress image uls 
 | 
			
		||||
    # with the newly created ones
 | 
			
		||||
    attachments.each do |attachment|
 | 
			
		||||
      attachment.replace_image_url_in_blog_posts
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  desc "reset blog tables and then import blog data from a Refinery::WordPress XML dump"
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ module Refinery
 | 
			
		||||
  module WordPress
 | 
			
		||||
    class Attachment
 | 
			
		||||
      attr_reader :node
 | 
			
		||||
      attr_reader :refinery_image
 | 
			
		||||
 | 
			
		||||
      def initialize(node)
 | 
			
		||||
        @node = node
 | 
			
		||||
@ -15,6 +16,10 @@ module Refinery
 | 
			
		||||
        node.xpath("description").text
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def file_name
 | 
			
		||||
        url.split('/').last
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def post_date
 | 
			
		||||
        DateTime.parse node.xpath("wp:post_date").text
 | 
			
		||||
      end
 | 
			
		||||
@ -22,6 +27,44 @@ module Refinery
 | 
			
		||||
      def url
 | 
			
		||||
        node.xpath("wp:attachment_url").text
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def image?
 | 
			
		||||
        url.match /\.(png|jpg|jpeg|gif)$/ 
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def to_refinery
 | 
			
		||||
        if image?
 | 
			
		||||
          to_image
 | 
			
		||||
        else
 | 
			
		||||
          to_file
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def replace_image_url_in_blog_posts
 | 
			
		||||
        ::BlogPost.all.each do |post|
 | 
			
		||||
          if post.body.include? url
 | 
			
		||||
            post.body = post.body.gsub(url, refinery_image.image.url)
 | 
			
		||||
            post.save!
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      private
 | 
			
		||||
 | 
			
		||||
      def to_image
 | 
			
		||||
        image = ::Image.new
 | 
			
		||||
        image.created_at = post_date
 | 
			
		||||
        image.image_url = url
 | 
			
		||||
        image.save!
 | 
			
		||||
 | 
			
		||||
        @refinery_image = image
 | 
			
		||||
        image
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def to_file
 | 
			
		||||
        raise "to_file is not implemented yet, sorry!"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -6,5 +6,38 @@ describe Refinery::WordPress::Attachment, :type => :model do
 | 
			
		||||
  specify { attachment.title.should == '200px-Tux.svg' }
 | 
			
		||||
  specify { attachment.description.should == 'Tux, the Linux mascot' }
 | 
			
		||||
  specify { attachment.url.should == 'http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png' }
 | 
			
		||||
  specify { attachment.file_name.should == '200px-Tux.svg_.png' }
 | 
			
		||||
  specify { attachment.post_date.should == DateTime.new(2011, 6, 5, 15, 26, 51) }
 | 
			
		||||
  specify { attachment.should be_an_image }
 | 
			
		||||
 | 
			
		||||
  describe "#to_refinery" do
 | 
			
		||||
    before do
 | 
			
		||||
      @image = attachment.to_refinery
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "should create an Image from the Attachment" do
 | 
			
		||||
      @image.should be_a(Image)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "should copy the attributes from Attachment" do
 | 
			
		||||
      @image.created_at.should == attachment.post_date
 | 
			
		||||
      @image.image.url.end_with?(attachment.file_name).should be_true
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#replace_image_url" do
 | 
			
		||||
    before do
 | 
			
		||||
      test_dump.authors.each(&:to_refinery)
 | 
			
		||||
      test_dump.posts.each(&:to_refinery)
 | 
			
		||||
      @image = attachment.to_refinery
 | 
			
		||||
 | 
			
		||||
      attachment.replace_image_url_in_blog_posts
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "should replace attachment urls in the generated BlogPosts" do
 | 
			
		||||
      BlogPost.first.body.should_not include(attachment.url)
 | 
			
		||||
      BlogPost.first.body.should include(@image.image.url)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user