Courtesy of Twitter

I am so freaking awesome

June 24th, 2008 by Radar
  (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

June 20th, 2008 by Radar

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

June 13th, 2008 by Radar

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

June 10th, 2008 by Radar

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

June 7th, 2008 by Radar

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

June 6th, 2008 by Radar

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

June 6th, 2008 by Radar

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?

June 5th, 2008 by Radar

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.

Availability is unavailable

May 29th, 2008 by Radar

http://thesaurus.reference.com/search?r=20&q=availability

See for yourself.

Mac: The First Two Hours

May 25th, 2008 by Radar

So today as a kind of a reward for working hard and kind of I’ve always wanted one, I bought myself a MacBook Pro. It’s a 15.4″, 2.4ghz, yada yada, brand new MacBook Pro.

I took the train in to town (after being placed on hold for 10 minutes) and, thanks to Google Maps’s wonderful position of Next Byte (saying it was on the southern side of Rundle Mall when it was actually on the northern side), I accidentally stumbled into the store whilst looking for it. The guy who served me was more than happy to go upstairs and get one… but when he returned the serial number didn’t match. So he went to get another… and the serial number still didn’t match! Each time taking about 3 minutes. Third time lucky, he came back and said “I know this is in the system”, and I bought it on the spot. 

It’s now two hours after I’ve gotten home. It was weird at first having a perfectly good computer in front of me and not knowing what to do with it, but once you play around with it for a little while things (simple and not so simple) become obvious. The dock is probably my favourite feature so far, even if I did have to make it about 33% of it’s original size. I’ve also managed to install Git!

 

The downloads box is another good feature, but it needs some more notification, like what the Colloquy window has when someone says your name. I love the way you install programs, so non-fuss. I click once on the downloaded file and it’s already there for me to use.

If the ambient light is bright enough, the keyboard keys are not illuminated and the screen gets brighter automatically so you can see in lighter conditions. There is a certain limit to how dark the screen gets, so you’re not going to be adjusting the brightness if you’re sitting in a dark cave somewhere.

The magnetic power plug is good too, and I know full well the damage that can be caused to a laptop when you trip over a cord that’s plugged into one; I recently destroyed the headphone socket when I tripped over the cord coming out of my Toshiba laptop. I know that if I trip over the Mac’s power cord, it should come out right away. The annoying thing I find about it is the light… but I guess I’ll get used to that.

The terminal was originally hard to find (I am/was/will be a Mac Noob), but eventually I did. I love how the commands are so unix like, such as sudo to do stuff as root and… just about any unix command you can think of.

Another nice touch is having the menu bar at the very top of the screen rather than the application, so whatever application you’re in it’s always in the same spot and generally the menus seem to be laid out the same way.

One last thing, *everything* looks so god damned shiny. It’s awesome. The fonts are all anti-aliased (except the terminal’s by default, not too hard to change) and the buttons all have that classic Mac styling we’ve come to love and envy.

All in all, a very nice system and worth the money.