Aug
11
2009
2

Rails Bugmash August 2009: Before, During & After

This past weekend I was sitting at home, kids in bed, staring outside at the lovely (i.e. miserable) weather we are living with up here in Manitoba this summer. I popped open my MacBook and headed over to Stackoverflow.com to see if there were any questions I could help with. After 20 minutes or so of staring at the screen I somehow remembered the blog post about the Rails Bugmash. I headed over the RailsBridge Wiki to check it out. That’s how it started…

Before

I have been using open source tools pretty much my entire career as a web developer. I have been using Ruby and Rails for about that last 4 years. I never considered getting involved in trying to “fix” Rails. I’ve talked about the reasons for this before so I won’t waste more time on it here.

The points that pulled me in this time was the perceived level of support I would have should I decide to take a chance and actually try and tackle one of the identified bugs. In the past, I have always felt very detached from core development when all I did was throw a ticket into a tracking system and then wait to see if anyone responds. It all seems very hands-off, like two people lobbing a ball over a fence to each other (which is probably a by-product of asynchronous communication). The description of the Bugmash made me believe that this time live support was going to be there if you needed it. For me, that was a powerful motivator to give it a shot.

During

On Saturday evening I got started by going to the bug list, scanning through them and trying to latch onto something I understood. I logged into the irc channel and started watching the conversations develop and quickly realized my expectations were going to be met. Here was a group of people who were ready and willing to work together and help anyone who wanted it. Confidence boosted, I jumped back to bug list and headed for the last page. I started grabbing incomplete tickets because they seemed to be the ones that were pretty much untouched. Ticket #767 was the first one I verified. I started trying to figure out how to patch it, but got lost in the tests (and started to fall asleep). I headed back to irc, posted a message about what I had found, stopped work on the ticket and immediately someone picked it up. Hey, that felt good.

Sunday evening started off a little different. You see, I didn’t read the full wiki post about the Bugmash before I started work on Saturday. I had no idea there were points being offered or that there were even prizes! Finding that out that I had amassed 175 points, pretty much by accident, certainly increased my motivation. I found a ticket that I thought I could handle as quickly as I could. #430 was the lucky one. I verified the current patch, which would not apply to 2-3-stable or to master, and posted my findings to irc. A couple people checked the ticket and commented that it seemed like a nice feature and asked me if I was going ahead. I did (as by this time I wanted to get a changeset and 1000 points). Fixing the patch for 2-3 was fairly easy, but a patch for master required a rewrite due to the refactor to ActiveModel. Fortunately, Elliot Winkler, the original patch creator did a great job on the patch and the tests and once I found where in ActiveModel it needed to go it slid in nicely. Sunday night’s work ended by attaching the patches to the ticket, notifying the irc channel, commenting on a couple more tickets before signing off.

After

Monday morning I found myself watching the commit logs for Rails to see if the patch that I had worked on would get put in. I was a little disappointed to find that it hadn’t. That didn’t last long, a short time later I saw this added to the ticket. Core decided that it was best to just be in master so that’s where it went. From now on I can say I contributed to Rails (in a very small way with much help from others, but I contributed nonetheless!). That was definitely the high point of the whole experience.

Since then I have found myself watching the remaining tickets on the bugmash list as well as the list of all Rail’s tickets. I am trying to comment on the ones that I feel I have some understanding of, but the feeling of disconnect is slowly creeping back. I am still on a high from the changeset and am going to do my best to not let it wear off.

Look Back

The bugmash was quite the experience and a will be helping out again the next time it comes around. In fact I am going to encourage the developers that I work with to get involved too. I think it was organized and operated very well, kudos to the organizers at RailsBridge for that. The support structure that was in place to help developers was all that I hoped for. I may have been the only one in Winnipeg involved, but I didn’t feel alone. I loved the points system, but that’s just my nature. I certainly was not in it for the prizes, but I will admit that it did motivate me. I also enjoyed reading the irc chat and all the twitter messages and blog posts that were spawned by the event. It was nice to know that many of the people involved were first-timers, too. A point I think can’t be stressed enough.

In the end, I earned a little about 1300 point and more importantly I was motivated to learn about the Rails framework at a level that I have been avoiding for to long, in the code. I want to keep up the momentum from here. Hope to see you at the next one!

Written by Peer in: Programming, Rails, Ruby, Uncategorized | Tags: , ,
Jun
09
2009
0

Rails ActiveRecord callback gotcha!

My team came back to me the other day and said that the ActiveRecord callbacks in our Rails application did not work. They had discovered that when they initially created the new object everything worked fine. However, once they tried to update the same object the column would not be updated by the callback. Here is an example of what they had:

1
2
3
4
5
6
7
8
9
10
11
12´
13
14
15
16
17
18
19
20
21
class Book < ActiveRecord::Base
  belongs_to :author
    
  before_save :set_author
  def set_author
    if lang == 'fr'
      author_id = 9984
    else
      author_id = 5214
    end
  end
end

# Console
>> book = Book.new(:title => 'Mine', :isbn => '3323')
>> book.save
>> book.author_id
=> 5124
>> book.update_attributes(:lang => 'fr', :isbn => '2830595')
>> book.author_id
=> 5124

That is definitely not right. I didn’t believe it at first, if this was a bug how could it possibly have been missed. I mean this is core ActiveRecord, its been in Rails as long as I have been using it (almost 4 years). I had to figure out what was going on, but I won’t bore you with the details of my experimentations. Here is what I did to get it to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Book < ActiveRecord::Base
  belongs_to :author
    
  before_save :set_author
  def set_author
    if lang == 'fr'
      self.author_id = 9984
    else
      self.author_id = 5214
    end
  end
end

# Console
>> book = Book.new(:title => 'Mine', :isbn => '3323')
> book.save
>> book.author_id
=> 5124
>> book.update_attributes(:lang => 'fr', :isbn => '2830595')
>> book.author_id
=> 9984

Can you see the difference? That’s right its the addition of self! I must admit I am not entirely sure why it didn’t work the other way and if you know please fill me in. I am sure some of you are thinking that write_attribute would have been an effective means to do this too and you are correct. If you prefer that method I would like to know why.

Update turns out that ruby takes the author_id=1 as setting a local variable and self.author_id=1 as making a method call. Which makes perfect sense when you think about it see “Use self explicitly” for more details. (thanks to Craig Webster and Florian Gilcher, via Twitter for clearing this up for me)

For the extra curious, check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Book < ActiveRecord::Base
  belongs_to :author
  
  def author_id=(value)
    raise "Gotcha! #{value}"
  end
  
  before_save :set_author
  def set_author
    author_id = 9984
  end
end

# Console
>> b = Book.find(2)
=> <Book ...>
>> b.update_attributes!(:title => 'Other (again)', :isbn => '22343')
=> true
>> b.update_attributes!(:lang => 'en')
=> true

Wouldn’t you expect to see a raised exception in there somewhere? Add self and you get the exception. Use write_attribute and you don’t. (Update of course you would understand why if you read the above update)

Written by Peer in: Programming, Rails, Ruby | Tags: , ,
May
05
2009
0

Little known facts #1: gem search

Hopefully I can start filling out this blog with little known facts about various things. Now when I say little know facts, I mean little known to me, so if you already knew this they why didn’t you make blog post so that I could find out from you :-) .

First up is some Ruby, specifically Rubygems. Did you know that there is a “search” command in gem? I didn’t. As a result I found myself doing this all the time:


pallan.local:~/gems $ gem list | grep ldap
ruby-net-ldap (0.0.4)
pallan.local:~/gems $

Thanks to this wondrous discovery, now I can do this:

pallan.local:~/gems $ gem search ldap

*** LOCAL GEMS ***

ruby-net-ldap (0.0.4)

You can also search remotely

pallan.local:~/gems $ gem search ldap -r

*** REMOTE GEMS ***

activeldap (1.0.2)
activesambaldap (0.0.6)
ambitious-activeldap (0.1.1)
dm-ldap-adapter (0.2.0)
jeremyevans-simple_ldap_authenticator (1.0.0)
jruby-ldap (0.0.1)
ldapmapper (1.4)
passiveldap (0.1)
ruby-activeldap (0.8.3.1)
ruby-activeldap-debug (0.7.4)
ruby-ldapserver (0.3.1)
ruby-net-ldap (0.0.4)
sdague-net-ldap (0.0.4.1)
ucb_ldap (1.3.2)
pallan.local:~/gems $

And don’t forget the detailed view

pallan.local:~/gems $ gem search ldap -d

*** LOCAL GEMS ***

ruby-net-ldap (0.0.4)
Author: Francis Cianfrocca
Rubyforge: http://rubyforge.org/projects/net-ldap
Homepage: http://rubyforge.org/projects/net-ldap
Installed at: /Library/Ruby/Gems/1.8

A pure Ruby LDAP client library.
pallan.local:~/gems $

Now go save yourself some keystrokes!

Written by Peer in: Programming, Ruby | Tags: ,
Apr
26
2009
1

Giving back (and it’s about time!)

For most of my online development career I have been using open source software. I have been doing this for almost 10 years not and not once had I taken the chance to give back to the community that supported me so much over that time.

Why not? Well, most of it was fear that I was not “good enough” to contribute and the high and mighty maintainers of the code would look at patch and have a great big laugh before rejecting it.

I’m over that now and I want to thank the many talented developers that I have worked with over the past year for showing me how. They taught me not to be afraid anymore. Here’s the proof.

Thanks guys.

Written by Peer in: Uncategorized |
Apr
23
2009
0

Rails Annotations Snippet for TextMate

My team tends to use the annotations in rails alot. Getting comments into the code that are easily parsed out into a nice list from a built in rake task is great (for lazy developers like myself). Entering them is a different story. Not that it’s hard, but with many members of the team entering them all the time you end up having to add on additional information, like signatures and timestamps.

It is a very repetitive process. Sounds like a TextMate snippet will fit the bill nicely. The following instructions will show you how you can setup your own snippet that will generate annotations similar to this:

# TODO: we have to add that calculation (pa, 01-02-2009)
# FIXME: this value can never be modified (pa, 23-04-2009)

1. Go to the Menu Bundles -> Bundle Editor -> Show Bundle Editor…
2. In the left sidebar click on “Text”
3. Click on the left plus “+” sign and select “New Snippet…”
4. The new snippet will be selected, give it a new name (like Annotation)
5. Hit tab, to deselect the snippet, then click on it to select it
6. Replace the contents of the large text box with this “# ${1:TODO}: ${2:comment} `date “+(pa, %d-%m-%Y)”`$3” (change ‘pa’ to your initials)
7. Change the “Activation” selector to “Tab Trigger” if it isn’t already and put in ‘an’ (or whatever pattern you want to trigger it)
8. Hit tab, to deselect your snippet
9. Go to the Menu Bundles -> Bundle Editor -> Reload Bundles
10. and you’re done!

Open a new file in TextMate, type ‘an’ and hit tab. You should get a nice annotation to appear with “TODO” already selected. Change TODO to the annotation you desire and hit “tab” again, “comment” will now become selected.

All that said, I am not taking credit for this snippet it was provided to me by a member of my team and I am not sure where they got it from. Just wanted to spread the word!!

Written by Peer in: Uncategorized | Tags: , , , , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes