DevChix’s narwen recently blogged about How rails has changed how I seek for a job and writes about how having a strong web presence is so important to your career. And I say “I feel ya, girl!”
Before I came to the Berkman Center, I had never heard of Open Source or Social Media. How could I ever have been so so unconnected! But this brave new world of ‘cultivating a comprehensive web presence’ is more difficult than it would seem. It’s more than just witty tweets and a profile on Facebook.
Seems like everyone in the ruby on rails community has made significant contributions to rails core, accrued a number of speaking engagements and written a book. And while I guess not everyone is expected to participate at that level, I really should be attending those Tuesday night meetups, uploading that bugfix to Github and blogging about my daily programming achievements.
While I recognize that it’s become more important than ever to reach out to your community, make friends and build your reputation, sometimes it feels like being a developer is a lifestyle choice rather than just a career.
January 8th, 2010
ZDNet has recently published a series of blog entries and finally a video outing HP about the problems with their Pavilion 6000 and 9000 notebooks. Complaints on the HP support forum apparently only go back till June 2007 but I’ve been experiencing problems since my laptop arrived at my home in September 2006.
The wireless card suddenly stops being reconized and wireless connectivity starts breaking down. I live without the wireless by using a LAN connection and after a while the video cuts out as well making the laptop unusable. It’s happened to me four times (Sept 2006, Summer 2007, Sept 2008, and Dec 2009) and its always the same. First the wireless goes out and maybe a week or two later the screen goes black.
Under warranty, I’ve sent my laptop back to HP and have had it returned fixed each time only to have the same problem return later. Now that I let my warranty run out, tech support tells me that I’m *S*O*L*even though I’m complaining about a reoccurring problem. Because the last complaint was over a year ago, I was told that this was considered a new complaint because HP can’t fix everything that goes wrong with old parts on an old computer.
What aggrevates me most, is that the last time this computer was sent in for repair for this problem in September of 2008, the tech staff insisted that my kids must have spilled juice on my laptop and that was the cause of my motherboard problem. I wonder if HP would have accused my children if I were a man? When I told them I didn’t have kids the accusation switched to, “you must have spilled coffee on it”. So which is it? Juice or coffee? Is HP saying that everyone on the hp support forum has spilled beverages on their laptops?
Obviously, this laptop deserves to be replaced. But I wonder if I’ll get the same consideration as the others since I seem to be experiencing this additional video problem as well. Either way, HP YOU SUCK and I’m never buying your products again!
December 16th, 2009
Today I committed a change to the production server that was not yet ready for prime time. Don’t laugh. Ya’all have done it or will do it at one time or another. Fortunately, I hadn’t yet restarted the server… so that bought me a little time.
A frantic Google search on roll back and revert revealed that what I needed to do is merge. Jacob Wright goes into detail in his blog How to Roll Back Changes Using Subversion.
My coworker Dan Collis-Puro, found that an alternate, simpler way to achieve the same affect is to use the update command below (where 132 is the revision number I wanted to roll back to):
>> svn update -r132 .
Worked perfectly. Thanks, Dan!
December 9th, 2009
Last Spring I hosted a talk by Jon Yurek and Dan Croak of thoughtbot, inc entitled “Test Driven Development with Ruby on Rails”. And it was PACKED!!!
Many folks understand the importance of testing, the value-add, the business benefits of TDD, that’s why we were all there. We got a thorough overview of the numerous testing products. And there’s a LOT out there… or maybe it just seems that way. But once I was back at my desk facing my app, I still had trouble getting started.
Yeah, I’ve tried a few tutorials but they often only utilize the irb command line and don’t really explain how everything fits into a real live rails application.
Testing is just one of those things where you have to just dive in and go for it. That being said, let me share with you my endeavors using the Rails Guides.
1. Open your current ruby on rails project
Yeah, I said open your current project. If you don’t get started today, working every day, it’ll never happen right? But if you don’t have a current rails app and need some help getting started check out this Rails 2.0 step by step tutorial.
Okay now that we’re set up, we’re going to create a brand new model for the purposes of this testing tutorial:
<br /><br /><br /><br />
>>ruby script/generate scaffold CoinChanger<br /><br /><br /><br />
By the way, don’t let anyone dissuade you from using scaffold. It’s much improved over older versions since 2.0 and comes packed with all kinds of goodies incredibly helpful to the newbie. You can always delete the files you don’t need later.
2. Write a Test
What already? Yeah.
<br /><br /><br /><br />
>> cd test/unit<br /><br /><br /><br />
You should see the stub of a test already here:
<br /><br /><br /><br />
require 'test_helper'<br /><br /><br /><br />
class CoinChangerTest < ActiveSupport::TestCase<br /><br /><br /><br />
# Replace this with your real tests.<br /><br /><br /><br />
test "the truth" do<br /><br /><br /><br />
assert true<br /><br /><br /><br />
end<br /><br /><br /><br />
end<br /><br /><br /><br />
I didn’t find this too terribly helpful in getting me started. Let’s instead replace the stub code with this:
<br /><br /><br /><br />
require 'test_helper'</p><br /><br /><br />
<p>class CoinChangerTest < ActiveSupport::TestCase</p><br /><br /><br />
<p>def test_return_correct_change<br /><br /><br /><br />
#setup<br /><br /><br /><br />
#exercise<br /><br /><br /><br />
#verify<br /><br /><br /><br />
#teardown<br /><br /><br /><br />
end</p><br /><br /><br />
<p>end<br /><br /><br /><br />
So, require ‘test_helper’ specifies the default configuration to run our tests. It appears in every default test file. Doesn’t seem DRY to me. I betcha there’s a config file in the augmented testing frameworks where this would belong. But for now, we’ve leave it.
Next, you’ll notice that our CoinChangerTest class is extended by ActiveSupport::TestCase. Now, this is a little different than what you might find in those irb examples I mentioned before. Those examples extend Test::Unit::TestCase. Now that’s totally fine when you’re running a bunch of isolated files in a tutorial, but not when you’re working with a real rails app.
The class ActiveSupport::TestCase extends Test::Unit::TestCase and gives you all the testing goodness you need to run fixtures. We won’t be playing with data and fixtures in this tutorial (stayed tuned for future blog posts) but we’ll use the ActiveSupport class since we’re testing a full fledged rails application.
After that we see a pattern that will guide us through every test we write:
- setup
- exercise
- verify (assert expectations)
- teardown
So, here is an example of a unit test:
<br /><br /><br /><br />
require 'test_helper'</p><br /><br /><br />
<p>class CoinChangerTest < ActiveSupport::TestCase</p><br /><br /><br />
<p>def test_return_correct_change_in_pennies<br /><br /><br /><br />
#setup<br /><br /><br /><br />
@changer = CoinChanger.new<br /><br /><br /><br />
#exercise<br /><br /><br /><br />
hash = @changer.change_me("$5.87")<br /><br /><br /><br />
#verify<br /><br /><br /><br />
assert_equal 2, hash[:pennies]<br /><br /><br /><br />
#teardown<br /><br /><br /><br />
# delete files, close network/database connections<br /><br /><br /><br />
end</p><br /><br /><br />
<p>end<br /><br /><br /><br />
In the example above, we setup an instance of my model class:
<br /><br /><br /><br />
@changer = CoinChanger.new<br /><br /><br /><br />
Then we exercise the method which should return a hash:
<br /><br /><br /><br />
hash = @changer.change_me("$5.87")<br /><br /><br /><br />
Then verify that the implementation works correctly by asserting expectations :
<br /><br /><br /><br />
assert_equal 2, hash[:pennies]<br /><br /><br /><br />
Finally it’s time to clean up. There is nothing to teardown in this example. If the test were creating files or network/database connections, then we’d use the teardown method to close out:
<br /><br /><br /><br />
assert_equal 2, hash[:pennies]<br /><br /><br /><br />
3. Run a Test
Go ahead. Try it out.
<br /><br /><br /><br />
>> cd test<br /><br /><br /><br />
>> ruby unit/coin_changer_test.rb<br /><br /><br /><br />
Oh, dang! Get an error message? When you’re testing a rails application, you need to be sure that your database.yml is set up and pointing to a valid database. Those of you who just created a brand new rails app, you’ll need to edit your database.yml file. I’ll assume you can do this on your own, seriously, take a look at the Rails 2.0 step by step tutorial.
Let’s try again.
<br /><br /><br /><br />
Loaded suite unit/coin_changer_test<br /><br /><br /><br />
Started<br /><br /><br /><br />
E<br /><br /><br /><br />
Finished in 0.219 seconds.</p><br /><br /><br />
<p> 1) Error:<br /><br /><br /><br />
test_return_correct_change(CoinChangerTest):<br /><br /><br /><br />
NoMethodError: undefined method `change_me' for #<br /><br /><br /><br />
unit/coin_changer_test.rb:11:in `test_return_correct_change'</p><br /><br /><br />
<p>1 tests, 0 assertions, 0 failures, 1 errors<br /><br /><br /><br />
Uh, oh. We forgot to create our change_me method in our CoinChanger model. Let’s do that now.
<br /><br /><br /><br />
class CoinChanger<br /><br /><br /><br />
def change_me( dollar_string )</p><br /><br /><br />
<p> #transform dollar string into change<br /><br /><br /><br />
return {}</p><br /><br /><br />
<p> end<br /><br /><br /><br />
end<br /><br /><br /><br />
And rerun our test:
<br /><br /><br /><br />
Loaded suite unit/coin_changer_test<br /><br /><br /><br />
Started<br /><br /><br /><br />
F<br /><br /><br /><br />
Finished in 0.344 seconds.</p><br /><br /><br />
<p> 1) Failure:<br /><br /><br /><br />
test_return_correct_change(CoinChangerTest) [u<br /><br /><br /><br />
expected but was<br /><br /><br /><br />
.</p><br /><br /><br />
<p>1 tests, 1 assertions, 1 failures, 0 errors</p><br /><br /><br />
<p>
Well, now we’re getting somewhere! The test failed because it’s returning an empty hash. Let’s give our test what it wants:
<br /><br /><br /><br />
def change_me( dollar_string )</p><br /><br /><br />
<p> #transform dollar string into change<br /><br /><br /><br />
return {:pennies => 587}</p><br /><br /><br />
<p> end<br /><br /><br /><br />
So, now if we run the test again… hopefully we see this:
<br /><br /><br /><br />
Loaded suite unit/coin_changer_test<br /><br /><br /><br />
Started<br /><br /><br /><br />
.<br /><br /><br /><br />
Finished in 0.219 seconds.</p><br /><br /><br />
<p>1 tests, 1 assertions, 0 failures, 0 errors<br /><br /><br /><br />
This means the test passed and was successful. Yay!
Hopefully this is enough to get started. Stay tuned for more on testing!
November 25th, 2009
I’ve been bumping into weirdness when I click on links to download various plugins from Github. Even when I script/plugin install, all I get is an empty folder.
Turns out that this is just more wacky Windows behavior. RubyizednRailified explains in his post, Installing Rails plugin from Github on Windows. Thanks!
November 17th, 2009
This post is more a placeholder for some SQL that I often need. It’s common to want to copy the contents of one table into another. While there are some guis out there that will do this for you, they often don’t allow you to select partial contents or a variety of extras that you may need.
What annoys me about having worked with numerous flavors of SQL and databases is that the syntax tends to differ slightly. On some dbs, this works just fine:
select *
into table1
from table2
where id = 1
But some dbs expect that table2 is some sort of variable rather than another table containing the same columns. So on others (like MySQL), you need to do it this way:
insert into table1
select *
from table2
where id = 1
November 12th, 2009
The Berkman Center has friends all over the world and just about every website we have requires language support.
We recently had an issue with RedCloth and the Arabic character set. When saving content, line breaks disappear when you create a new RedCloth object. It’s as if the Arabic line break character is ignored.
I cut and paste my Arabic document into the text area box on the front page of the RedCloth site. And the line breaks show up fine there. So, the ajaxy bit works fine. Perhaps the culprit lies in the .to_html method.
This could be an issue with the Windows-1256 charset for Arabic. I wouldn’t be suprised if the problem doesn’t occur on a Mac which I assume uses ISO 8859-6.
Anyway, the workaround is easy. Just use html line breaks: <br />
UPDATE: Had friends test this out on the mac using a variety of browsers and still had the same problem. On either Windows or Mac, typing the enter key using the Arabic charset uses a strange character that RedCloth/Textile doesn’t understand.
October 23rd, 2009
My current ror project at Berkman is a new Herdict website being released later this week. It is an extension of the BadwareBusters.org project that has been released to GitHub as LittleVoice. My work in LittleVoice primarily involves restful authentication and scoring model methods, but for this new project I’ve been tasked with adding Twitter, OpenId and anonymous authentication.
In retrospect, I might have been better off implementing Authlogic. But since I had already implemented Restful Authentication with all the bells and whistles in LittleVoice, it seemed to make more sense to simply throw in the open_id_authentication and the twitter_authentication plugins and be done with it. Mistake #1
Redirecting to OpenID and Twitter via the form_remote_tag form doesn’t work. The app just hangs. I tried various options and incantations of form_remote_tag to no avail. Forunately, I could just bypass this nuisance issue with a simple form_for.
Otherwise, Twitter worked well out of the box except for the fact that the api does not return a user’s email address after authentication for security reasons. This bummed me out because the LittleVoice app has a business rule requiring all users to have a unique email address. But problem easily solved by requesting email up front as part of my authentication form. No big deal.
Open ID however has a number of quirks that caused me great pain to work out. And in researching these quirks and issues, I had a hard time finding information short of digging into the OpenID api. And who wants to dig into the api? I don’t have time for that! Mistake #2
Next, I found that my login and signup pages were working fine. But authenticating via OpenID on the fly didn’t. LittleVoice allows users to write a post before logging in, and rather than “submit” be able to login in-line. But logging in via OpenID in the middle of my controller just wouldn’t work right.
What the heck was different between these two methods? This is a great example of why it pays to be DRY. But I digress…
In my sessions controller, I invoked OpenID in a form_tag that redirected to the open_id_logon named route. In my other controller, I invoked OpenID by directly calling the authenticate_with_open_id plugin method. Mistake #3
What wasn’t apparent right away is the fact that the authenticate_with_open_id method needs to be called twice. This method redirects to the OpenID url entered by the user. When the OpenID website is ready to return back to your site, you need to make sure that it calls authenticate_with_open_id again so you can grab the result, identity_url and registration object. By calling the method directly in another controller, my return_to url was my controller method rather than the authenticate_with_open_id method. Simply using the named route at all times, solves this issue. But there’s another way.
There is a little known option for the authenticate_with_open_id. You can set the return_to directly like this:
authenticate_with_open_id(openid_url, :return_to => open_id_logon_url) do |result, identity_url|
...
end
But here’s the really painful part. When you try to use the return_to option you get an error message.
OpenID::Server::UntrustedReturnURL
There is a bug in the open_id_redirect_url method of the OpenIdAuthentication module. The current module code looks like this:
def open_id_redirect_url(open_id_request, return_to = nil, method = nil)
...
open_id_request.redirect_url(requested_url, return_to || requested_url)
end
But it needs to be this:
def open_id_redirect_url(open_id_request, return_to = nil, method = nil)
...
open_id_request.redirect_url(return_to || requested_url, return_to || requested_url)
end
*sigh* I can’t wait to close this ticket.
October 21st, 2009
The Ruby on Rails Workshop for Women event that I spent the last two months organizing went flawlessly! Teachers, Sarah Allen compared the event to the Stone Soup fable and Andy Gregorowicz wrote about the workshop from his perspective. The tweet stream was also fabulously positive.
Here’s some of the feedback received:
So cool that folks travelled to Boston from as far away as PA to attend Ruby on Rails Workshop for Women!!
…it makes a huge difference to be able to ask someone stupid ?s.
I like being able to say that I deployed my first Rails application today before lunchtime
like running a half marathon.
Totally impressed with the amount of Ruby and Rails info the students absorbed.
I want to thank the teaching assistants at #rorw4w. You were never judgmental and always patient.
You know what’s a lot of fun? TAing at #rorw4w.
It was indeed entirely attitude-free, as promised – unique in my experience! I appreciated that so much, and I had a lot of fun, as well.
I was truly impressed with the number of ror rockstars in the Boston community who were willing to give up their weekend to help some newbies. I have a new mantra: MINSBRAN – Matz Is Nice So Boston Rubyists Are Nice. I will never fear going to another boston.rb or hackfest event again.
Sarah mentions all the awesome volunteers by name in her blog post, so here I’ll thank Berkman folk for their support in making this happen: Urs Gasser, Colin Maclay, Amar Ashar, Carey Andersen, Seth Young, Catherine Bracy, Jason Callina, Brandon Palmen, Dharmishta Rood, Daniel Jones, and the Gender and Technology committee. I should also thank our CRCS supporters, Margo Seltzer and Salil Vadhan.
Tonight we gather again at the Open Source Code Crunch event at the Berkman Center. This is going to be a monthly event promoting mixed gender collaboration. Programmers will meet monthly and use their skills towards open source projects in a welcoming, collaborative, attitude-free, newbie-safe environment. Looking forward to it.
October 21st, 2009
Getting “????” in your database rather than your Arabic or Chinese character sets? Try this:
ALTER TABLE contents CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
October 19th, 2009
Previous Posts