Posts Tagged ‘ruby’

Faking It

Monday, July 14th, 2008

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/spechelper.rb and then you can use loginas(: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.

More Migration Sexiness – remove_columns & add_columns

Wednesday, December 19th, 2007

After seeing a post on errtheblog about them making migrations sexier, I was inspired on the train ride this morning to write something similar. I had a table and I wanted to drop many columns for it and I ended up writing something like:

 removecolumn :table, :column1

removecolumn :table, :column2

removecolumn :table, :column3

removecolumn :table, :column4...

and by now you’re starting to get the idea. I have to not only type out remove_column four times, but also the table name! What a waste of time!

So I hacked up some code and put it into lib/custommethods.rb.

module ActiveRecord
  module ConnectionAdapters
    class MysqlAdapter
      def removecolumns(tablename, *columns)
        columns.each { |column| removecolumn table_name, column }
      end

  def add_columns(table_name, type, *columns)
    columns.each { |column| add_column table_name, column, type}
  end
end

end end

So now instead of the monstrosity above I can now type:
removecolumns :table, :column1, :column2, :column3, :column_4
to remove all the columns.

Also inspired by removecolumns was addcolumns (already spied by the observant few). There’s a little difference in addcolumns compared with addcolumn, the type is now the second argument instead of the third:

addcolumns :table, :string, :column1, :column2, :column3

More Migration Sexiness – remove_columns & add_columns

Wednesday, December 19th, 2007

After seeing a post on errtheblog about them making migrations sexier, I was inspired on the train ride this morning to write something similar. I had a table and I wanted to drop many columns for it and I ended up writing something like:

 removecolumn :table, :column1

removecolumn :table, :column2

removecolumn :table, :column3

removecolumn :table, :column4...

and by now you’re starting to get the idea. I have to not only type out remove_column four times, but also the table name! What a waste of time!

So I hacked up some code and put it into lib/custom_methods.rb.

 module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
def remove_columns(table_name, *columns)
columns.each { |column| remove_column table_name, column }
end
def add_columns(table_name, type, *columns)
columns.each { |column| add_column table_name, column, type}
end
end
end
end

So now instead of the monstrosity above I can now type:

remove_columns :table, :column_1, :column_2, :column_3, :column_4

to remove all the columns.

Also inspired by removecolumns was addcolumns (already spied by the observant few). There’s a little difference in addcolumns compared with addcolumn, the type is now the second argument instead of the third:

add_columns :table, :string, :column_1, :column_2, :column_3

December Meetup

Wednesday, December 12th, 2007

There was an Adelaide Ruby Users Group meeting held last night at the usual location, the Brecnock Hotel on King William Street. This was the first meeting my friend, Tom, had decided to come to and he caught the train into town from Gawler. He eventually arrived at the other end of town at around 5:30 and phoned me up asking where I was. His voice seemed to echo too much, and the stairway in our building doesn’t echo like it did. I eventually figured out that he was in the TSR building and managed to get up to Level 2 unhindered. Great security the TSR guys have.

I finished up at work and then Anuj, Vishal, The Other Mongrel and I headed over to the Brecnock. Adam and Ali had other things to do and Michael still had his time off from work due to an operation he had.

We ordered our food at about quarter-past 6. It arrived at around 7ish in typical Brecnock style.

The meeting begun at 6:30 and begun with Luke talking about merb and Datamapper. Whilst very interestin, the talk was very long (at least an hour). I will try to grab a video of his talk once it’s up on the internet.

Afterwards I did a talk on Railscamp with 20 photos that I or other people (Lachie, Tim Lucas, Ryan Allen and Ross Hill) took at Railscamp. I rattled off a list of what presentations there were at Railscamp and this started a talk on Git. The Git talk went on for a while, and it was already 9:00 by this time so Tom and I decided to leave. Apparently the other guys only continued until about 9:15, and the talk turned to usability.

Tom thought it was really good and he said he’d definitely be going to the next one.

So why don’t you come as well?