Courtesy of Twitter

What A Weekend!

July 25th, 2008 by Radar

And it hasn’t even begun!

Here I sit on a Friday night, on my bed with two quilts over the lower-half of my body. Sitting by the open window (yes, open. Even though it’s winter and 7.7 celcius outside) on my bed with my laptop on top of the quilts. My mum’s boyfriend’s son sits on the only chair in the room, using UbĂ«rComputer and playing Spore Creature Creator, creating a dinosaur like creature. I feel relaxed, probably due to me getting off work a bit earlier than usual because the internet was balls (dialup had better ping times), and I know that there’s going to be a lot of stuff to do on the weekend.

Tomorrow I’m going to be attending , not because I like dressing up in a Sailor Moon costume (not that I’m going to of course), I just want to see what all the fuss is about. Two of my friends (Brenton and James) have both recommended in one way or another that I go, so I’m going. Hopefully I’ll enjoy myself, but it is an anime andvideogames conference, and I’m not all that in to anime…

I’ve been tasked (is this the correct word?) to host Railscamp #4! I have an idea for a venue and I’m going to check it out on Sunday. Hopefully if all goes according to plan there should be a formal announcement soonish. Price and what not is not worked out yet, but I have a good idea and I’m not going to tell you! I’ll have a better idea Sunday night, if I can be bothered blogging about it.

See you on the other side!

Faking It

July 14th, 2008 by Radar

Today I was doing the one thing I truly, truly love doing and that’s complaining about writing RSpec tests. I came across a doozy of a problem involving RSpec testing and faking subdomains. Here’s a stripped down version of what I did:

def login_as(user)
  session["user"] = users(user).id
  request.host = User.find(session["user"]).company.domain + ".example.com"
end

Just pop that into your spec/spec_helper.rb and then you can use login_as(:user) which will find the fixture with the name of “user” and then go from there to setting your faked host as being from, for example, blah.example.com.

Pretty simple, shame Google didn’t turn up any relevant results without me having to dig deeper than usual.

Empty Wallet Treatment

July 6th, 2008 by Radar

Thanks to the guys at NetFox (Yuji or Adam) for the name for this post.

Tomorrow I go to the dentist to have empty wallet (read: root canal) treatment (hereafter referred to as EWT). My tooth that broke at Railscamp has a serious bout of decay in it no thanks to me not caring for my teeth as I really should have, for quite too long. Consider this a warning to all the readers of this blog to look after your teeth. It’s going to cost me a fair bit (thankfully I save money, rather than spend), and thanks to a complete lack of Dental insurance I’ll be paying through the nose (or is that the mouth?). I am not in any way looking foward to the needles (notice the plural!) that are going to be going into my gum tomorrow morning. The best part is that a client is coming in tomorrow at 10:30 and I’m one of the people working on their project, so I’ll be [attempting to] speak with them. Perhaps I could type and get the text-to-speech voice on the MBP to read it out, or something.

Look after your teeth, please. You are not invincible, and neither am I.

RailsCamp ‘08 (#1?)

June 28th, 2008 by Radar

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

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.