Today I set out to develop a simple proof of concept: a Wiki developed in Ruby on Rails using Subversion as a repository.
Doing a very basic Wiki using RoR is a piece of cake, given the availability of RedCloth and BlueCloth. However, having it use Subversion instead of a relational database is something that needs more work.
Luckily, the latest Subversion release (1.3.2 at the time of this writing) includes full Ruby bindings using SWIG. Unluckily, the precompiled package for OS X that I was using (from the fine folks at Metissian) doesn’t come with the bindings, so I had to reinstall Subversion from the source: nothing really complicated here, but the options for getting it to work over HTTPS should be documented better.
Speaking of documentation, there is apparently none for the SVN Ruby bindings… which is a shame. I could only find something regarding installation thanks to PJ Hyett, why the lucky stiff and Garrett Rooney, but armed with the source, using BountySource’s bsSvnBrowser as a sample (read-only code though, while I need to read and write) and especially Garrett’s book Practical Subversion1, I managed to conjure up some code that, on the surface of it, seems to work.
Here’s a snippet that saves an ActiveRecord instance to the SVN repository:
def save(validate = true)
return false if (validate && ! valid?)
repo = Svn::Repos.open(REPO_PATH)
repo.fs.transaction { | txn |
txn.root.make_file(title)
stream = txn.root.apply_text(title)
stream.write(text)
stream.close
}
return true
end
I’m not sure this the correct way to implement save, but it works.
1. When I reviewed Garrett’s book, I wrote: “It’s written largely from the perspective of a repository administrator or a power user wanting to extend the system and develop new applications on top of it, as the long (73 pages) chapter on Subversion APIs demonstrates.” Finally that long chapter has been useful to me!