new plugin: acts_as_git
Courtenay : November 14th, 2008
With the help of Jamie van Dyke at Parfait and Scott Chacon at GitHub, I'm pleased to announce Acts As Git (no, I don't like the name either). It's a simple plugin which stores all changes you make to a text field in a git repository. This is ideal for something like a git-backed wiki.
Look at it here: github or check it out from
git://github.com/courtenay/acts_like_git.git
From the README:
ALG automagically saves the history of a given text or string field. It sits over the top of an ActiveRecord model; after a value is committed to the database, the plugin writes the new value to a text file and commits it to a git repository. This way you get all the advantages of using Git as version-control.
Usage:
class Post < ActiveRecord::Base
versioning(:title) do |version|
version.repository = '/home/git/repositories/postal.git'
version.message = lambda { |post| "Committed by #{post.author.name}" }
end
end
To view the complete list of changes:
>> @post = Post.find 15
<Post:15>
>> @post.title
=> 'Freddy'
>> @post.history(:title)
=> ['Joe', 'Frank', 'Freddy]
>> @post.log
=> ['bfec2f69e270d2d02de4e8c7a4eb2bd0f132bdbb', '643deb45c12982dde75ba71657792a2dbdda83e6',
'1ce6c7368219db7698f4acc3417e656510b4138d']
>> @post.revert_to '1ce6c7368219db7698f4acc3417e656510b4138d'
>> @post.title
=> 'Joe'
It uses the excellent Grit library, and doesn't actually have a checked-out repository. The latest version of your data is still stored in the database. You can actually clone this repo and view the changes; pushing back to it won't do anything useful.
8 Responses to “new plugin: acts_as_git”
Sorry, comments are closed for this article.
November 14th, 2008 at 05:31 PM
Sweet plugin. Thanks for sharing it!
November 14th, 2008 at 07:55 PM
This is the coolest thing I have ever seen.
November 15th, 2008 at 12:53 AM
Very nice. I have just the right project for this.
November 15th, 2008 at 02:41 AM
That's brilliant !!!
November 15th, 2008 at 06:33 AM
This looks really interesting! I don't understand what that last sentence means, though. Are you saying that changes made to the git repo directly won't be reflected in the app?
November 15th, 2008 at 11:57 AM
Awesome
November 16th, 2008 at 09:05 PM
Doesn't ikiwiki do this? (the revision-control-system-backed wiki idea, at least)
November 28th, 2008 at 12:41 PM
I was using actslikegit and found a problem.
Bascially:
This bug doesn't show up if you reload the app or script/console, as it is a cached value. p2.body is incorrect, but p2['body'] still has the correct value.
This is because p1 and p2 share the variable, @versionedfieldsvalues as a class variable.
It seems this should be an instance variable instead.
A patched fork is available at http://github.com/hachiya/actslikegit
I've submitted a pull request to you guys. Feel free to use as desired.
Hopefully this helps someone.