tags:

views:

27

answers:

1

I've got a Rails app with blog entries that I want to update from a separate Blog.git repo.

I envision my workflow as something like:

  1. Write new blog entry
  2. Push to remote Git repo
  3. Call cap deploy:update, which will invoke a Rake task to update the database with any changed or new entries

The hitch here is finding which files have changed. I'd like to harness Git for this, and I know I could do some awk or Perl scripting on git diff.

But is there a better way? I've briefly looked at Grit but can't find a good solution.

Update: It turns out Grit is the best solution to this problem, at least as far as I can tell. Here's what I used to solve the problem:

desc 'Posts all entries to database'
task :post_all do
  Dir.chdir REPO do
    Grit::Repo.new('.').tree.contents.each do |file|
      # post_entry cleans up my blog entries and posts them via Post.create()
      post_entry(file.data, :text) unless file.basename =~ /\.gitignore/
    end
  end
end

desc 'Posts all new or changed entries to database'
task :post_new do
  Dir.chdir REPO do
    Grit::Repo.new('.').head.commit.diffs.each do |diff|
      post_entry diff.b_blob.data, :text
    end
  end
end

desc 'Deletes entries from database'
task :remove_all do
  Post.destroy_all
end

desc 'Synchronizes the remote blog repo and the database'
task :sync => [ :remove_all, :post_all ]
+1  A: 

is a database really necessary? check out the jekyll gem. hundreds (if not thousands) of simple blogs use it, including two of mine.

http://github.com/mojombo/jekyll

otherwise, grit is a good solution. i've used it for a few things and it works well.

Derick Bailey
I started off using Toto (by Alexis Sellier) because I wanted a minimalist solution. However, I want to be able to sort my entries on a whim and also want to associate all of my other models with articles (because this is only starting out as a blog -- it will probably grow to something bigger). That said, I figured out my own problem, but I'll give you the points because Jekyll is just A Good Thing (tm). Cheers.
David Jacobs
:) thanks! ... and glad to see you got it solved.
Derick Bailey