Courtesy of Twitter

Archive for June, 2008

RailsCamp ‘08 (#1?)

Saturday, June 28th, 2008

I had an enjoyable weekend last weekend at Railscamp, and I had this whole 3-page summary written up for you readers but I’d figured I’d spare you that lengthy document (for now, ahahaha) . These are the things you need to know:

1. It was held at Kariong (I kept calling it Kai-rong), about 6km from Gosford which itself is 1 hour and 30 minutes by train from Sydney. Scenic country-side, yada yada.
2. I am in love with Sydney trains. Flippable seats and no noisy engines.
3. The venue (one main hall, one “cafeteria” hall and two separate-but-joined domitories) was exceptional. We ran everything off a number of power points that you could count on one hand.
4. The people were exceptional. The talks were exceptional. Nothing beat Pat Allen’s talk (the very last one at Railscamp 2.0), and nothing will ever.
5. Yes Guitar Hero was played. Two and Three. Yes, I won. Yes, I’m a sore winner.
6. I also coded. Gitjour became a little weekend project and it was fun working on that with Lachlan Hardy, Tim Lucas, Dr. Nic & Others.
7. Other applications included the ever-awesome Duke (Upload, Vote, Boogie), Swore-DS, and Pete Yandell’s proposal to create an application to track species in the wild.
8. Dr. Nic was obsessed with Cherry Pie. When he wasn’t playing it on Guitar Hero 2, he was humming/singing it.
9. The beds were creaky. Earplugs were my saviour.
10. Lachie had a machine that made him sound like Darth Vader whilst he slept. Very chilling. Earplugs helped here too.
11. There was a waterfall. We walked behind it. Then we bashed through the bush to camp.
12. Risked life / possible broken bones to climb a large rock face. using nothing but what nature provided. All this whilst trying to stop my camera from bashing against it. Then walked back to “camp”, only come out 500m away at the office. Stupid GPS!
13. Went away feeling fantastic.

There was talk of possibly another Railscamp by the end of the year, or another one this time next year. I’m looking very much forward to it.

I am so freaking awesome

Tuesday, June 24th, 2008
  (Dir.entries("#{RAILS_ROOT}/app/models") - [".","..",".svn"]).each do |model|
    has_many model.split(".").first.pluralize.to_sym, :foreign_key => "owner_id"
  end

I tried adding:

 if model.classify.is_a?(ActiveRecord::Base)
      <<-EVAL
      class #{model.classify}
        belongs_to :owner
      end
      EVAL
    end

too, but Rails didn’t like that.

Going Dark

Friday, June 20th, 2008

I won’t be online for the next four days, I’ll be at Railscamp instead.

I need you not internet.

A bit of refactoring love

Friday, June 13th, 2008

Find, Find, Find, Find, I don’t think so…

As explained in previous posts, Rails controllers have 7 default actions (index, new, create, show, edit, update, destroy). Four of these seven actions make the same find call, Model.find(params[:id]) and this tutorial is to tidy that up so you’re not repeating yourself over four different actions. To clean this up we’ll just call a before filter:

class ForumsController < ApplicationController
  before_filter :find_forum
 
  # Actions go here
 
  private
    def find_forum 
      @forum = Forum.find(params[:id])
    end
end

Now you may be thinking, “Why are we doing that? That’s 5 lines!”. Think about if you wanted to change the find statement, and now you’ll begin to picture why. Changing one line is much easier than changing four. For example, if I wanted to find forums by their slugs instead of an ID I would simply change @forum = Forum.find(params[:id]) to @forum = Forum.find_by_slug(params[:id]). Of course, for this to work with the restful routes helpers the way we expect it to (e.g. forum_path(@forum) -> /forums/the-first-forum), we’ll need to re-define #to_param in our model:

class Forum
  def to_param
    slug
  end
end

Common Lookups

Sometimes you’ll have data initialised for your forms and you’ll want to initialise this data multiple times. Instead of repeating yourself like this:

class ForumsController
 
  def new
    @forum = Forum.new
    @something_special = SomethingSpecial.find(:all, :order => "id DESC")
  end
 
  def create
    @forum = Forum.new(params[:forum])
    if @forum.save
      flash[:success] = "A forum has been created."
      redirect_to @forum
    else
      flash[:failure] = "A forum could not be created."
      @something_special = SomethingSpecial.find(:all, :order => "id DESC")
      render :action => "new"
    end
  end
 
end

You could instead have:

class ForumsController
 
  def new
    @forum = Forum.new
    common_lookups
  end
 
  def create
    @forum = Forum.new(params[:forum])
    if @forum.save
      flash[:success] = "A forum has been created."
      redirect_to @forum
    else
      flash[:failure] = "A forum could not be created."
      common_lookups
      render :action => "new"
    end
  end
 
  private
    def common_lookups
      @something_special = SomethingSpecial.find(:all, :order => "id DESC")
    end
end

Shorter Routing

One last thing that I’d like to show you is shorter routing. Ever since the restful routing helpers were added, routing to specific controllers and their actions has become easier and easier. Rails 2.0 makes it extremely easy, but first we’ll see how far we’ve come:

  1. <%= link_to @forum, { :controller => “forums”, :action => “show”, :id => @forum.id } %>
  2. <%= link_to @forum, forum_path(@forum) %>
  3. <%= link_to @forum, forum_path %>
  4. <%= link_to @forum, @forum %>
  5. <%= link_to @forum %>

As long as there’s a #to_s method in the Forum model it will insert that as the phrase shown to the user for the link. All of the above should produce the same URL, with the exception of the first which will produce /forums/show/1, and going down the list they’re just shorter ways of writing the same thing. If you had nested routes such as forum_topic_path(@forum, @topic) you could do <%= link_to @topic, [@forum, @topic] %> as the extremely short version of it. The reason why we can’t do just <%= link_to [@forum, @topic] %> is because this will show the to_s version of @forum, followed immediately by the to_s version of @topic.

Change of Plans #2

Tuesday, June 10th, 2008

Ok, RadarLAN is back on again.

InLAN’s venue is apparently undergoing “renovations” so that’s not going to be in use until July sometime. So, RadarLAN is back on! Saturday, 6:30pm, same bat-place. Darling brother has decided to hold a party on the same night, depsite my one month notice. Oh well. It’s at mum’s house.

SVN: Why I moved away, and why you should too

Saturday, June 7th, 2008

At my new work, we use SVN for all our projects, and we did the same thing at SeaLink too. The only difference being during that time in between SeaLink and NetFox, I was introduced to git.

Git is, in my opinion, a far superior “product” than SVN was, is or ever will be. It commits faster, it doesn’t whinge (read: completely block you from committing) when you manually rm directories instead of using their propriatary rm command, and you can type one command to add in all new files, all changed files and your commit message in one fell swoop.

Git is incredibly fast. If there was a race between the speed of light and git, git would win. I had a race between git and svn out of interest for a work project, work’s project took about 5 minutes to commit, whereas git took about 30 seconds.

Need more reason? Okay then. Ever played around with svn propset in regards to setting svn:ignore. Ever realised how much of a pain in the ass it is? Me too! Wow. Git has this one file called .gitignore where you specify the relative path to the files to ignore. Say I have a log directory and I want all files in it with the extension .log. I would simply put “log/*.log” in my .gitignore file in the root of my project, and it would know what I’m talking about. “So what about two directories deep?”, I hear your nasily voice ask. Oh, that’s just log/**/*.log, effectively blocking any folder within the log (existing or not) from having any of their .log files commited.

But wait, there’s more!

Remember those .svn directories? Yeah, all of them. There’s only one for .git, and it’s kept right where you’d expect it, in the root directory of the project. This was one of my personal huge gripes with svn. When you’d delete a folder without doing svn rm, it would delete the .svn folder contained within it. So you’d go to commit it, and SVN would block your commit. You know what git does? Git tells you that the file is gone, but still lets you commit everything. Yeah, I know it’s awesome.

Tried merging and branching svn repositories? You’re right, painful is not the right term. Excruciating does not even come close. Git does this beautifully. Type “git branch experimental” and it’s there! Where experimental can be any name, I highly recommend naming them after characters from the Lord of the Rings series, or Star Wars. Wow, that was hard. Then you’ve got to checkout the new branch, “git checkout experimental”. Now I’m working on the new branch. No seperate folder containing the branch code, I don’t need one. Oh, and merging: “git merge ‘your message’ head experimental”, and there you have it.

One final reason: Rails has moved away from SVN to git. Many rails plugins have also moved away. I personally hope SVN will die a quick and painful death, to be replaced with a better SCM.

Change of Plans

Friday, June 6th, 2008

Friends and stalkers, instead of having a RadarLAN at my dad’s house on the 14th, I will now be attending InLAN instead. For more information please visit http://www.inlan.net.au. I’ll see you all there. Coincidentally, this is the same weekend before I go away for a weekend, so consider this my going away party.

RadarLAN is back on. Please see Change of Plans #2.

Train Timetables

Friday, June 6th, 2008

Been doing a little study on the trains, coded up a little rails application and out of curiosity I’m recording the stations, what time a train should get to a station and what time it does. Pretty simple really.

And here’s the results so far.

If there’s anybody else out there that journeys on the trains, I’d be interested if you could collect data for me for whatever line you’re travelling on. You don’t have to have a laptop, all I need is the stations and the time it stopped at them and what train you’re on.

Which Came First?

Thursday, June 5th, 2008

Today a question was asked by the work experience kid that made me think what in Ruby gets called first, a local variable or a method. The answer can be explained by this short irb example (thanks to Yuji Yukoo, a work mate of mine):

irb(main):001:0> a = "b"
=> "b"
irb(main):002:0> def a
irb(main):003:1> "a"
irb(main):004:1> end
=> nil
irb(main):005:0> a
=> "b"
irb(main):006:0> a()
=> "a"

Firstly we define a local variable called a, and then a method called a, and then we type a and press enter, and the local variable is outputted rather than the method. Next we call a() which outputs “a”, the code defined within the method a. It’s just one of those interesting things of note.