Archive for August, 2008

A Sense of Belonging

Friday, August 29th, 2008

Numerous times I’ve needed and I’ve seen other people have needed the need to check whether an object belongs to the currently logged in user. I’ve worked out that something like this works:

class User < ActiveRecord::Base
  hasmany :posts
  def owns?(object)
    object.user == self
  end
end
This works when you have a currently logged in user and call it by using currentuser.owns?(@post). Now what if you wanted to do it the other way around? Well it’s really as simple as this:
class Post < ActiveRecord::Base
  belongsto :user
  def belongsto?(otheruser)
    user == otheruser
  end
end
Now you can reference that through @post.belongsto?(otheruser).

If you wanted to use either of these in the controller, it would be like this:

class PostsController < ApplicationController
  def edit
    @post = Post.find(params[:id])
    check_ownership
  end

def update @post = Post.find(params[:id]) if currentuser.owns?(@post) # or @post.belongsto?(currentuser) # carry on... if @post.updateattributes(params[:post]) flash[:success] = "Post updated!" redirectto topicpath(@topic) else flash[:error] = "Post could not be updated." render :action => "edit" end else flash[:error] = "You do not own that post." end end

private def checkownership if !currentuser.owns?(@post) # or @post.belongsto?(currentuser) flash[:error] = "You do not own that post!" redirectbackordefault topicpath(@topic) end end end

Now here we’ve called checkownership in the edit action which will stop the template from being rendered by calling redirectbackordefault. We can’t call (as I found out thanks to BlueSea) checkownership in the same way in the update action because the code will still be executed. So we must call the methods we defined in the model, either currentuser.owns?(@post) or @post.belongsto?(current_user).

Fired: Day 1

Thursday, August 28th, 2008

Yesterday I was given my two weeks notice by my boss, he cited personality issues. I had posts on this website explaining my side of the story and why I felt the way I did, but I have chosen to take them down on the advice that it may cost me future employment and that it also may land me in legal “hot water”. What can you say these days without getting in trouble?

I planned on riding Adelaide to Elizabeth again today. Had a meeting with the SeaLink guys about Railscamp #4 sponsorship instead and did four laps of lake torrens on my bike before the right pedal on my bike came loose.

Time to chill.

Moving Topics

Wednesday, August 13th, 2008

Worked out topic moving. Code can be seen here.

There was a big whinge about how hard it is, but I was just in a retarded state of mind. One good night’s sleep and explaining it to multiple people (thanks Justin Smestad and Brenton Fletcher!) clarifies so much.

InLAN

Sunday, August 10th, 2008

Went to InLAN last night and it was great fun. Won the TF2 comp with Devastator, Insane Gazebo (aka Precious Pergola), Cadiniller and TheScream, came second in Warsow (even though Devastator was so confident that was his place) and won a door prize of about 500g worth of chocolate.

Upon driving home on The Golden Way, during very light showers on slippery road, a stoned pedestrian decides it an appropriate time to step out onto the road. Not only was he now ON the road, but my car was also ON the road and in the same lane. A short fight ensued between a 1-ton 70-kilometer per hour moving vehicle and a 90kg pedestrian in which the pedestrian destroyed the car’s front grill, dented the bumper and bonnet, rolled up the bonnet and proceeded to smash the bottom left hand corner of the windshield before bouncing off the car and onto the road to lay unconcious. All the while, Devastator’s screaming and I’m not too calm myself. Police arrived, ambos and fire crew too. Statements, blood alcohol readings (negative) and drug tests (negative also) were taken. Hilarity did not ensue. It was cold. It was raining. It was not fun at all. I don’t recommend doing it. Pictures of my now not-so-sweet ride can be found here.

Some more information is:

  • The police were apparently looking for a guy matching his description. I was never told why. Something about a petrol station.
  • He was unconscious after I hit him and still when I dragged him (and I’ve been told I wasn’t supposed to do that) but seemed to be regaining consciousness when he was lifted into the ambulance.
  • He probably has a collapsed lung due to the impact and possible broken ribs (yes, plural)
  • I heard over the police radio mention of marijuana in relation to the description of the guy
  • My dashboard is now loose on the right, but nothing a little glue can’t hold still
  • I’m fine, Devastator sounded a bit shaky still this afternoon
  • Thank you for your concern, especially Dad who didn’t hesitate to come out and pick me up that late at night.

So, tomorrow I’m taking the day off work and taking the car to get repaired. During the time of the day that I’m not getting the car repaired I’ll probably working. On what, we’ll have to decide tomorrow.