Introduction
A post-commit hook is basically a piece of information that can be sent to a third party software service to notify and/or trigger an event at their end. A simple case use I am going to explain would be sending a message to a Campfire chat room every time a new record is created. Remember, a record can be anything you want i.e. a blog post or a new medical record. It just depends on what your Rails application actually does.
Example Use / Case Study
I have an application that has a model called Kases (Cases is a reserved word in Rails, so we have to use Kases). Put simply, imagine a lawyer and what they use Cases for.
Each case hold various bit of data that’s important to the Lawyer. However, this lawyer is part of a partnership – his colleagues all chat in Campfire all day to keep in touch with each other in their remote offices. What we need to do, is send a simple message to their company Campfire room whenever someone creates a new case in our application to let the others know.
Pre-Requisites
To follow this basic guide you must have a Campfire account with 37signals. You need your accounts subdomain, your accounts API access key and the numerical ID of the room you want to send the message to. You can get this number by visiting the room and copying the number from the URL. It should be the only number in the URL, after the final forward slash.
Setup
Add the following to the Kase.rb model.
# Campfire Post-Commit def sendtocampfire post_commit :campfire do authorize :subdomain => "YourSubDomain", :token => "YourAPIToken", :room => 'YourRoomNumber' post "Enter your message here.", :type => :text end end
Add the following to kases_controller.rb.
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
respond_to do |format|
@kase.sendtocampfire if params[:send_to_campfire]
#flash[:notice] = 'Record was successfully created.'
flash[:notice] = fading_flash_message("Record was successfully created.", 5)
format.html { redirect_to(@kase) }
format.xml { render
ml => @kase, :status => :created, :location => @kase }
end
end
Note: If you have already generated the scaffold for your model, just add the following to the def create part.
@kase.sendtocampfire if params[:send_to_campfire]
Finally, and optionally, add the following to your view just before the submit button.
< % = check _box_tag :send_to_campfire, 1, true %> Send Case to Campfire?
Fin.
Now whenever someone creates a Kase/Case a message will be sent to Campfire, but only if the “Send Case to Campfire?” select box is ticked.
Campfire accepts the following hooks:
post "Some message" post "Some message", :type => :text post "Some pasted code", :type => :paste post "http://twitter.com/johndoe/statuses/12345", :type => :twitter
In case this helps anyone else, this is the format FreeagentCentral requires to import CSV files:
01/01/2010, 50.00, "Explanation here" 02/02/2010, 28.21, "Explanation here"
or
01/01/2010, -50.00, "Explanation here" 02/02/2010, -28.21, "Explanation here"
Obviously, the top is for deposits and the bottom for expenditure.
I have found that working with CSV’s exported from MS Excel is as follows:
Import into Numbers
Arrange columns as required (Date, Value, Explanation)
Save as CSV
Open in Coda
Blockedit the columns to alter date format and add symbols where necessary
http://www.freeagentcentral.com
Up until the end of 2009 I created folder upon folder of website iterations and versions. Numbering them by version number and crying if I messed the numbering system up (which was a regular occurrence). If a client wanted to go back to a previous version of the content but couldn’t remember how the content was worded I would spend ages sifting through the folders hoping to hell I had kept the folder that we needed.
Every time the Apple online store went ‘down’ while the magicians worked in the background I wondered how they instantly switched from one version to another. With changes on every page of the store plus additions to categories etc. they can’t have been doing a simple folder removal and upload via FTP, mainly because it would have taken hours. Never mind if something went wrong, how would they fix the problem in less than a few hours. I strongly suspect that the Apple store is actually usually offline for longer than necessary to instill media hype around potential new products and services. Even so, however they pull this off – its smart.
I asked on Designate Online how people though this was done, I suspected it was SVN but I didn’t really know what SVN actually was. As usual, the folks at DO offered a fair amount of advice and suggestions. To be honest, I didn’t really understand most of it – but the bits I did pointed me in the right direction. As stupid and rash as it seems, I purchased Versions for Mac pretty much immediately. Even though I didn’t fully understand SVN, I did want to use the right tools and I really learn best when playing around – rather than reading tutorial after tutorial and getting swamped with information.
It was immediately apparent that I didn’t know enough to set my own versioning server up so I had to find someone who had been through all this before and had used their knowledge to make my life easier – enter Beanstalk.
Put simply Beanstalk is the behind the scenes magic that I wanted – just like Apple use. As you can imagine Apple won’t use a setup such as Beanstalk – they most probably have racks of servers just for that, but, I’m not Apple. I have neither the funds nor the knowledge to create a setup like that. Beanstalk are the wheels and Versions for Mac is the interface to make it all work like it should.
Unlike most Mac users – I very rarely use Terminal.app. I don’t know why way around OS X, nor do I really need to learn. So, how the hell do I make things work – well – frankly, its a little bit of a botch job, but in the main its all pretty smooth and I have to be honest I’m loving it.
Update: NetTuts have just created a really useful screencast explaining the basics to Versions in the Git format.
http://net.tutsplus.com/videos/screencasts/terminal-git-and-github-for-the-rest-of-us-screencast/