Archive for the ‘Rails’ Category

Railsbridge BugMash on now

Saturday, January 16th, 2010

Railsbridge BugMash

The call went out the other day for participants in the Rails 3.0pre BugMash on Jan 16th – 17th. Included were some imple instructions for cloning Rails 3 and creating a new Rails app from it. So I pulled it down and gave Rails 3 a test spin.

Project

I decided to start a project for making a sort of universal friends list. One that encorporates XFN tags and OpenID. The data could then be used as a resource for other sites to seed a friends list. In order to claim an OpenID as your own you need to authenticate it.

Bundler

The first thing I noticed was the existance of the Gemfile in the rails root and how empty config/environment.rb is. I’d read on about the new bundler and thought it was going to be painful, but I was wrong, it was actually a joy. It even removed gem after I commented out the requirement statement in the Gemfile.

Plugins

I installed Authlogic as a plugin, after failing to get it working as a gem. I’ve had the same issue with Rails 2.3 so I don’t think this is an issue with Rails 3. The OpenId Authentication plugin worked fine as well.

Routes

The new routing module took a little getting used to, but after a few mistakes I got it working correctly. In the end config/routes.rb looks much cleaner, and still contains lots of commented samples. Also the legacy routing is commented by default, which is nice since that’s something I do anyway.

Premade .gitignore

Speaking of nice defaults, creating the rails app made .gitignore file for me with tmp / db / log already in it. Very Nice!

View validation

One thing that came as a bit of a surprise to me that the views are validated against the models. If you reference a field on the model in the view that does not exist, Rails will now error and tell you that it’s not there. So you need to rake db:migrate to have it recognize the fields on a model.

Conclusion

I have really liked what I see of Rails 3 thus far. Thanks to all those that have worked on it. I think that I will start using this version by default.

Chores: A test driven website – Part 10

Monday, June 8th, 2009

Where was I?

By the time I finished up post nine and started working on post 10, I was completely lost. It was entirely my fault and it happens to the best of us, and me too. Coming back to the Chores app after time had passed, and looking around, I thought two things:

  • How did this get to be such a mess?
  • Where was I?

A number of factors had come into play that led to this point. The first of which was lack of discipline. As came across features I thought were interesting or necessary, I abandoned the test driven mantra of red, green, refactor. This ruined my focus, splintered my development efforts and generated a fair share of untested code.

Another issue I ran into is that over the time between my first posts and now, a number of new versions of libraries were released and Rails itself moved from version 2.2.2 to 2.3.2. Subtle variations in the APIs for these gems, generated a few errors and left me scratching my head more than once. I would like to say that change is good, it’s great that the Ruby community is so active in improving it’s products. However, I needed to be more proactive in managing the changes.

Time is another factor that worked it’s entropy in Chores. You hear people talking about the poor developer left to maintain sloppy code months or years after it’s release, and that they may be you. Well it was me, and I was feeling the hurt.

Not all is lost

Thus far, I’ve painted a pretty dire picture of the state of the code. I’ve probably left you thinking, “but this is Chores: A test driven website! How could this happen?” Well it does happen, but you are right. On the bright side, it is a test driven website. If it wasn’t I would probably be tempted to toss it out and start over. But, because it’s test driven and in source control, I’m fairly confident that I can get the codebase up to date and functional in a reasonable amount of time. This in addition to a few practices will have me back in business in no time

While these are good practices to follow in the future, what can I do to get this codebase back under control?

  • Git back to known state – Since I’ve done some unknown piecemeal development on chores I will use my post_9 tag.
  • Make sure existing tests pass – This is where our tests are worth their weight. We know what code is working, and what that code does.
  • Evaluate holes – Find the weak points in the tests and make a plan to fix them.

Git Happy

Note: You can find post_9 here, though if you happen to be following along, you would want to merge it into your own source.

My first step is to rewind to the tag I made for my last post. Right away I realise that I now have two source repositories, one on my shared host and one on github. One is really enough so part of the process will be to move to only github.

rubyyot@atlas:~/working/chores$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#
nothing to commit (working directory clean)
rubyyot@atlas:~/working/chores$ git remote
github
origin
rubyyot@atlas:~/working/chores$ git remote rm origin
rubyyot@atlas:~/working/chores$ git remote
github
rubyyot@atlas:~/working/chores$ git remote add origin   git://github.com/rubyyot/chores.git

Above, I listed my remote repositories. I see that I have two, so I remove one and add another. A mistake I made here is that I set my remote to my public clone url. I should have, since I will be pushing to it, used my clone url, git@github.com:rubyyot/chores.git.

rubyyot@atlas:~/working/chores$ git status
rubyyot@atlas:~/working/chores$ git reset --hard post_9
HEAD is now at b0eecae added auto formatting to Identity.find_or_create_if_valid

Here I have done a hard (destructive) reset back to the tag post_9. I could have avoided doing such a dangerous procedure if I had been working on a branch.

rubyyot@atlas:~/working/chores$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       db/production.sqlite3
nothing added to commit but untracked files present (use "git add" to track)
rubyyot@atlas:~/working/chores$ vi .gitignore      #added db/*.sqlite3 to .gitignore
rubyyot@atlas:~/working/chores$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   .gitignore
#
no changes added to commit (use "git add" and/or "git commit -a")

Here I noticed that git sees my “production” sqlite3 database is not in sync with the index. I really don’t want or need to have a pseudo-production sqlite3 database in source control so I updated my .gitignore file to ignore it.

rubyyot@atlas:~/working/chores$ rake test
(in /home/rubyyot/working/chores)
/usr/bin/ruby1.8 -I"/home/rubyyot/working/chores/lib" -I"/home/rubyyot/working/chores/test" "/home/rubyyot/.gem/ruby/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader.rb" "test/unit/identity_test.rb" "test/unit/chore_test.rb" "test/unit/child_test.rb"
Loaded suite /home/rubyyot/.gem/ruby/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader
Started
.............................
Finished in 0.347031 seconds.
 
29 tests, 43 assertions, 0 failures, 0 errors
/usr/bin/ruby1.8 -I"/home/rubyyot/working/chores/lib" -I"/home/rubyyot/working/chores/test" "/home/rubyyot/.gem/ruby/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader.rb" "test/functional/children_controller_test.rb" "test/functional/chores_controller_test.rb" "test/functional/home_controller_test.rb"
Loaded suite /home/rubyyot/.gem/ruby/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader
Started
..........
Finished in 0.201701 seconds.
 
10 tests, 17 assertions, 0 failures, 0 errors
/usr/bin/ruby1.8 -I"/home/rubyyot/working/chores/lib" -I"/home/rubyyot/working/chores/test" "/home/rubyyot/.gem/ruby/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader.rb"
rubyyot@atlas:~/working/chores$ rake features
(in /home/rubyyot/working/chores)
/usr/bin/ruby1.8 -I "/usr/lib/ruby/gems/1.8/gems/cucumber-0.3.11/lib:lib" "/usr/lib/ruby/gems/1.8/gems/cucumber-0.3.11/bin/cucumber" --format pretty features/define_children.feature features/define_chores.feature
Story:  Define child
As a parent
I want to define my child(ren)
So that I can assign them chores
 
  Scenario: Defining a child                                        # features/define_children.feature:6
    Given I am logged in as "test.example.com"                      # features/step_definitions/chores_steps.rb:9
    And I am on the homepage                                        # features/step_definitions/webrat_steps.rb:6
    When I follow "Add Child"                                       # features/step_definitions/webrat_steps.rb:18
    And I fill in "child[nickname]" with "Bobby"                    # features/step_definitions/webrat_steps.rb:22
    And I fill in "child[open_identifier]" with "bobby.example.com" # features/step_definitions/webrat_steps.rb:22
    And I press "Add"                                               # features/step_definitions/webrat_steps.rb:14
    Then I should see "Child added."                                # features/step_definitions/webrat_steps.rb:93
    And I should see "Bobby"                                        # features/step_definitions/webrat_steps.rb:93
 
  Scenario: Defining a second child                                 # features/define_children.feature:16
    Given I am logged in as "test.example.com"                      # features/step_definitions/chores_steps.rb:9
    And I am on the homepage                                        # features/step_definitions/webrat_steps.rb:6
    When I follow "Add Child"                                       # features/step_definitions/webrat_steps.rb:18
    And I fill in "child[nickname]" with "Bobby"                    # features/step_definitions/webrat_steps.rb:22
    And I fill in "child[open_identifier]" with "bobby.example.com" # features/step_definitions/webrat_steps.rb:22
    And I press "Add"                                               # features/step_definitions/webrat_steps.rb:14
    And I follow "Add another Child"                                # features/step_definitions/webrat_steps.rb:18
    And I fill in "child[nickname]" with "Suzy"                     # features/step_definitions/webrat_steps.rb:22
    And I fill in "child[open_identifier]" with "suzy.example.com"  # features/step_definitions/webrat_steps.rb:22
    And I press "Add"                                               # features/step_definitions/webrat_steps.rb:14
    Then I should see "Child added."                                # features/step_definitions/webrat_steps.rb:93
    And I should see "Bobby"                                        # features/step_definitions/webrat_steps.rb:93
    And I should see "Suzy"                                         # features/step_definitions/webrat_steps.rb:93
 
Story:  Define chores
As a parent
I want to define chores
So that I can assign them to my children
 
  Scenario: Creating a chore                      # features/define_chores.feature:6
    Given I am logged in as "test.example.com"    # features/step_definitions/chores_steps.rb:9
    And I am on the homepage                      # features/step_definitions/webrat_steps.rb:6
    When I follow "Add Chore"                     # features/step_definitions/webrat_steps.rb:18
    And I fill in the chore with "My first chore" # features/step_definitions/chores_steps.rb:5
    And I press "Add"                             # features/step_definitions/webrat_steps.rb:14
    Then I should see "Chore added."              # features/step_definitions/webrat_steps.rb:93
    And I should see "My first chore"             # features/step_definitions/webrat_steps.rb:93
 
3 scenarios (3 passed)
28 steps (28 passed)
0m0.266s

Now I’ve run my tests and features and am pleasantly surprised to see that they are all passing. I didn’t expect this, but it’s a pleasant surprise. Still there could be some hidden surprises lurking in the shadows.

rubyyot@atlas:~/working/chores$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   .gitignore
#       modified:   db/test.sqlite3
#
no changes added to commit (use "git add" and/or "git commit -a")
rubyyot@atlas:~/working/chores$ git rm -f db/test.sqlite3
rm 'db/test.sqlite3'
rubyyot@atlas:~/working/chores$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    db/test.sqlite3
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   .gitignore
#
rubyyot@atlas:~/working/chores$ git add .gitignore
rubyyot@atlas:~/working/chores$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   .gitignore
#       deleted:    db/test.sqlite3
#

After running my tests my sqlite3 test db shows as modified. Since my modification to .gitignore should be excluding all sqlite3 databases, it must already be in the git index. So I removed it and added the updated .gitignore to the index. I’m all ready for a commit.

rubyyot@atlas:~/working/chores$ git commit -m "resetting to post_9"
Created commit 79d6f46: resetting to post_9
 2 files changed, 1 insertions(+), 0 deletions(-)
 delete mode 100644 db/test.sqlite3
rubyyot@atlas:~/working/chores$ git push github
git push github
Enter passphrase for key '/home/rubyyot/.ssh/id_rsa':
To git@github.com:rubyyot/chores.git
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to 'git@github.com:rubyyot/chores.git'

I make my commit and try to push to github. The attempt is rejected. Looks like I need to merge into it.

rubyyot@atlas:~/working/chores$ git pull github master
Enter passphrase for key '/home/rubyyot/.ssh/id_rsa':
From git@github.com:rubyyot/chores
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
 README |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
rubyyot@atlas:~/working/chores$ git merge ac60baa287c7ee73851d1b9895c7e6c826abddac
Already up-to-date.

I pull from github which appears to auto-merge. My manual attempt to merge tells me that I’m up to date.

rubyyot@atlas:~/working/chores$ vi README   #made a small change to README
rubyyot@atlas:~/working/chores$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   README
#
no changes added to commit (use "git add" and/or "git commit -a")
rubyyot@atlas:~/working/chores$ git add .
rubyyot@atlas:~/working/chores$ git commit -m "small change to try commit to github"
Created commit 9991b45: small change to try commit to github
 1 files changed, 1 insertions(+), 1 deletions(-)
rubyyot@atlas:~/working/chores$ git push github
Enter passphrase for key '/home/rubyyot/.ssh/id_rsa':
Counting objects: 15, done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 955 bytes, done.
Total 9 (delta 5), reused 0 (delta 0)
To git@github.com:rubyyot/chores.git
   ac60baa..9991b45  master -> master
rubyyot@atlas:~/working/chores$ git status
# On branch master
nothing to commit (working directory clean)

I made a small change and attempted to commit to github. It worked, yay!

rubyyot@atlas:~/working/chores$ vi README  #another small change to README
rubyyot@atlas:~/working/chores$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   README
#
no changes added to commit (use "git add" and/or "git commit -a")
rubyyot@atlas:~/working/chores$ git add .
rubyyot@atlas:~/working/chores$ git commit -m "another small commit to test new remote to github"
Created commit a5b8a72: another small commit to test new remote to github
 1 files changed, 1 insertions(+), 1 deletions(-)
rubyyot@atlas:~/working/chores$ git push origin
fatal: protocol error: expected sha/ref, got '
*********'
 
You can't push to git://github.com/user/repo.git
Use git@github.com:user/repo.git
 
*********'

You will recall that earlier I removed my remote to my shared host and replaced it with github. Here I tried to push another small commit to it and it failed. I used my public clone url rather than my private ssh protected one, as I mentioned earlier. Time to fix that.

rubyyot@atlas:~/working/chores$ git remote rm origin
rubyyot@atlas:~/working/chores$ git remote add origin git@github.com:rubyyot/chores.git
rubyyot@atlas:~/working/chores$ git status
# On branch master
nothing to commit (working directory clean)
rubyyot@atlas:~/working/chores$ git push origin
Enter passphrase for key '/home/rubyyot/.ssh/id_rsa':
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 330 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@github.com:rubyyot/chores.git
   9991b45..a5b8a72  master -> master
rubyyot@atlas:~/working/chores$ git gc
Counting objects: 352, done.
Compressing objects: 100% (244/244), done.
Writing objects: 100% (352/352), done.
Total 352 (delta 105), reused 275 (delta 71)
rubyyot@atlas:~/working/chores$

All fixed up and I’m back in sync. I am still new to git and am only familiar with the handful of commands I use on a daily basis. There was probably a better or easier way to do what I did. If so, feel free to comment. If you are new to git too here are a couple resources.

How do I?

Below I included a small experiment I did where I cloned the chores source to a new local repository, checked out the post_9 tag to a new branch, modified origin to point to another repository, and checked the branch into master. Hopefully it will give you something to work with if you want to do anything similar. Alternatively, you could fork my github source into your own github repo and have fun.

 
rubyyot@atlas:~/working$ git clone   git://github.com/rubyyot/chores.git chores-github
Initialized empty Git repository in /home/rubyyot/working/chores-github/.git/
remote: Counting objects: 352, done.
remote: Compressing objects: 100% (227/227), done.
remote: Total 352 (delta 105), reused 320 (delta 88)
Receiving objects: 100% (352/352), 139.50 KiB, done.
Resolving deltas: 100% (105/105), done.
rubyyot@atlas:~/working$ cd chores-github/
rubyyot@atlas:~/working/chores-github$ git branch post_9_branch post_9
rubyyot@atlas:~/working/chores-github$ git checkout post_9_branch
Switched to branch "post_9_branch"
rubyyot@atlas:~/working/chores-github$
rubyyot@atlas:~/working/chores-github$ git remote rm origin
rubyyot@atlas:~/working/chores-github$ git remote add origin ssh://USERNAME@example.com/path/to/repo
rubyyot@atlas:~/working/chores-github$ git pull origin master
USERNAME@example.coms password:
From ssh://USERNAME@example.com/path/to/repo
 * branch            master     -> FETCH_HEAD
Already up-to-date.
rubyyot@atlas:~/working/chores-github$ git status
# On branch post_9_branch
nothing to commit (working directory clean)
rubyyot@atlas:~/working/chores-github$ git checkout master
Switched to branch "master"
rubyyot@atlas:~/working/chores-github$ git merge post_9_branch
Already up-to-date.
rubyyot@atlas:~/working/chores-github$ git status
# On branch master
nothing to commit (working directory clean)
rubyyot@atlas:~/working/chores-github$

Conclusion

In all it seems that things were not as bad as I had originally thought. Still it reinforces the need for discipline in doing TDD and coding in general. In the next post I will look over the health of the app and (hopefully) take appropriate steps to correct any problems.

Plans for Rubyyot.com and the Summer of Learning Challenge

Sunday, May 24th, 2009

Plans for the future

In the past week I started playing with Rack and put together a little Rack application that generates content from my blog’s rss feed about the site.   As I stated in that post I really didn’t like having the “parked” page up there, and I also wanted to try out Rack, which has gotten so much press of late.   I was very impressed with Rack, and how easy it was to put together a simple web application with it.

Until that point, I had used Rails, used non-Ruby web frameworks and used Ruby for other non web-based projects, but I had not created a Ruby based web application with anything other than Rails.  It was an enlightening experience.   It got me thinking about Rails, what it provides out of the box:

  • ActiveRecord for persistent, vendor agnostic storage and model building goodness
  • Built in support for AJAX and partial views.
  • Controllers to coordinate between them
  • Simple support for REST web services
  • ActionMailer for email handling
  • Flexible url routing
  • An active and growing community that provides a large number of useful and often cutting edge add-ons and guides.

Additionally, many of these features are modular and can be swapped out or replaced with other modules to suit your tastes.  That is quite an impressive and list, and by no means comprehensive.  With Rails 3 on the horizon, there are even more great things on the way.   It’s no wonder, that I fell in love with Rails.

However, I realized that a number of things I use to develop Rails applications are not truly Rails features, but only supported by Rails.  This by no means a slight against Rails, but it is something great about Ruby and the Ruby community.   Here are some features and libraries that I make use of:

  • erb – Templating engine available in ruby itself.
  • rake – An essential make utility for automating reoccurring tasks.
  • Test::Unit – Unit testing framework, baked into Ruby.
  • Cucumber, RSpec and friends – There are group of great libraries available for testing Rails and other Ruby apps.
  • git and github – Fast, distributed source control and repository hosting.
  • Rack – The new hotness in standardized web server interfaces.
  • Capistrano – Automated deployment.
  • Phusion Passenger – Once called mod_rails, it is actually more than that, as it supports all Rack based applications.
  • Dedication to best practices and self-improvement – While not technically a feature, it is ingrained in the Ruby and Rails communities and it’s contagious.

Chances are, if you have done any Rails development you have encountered, used and enjoyed some or all of these.   My point here is not to say that Rails is only great because Ruby is great.  Rather that while Rails is awesome, there is a strong and viable codebase for building applications, web-based or otherwise, in the greater Ruby community.  The relationship between Rails and Ruby and the popularity of Rails as a framework have made both better and stronger tools.

Building Cohesive Applications

This relationship would not be as be strong, if Rails was built as  a monolith.  Instead, Rails is designed in to be modular.  The framework itself is broken up into no less than 6 gems:

rubyyot@atlas:~$ jruby -S gem install rails
Successfully installed activesupport-2.3.2
Successfully installed activerecord-2.3.2
Successfully installed actionpack-2.3.2
Successfully installed actionmailer-2.3.2
Successfully installed activeresource-2.3.2
Successfully installed rails-2.3.2
6 gems installed

Within that, as we saw, the standard way of building rails uses a number of other tools and libraries.  These parts are generally built to be highly cohesive rather than highly coupled.  This property of good design is probably best described by Uncle Bob’s SOLID principles.  These are:

While these principles were written with less dynamic languages than Ruby in mind, such as Java and C#.  Understanding them and applying them  in any language will help you to write clean code.  Code with minimal dependencies that strangle it’s agility and make make maintenance a nightmare.  These SOLID principles also make code more modular and enable the long promised re-usability that all object-oriented programmers learn about in OO101.

Ruby Web Frameworks

One of the trends in the Ruby and Rails communities in the past year or two, has been the building of other web frameworks.  These were often termed micro-frameworks because they offered the ability to build ruby based web applications without having to run a full Rails stack.  The most notable of these are Merb and Sinatra.  While these began to splinter ruby based web development, my mantra for this year is:

Change is good.

These project went and approached the building of web applications in Ruby in different ways.  In doing so they opened up the doors to creativity and brought some improvements and  fresh ideas to the table.  The benefits of this are even now coming clear with the announcement that Merb will be merged into Rails 3.  This promises to bring more speed and modularity to the Rails framework.

Pragmatic Thinking and Learning

I recently completed the book Pragmatic Thinking and Learning.  It was an excellent book that centers around bringing our innate creativity (R-mode) to the party to achieve goals and solve problems.  While it’s a book targeted to technical people it is not written for one specific language or even just for programmers.  It takes a number of interesting studies and theories, most notable for me is Betty Edwards’ Drawing on the Right Side of the Brain and brings them together into a series of tips and hueristics for improving your thought processes.  It’s a very interesting book and I would recommend it to anyone interested in the topic.

Rails and Lettuce

In of the book, Andy Hunt is discussing personal wikis and shows a page from his WikiNotes (figure 8.3 ).  It shows a series of thoughts as follows:

  • U.S. per capita consumption of Romaine lettuce increased 162%
  • Lettuce consumption at a record High
  • Convenience of pre-washed, bagged lettuce outweighs cost.
  • MORAL: it’s got to be easy

I thought that this was a particularly interesting observation and in a way is a metaphor for Rails.

The sales of Romaine lettuce increased 162% by pre-washing and packaging it in convenient bags.   This packaging is widely popular in spite of the increased cost.  It’s clear that lettuce is a great product.  Everyone knows it’s healthy and it’s the building block of salads and great in sandwiches as well.  Still it’s popularity were greatly improved by giving it an instant, right out of the box, interface.   Similarly Ruby web frameworks are great products.  Rails packages that goodness up in an easy to learn, build and deploy.

Reinventing the Wheel vs. Digging a Rut

Rails is built on top of Rack.  Rack is written in Ruby and specifies the interface between the web server and the Rails framework.  In fact Merb and Sinatra are built on top of Rack as well.  Still, as we saw, Rack is available for building upon directly and of course Ruby and it’s libraries are available as well.  So why not build a Ruby web application without any framework at all?  Well the obvious answer is that you won’t have all of the nice things that these web frameworks provide.  Not to mention many of these pieces have now been used and tested by production applications for years now.  It may be a better question to ask:

Why wouldn’t I want to use one of the pre-built frameworks?

The answer to that question is what has been forming in my mind since I tried out Rack for the first time.

  • Frameworks remove options - Even the best framework on the planet is going to take away options.  Lean Methodologies include the idea of delaying decisions as long as possible allowing for greater flexability later on.  Many times we want to use a framework, because it leave a clear path to a production ready application.  This comes at the cost of options and I don’t want these removed.
  • Learning Opportunity -  Often the best way to learn something is to do it.  I’m not an expert with Rails, I still have more to learn, but I also want to learn more about writing and designing applications for the web in general.  Therefore it makes sense to bypass the framework and hook directly into Rack.
  • Many libraries are still available – As we saw before, many of the tools used in the creation of Rails applications are still available for use.
  • Completely Custom – I used to love watching shows like American Chopper and Monster Garage even though I know next to nothing about building a car or a bike.  The thrill was in watching highly skilled people create baddass and functional custom vehicles.  This is a similar though admittedly geekier endeavor.
  • Beginner’s Mind – This zen concept urges you to remove look at things without filtering them through your pre-conceived ideas and biases to gain greater understanding.  Approaching this project without relying on a pre-conceived framework should lead to similar results.

All of these are great reasons for me to try making my own path and not generating yet another Rails application.  That’s not to say that Rails isn’t great, or I’m better than Rails.  It’s just not the right fit, right now for my project.  In the end I’d rather re-invent the wheel than dig myself into a rut.

Vaporware and Playing to Learn

One thing that I do when working with my personal projects is that I will have an idea and start to run with it.  After a while of working with the idea, more ideas will come and my attention will turn away from my original project and move on to other things.  Some examples of this are:

  • Resolvr – A dependency injection mechanism for Ruby.
  • Gippr – A library for parsing the GIFT file format.
  • RSimpy – A wrapper for the API to Simpy.com

Will I ever get back to these and complete them?  Maybe.  Much of what I do in my personal projects is playing to learn.  Like when I was a kid and I wanted to build a giant working  robot or transformer out of legos.  Did I ever finish these projects?  No, but they were fun and I learned a number of interesting things along the way.  This is one of the great things about personal projects.  There is no one expecting you to deliver a product, so you are free to persue whatever interests you.  Even if that means that you abandon the project altogether and move on to another.

There is a character on the show Jimmy Neutron, that my kids watch,  called Professor Calamitous. He is a brilliant scientist bent on doing evil.  However, he is continually undone by his character flaw, that is that he can never finish anything.  I have often teased my wife, because she will get very excited about a project and work on it.  She won’t just do a little, but she really digs into it and attacks the subject.  After some time she will, almost overnight, lose interest and move on to something else.  I’ll tell her that her inner Professor Calamitous has struck.  In actuality, I’m just as guilty of it as she is if not more so (sorry for teasing you).

When viewed from a goals oriented perspective this is a horrible waste of time.  Why put all the energy into something that will be left to rot, unfinished?  From another perspective, that of a learning tool, it is a great accomplishment and a goal achieved.  The time wasn’t wasted because in doing something with the project, rather than just reading (or writing, speaking, etc.) about the topic, you develop hands on experience and engage much more of your mind (R-mode) than you would have otherwise.

Personal Wiki

Now I feel it’s my duty to bring all of these random thoughts together and issue a challenge to any out there who might be reading this.   The book, Pragmatic Thinking and Learning, recommends the use of a personal wiki to gather together your thoughts and organize them into an exocortex.  I looked through all of the available wiki software out there and none of them really felt right.  That was a kind of personal decision and it’s nothing against any of the software available.  They either didn’t provide the features I wanted, or I didn’t like the look and feel.

Requirements

  • Good feel – If I’m going to do this, I will be spending a significant amount of time with it.  Any tool I will be spending that much time with, better not have annoyances, no matter how small.
  • High Availability – While it may not need to be as available as a ubiquitous collection tool, it should be available to me at work and at home, at the very least.
  • Wiki Formatting - Whether via wiki markup, RedCloth or some other means it should be easy to build and grow as a wiki.  That’s kinda the point of a Personal Wiki
  • Single user editing -  It may seem odd to have a wiki that is editable by a single person, but if it’s just for me, then it only makes sense.
  • Public display – Some of the things I put in it would make sense to be available to others who are interested.
  • Private display – Some of the things in it may be private and kept that way.
  • Easy backup and recovery – If I’m going to go to all the trouble of writing and gardening a wiki, it should be safe and recoverable in the event of corruption, disaster or my own foolishness.
  • Customizable – I may want to have dynamic content on some pages.  Perhaps it should pull in my bookmarks on a topic and display them with other content.

That is quite a hefty list and I’m sure that there will be other things along the way that I’ll want to put in there.  It’s no wonder I couldn’t find what I wanted out of the box.

I’ve decided to build this directly on Rack, using git, github and other useful tools I would use in building a Rails app.  I’m not sure how this will go at this point, but it’s an exciting project and I hope to have something to show for it by the end of summer.

The Summer of Learning Challenge

We all have these kinds of ideas floating around in our heads, and the more creative ideas we listen to and act upon, the more will come to us.   I think that right now is a great time, as it is the beginning of Summer 2009 to act on one, or more, of these.  The summer provides a set time frame that is long enough to make something great, but short enough that we won’t put it off forever.  I think that it would be great to see others (you) find a project they are interested in a make a commitment to work on it this summer.  If you are a developer/programmer, make your project open-source.  It could be a great benefit to others.  It will defiantly be a great experience for you.

A few years ago I took part in and completed NaNoWriMo.  My book wasn’t finished at the end, but I wrote 50,000 words of it in one month.  That was a great accomplishment for me, something that I didn’t think I could do when I started.  By setting the goal and working toward it I was able to complete it.  It felt great.  What could you do if you set your mind to it?

What Will You Do?

If you are interested in taking the challenge or just have an idea or a comment about mine.  Please leave feedback or blog about it yourself an leave a postback.  It’s going to be a great summer.  Enjoy!

Chores: a test driven website – Part 9 (the lost episode)

Sunday, May 10th, 2009

This is part of the Chores series of posts.

Well it’s been a while since National Testing month and there has been a lot going on both in my world and in the Ruby Community. I’ve been doing a good deal of reading, deployed a Monorail website and been generally busy. Unfortunately, I haven’t focused on building Chores, my test driven chore delivering website.

Today I’ve changed all that by dusting off chores and updating my gems. The first thing that I noticed was that I needed to add some configuration for Webrat for it to function within Cucumber. I needed to add the following to the env.rb file:

Webrat.configure do |config|
  config.mode = :rails
end

Next I see that I’m getting an error on the new action for Children because the @child variable is nil. Let’s write a functional test to fix that.

Here is my first pass at the functional test and controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'test_helper'
class ChildrenCanCrud < ActionController::TestCase
  tests ChildrenController
 
  specify "test that it should respond to index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:children)
  end
 
  specify "that it should pass child on new" do
    get :new
    assert_response :success
    assert_not_nil assigns(:child)
  end
 
  specify "that it should create child" do
    assert_difference 'Child.count' do
      post :create, :child => { :parent => Identity.make, :identity => Identity.make }
    end
 
    assert_redirected_to children_url
  end
 
end

./app/models/child.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Child < ActiveRecord::Base
  attr_accessor :open_identifier
  before_create :set_identity
 
  belongs_to :identity,             :foreign_key => :child_id,
                                    :class_name => 'Identity'
 
  belongs_to :parent,               :foreign_key => :parent_id,
                                    :class_name => 'Identity'
 
  validates_presence_of :open_identifier,  :message => 'can not be blank.',
                                           :on => :create
  validates_presence_of :identity,         :message => 'can not be blank.',
                                           :on => :update
  validates_presence_of :parent,    :message => 'can not be blank.'
 
  def set_identity
    self.identity = Identity.find_or_make_if_valid @open_identifier
  end
end

./app/models/identity.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Identity < ActiveRecord::Base
  include OpenIdAuthentication
 
  validates_presence_of :identifier,        :message => "can not be blank."
 
  validates_uniqueness_of :identifier,      :message => "is already in use, please use another."
 
  validates_length_of :identifier,          :maximum => 100,
                                            :allow_nil => true,
                                            :message => "is too long."
 
  validate_on_create :valid_id
 
  has_many :parents,             :foreign_key => :parent_id,
                                 :class_name => 'Child'
  has_many :children,            :foreign_key => :child_id,
                                 :class_name => 'Child'
 
  def open_id
    return nil if self.identifier.nil?
    self.identifier[7..-2]
  end
 
  def identifier= identifier
    raise "Cannot update identifier." unless new_record?
    set_identifier identifier
  end
 
  def open_id= open_id
    raise "Cannot update open_id." unless new_record?
    set_identifier open_id
  end
 
  def valid_id
    errors.add(:identifier, "is not a valid Open ID.") if @invalid_id
  end
 
  def self.find_by_open_id open_id
    id = OpenIdAuthentication::normalize_identifier(open_id)
    find_by_identifier open_id
  end
 
  def self.find_or_make_if_valid open_id
    begin
      id = self.find_by_open_id Identity.normalize_identifier(open_id)
    rescue OpenIdAuthentication::InvalidOpenId
      return nil
    end
 
    unless id
      id = Identity.new :identifier => open_id
      id.save
    end
    id
  end
 
    def Identity.normalize_identifier id
    begin
      identifier = OpenIdAuthentication::normalize_identifier(id)
    rescue OpenIdAuthentication::InvalidOpenId
      identifier = nil
    end
    identifier
  end
 
  protected
  def normalize_id id
    identifier = Identity.normalize_identifier id
    unless identifier
      @invalid_id = true
      identifier = id
    end
    identifier
  end
 
  def set_identifier id
    write_attribute(:identifier, normalize_id(id))
  end
end

and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class ChildrenController < ApplicationController
  def index
    @children = Child.find(:all)
  end
 
  def new
    @child = Child.new
  end
 
  def create
    @child = Child.new(params[:child])
    if @child.save
      flash[:message] = "Child added."
      redirect_to children_url
    else
      flash[:error] = "Error saving Child."
      render :action =>  "new"
    end
  end
end

update to ./test/unit/child_test.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'test_helper'
 
class ChildShouldBeValidated < ActiveSupport::TestCase
 
  specify "that a parent is required" do
    child = Child.new
    child.open_identifier = valid_open_id
    invalid child
    child.parent = Identity.make
    valid child
  end
 
  specify "that a child is required" do
    child = Child.new :parent => Identity.make
    invalid child
    child.open_identifier = valid_open_id
    valid child
  end
 
  specify "message for a null identity" do
    child = Child.new :parent => Identity.make
    validation_message_for child, :open_identifier, "can not be blank."
  end
 
  specify "message for a null parent" do
    child = Child.new :identity => Identity.make
    validation_message_for child, :parent, "can not be blank."
  end
end
 
class ChildShouldCreateIdentity < ActiveSupport::TestCase
 
  specify "that open_identifier is used to pass the OpenId" do
    child = Child.new :parent => Identity.make
    child.open_identifier = valid_open_id
    assert child.save
  end
end

./test/functional/children_controller_test.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'test_helper'
 
class ChildrenCanCrud < ActionController::TestCase
  tests ChildrenController
 
  def setup
    @identity = Identity.make
  end
 
  specify "test that it should respond to index" do
    get :index, nil, build_session_hash_for(@identity)
    assert_response :success
    assert_not_nil assigns(:children)
  end
 
  specify "that it should pass child on new" do
    get :new, nil, build_session_hash_for(@identity)
    assert_response :success
    assert_not_nil assigns(:child)
  end
 
  specify "that it should create child" do
    assert_difference 'Child.count' do
      post :create, {:child => { :open_identifier => "cheese.example.com" }}, build_session_hash_for(@identity)
    end
 
    assert_redirected_to children_url
  end
 
end

./test/test_helper.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require File.expand_path(File.dirname(__FILE__) + "/blueprints")
require 'test_help'
require 'machinist' #if you installed as gem rather than plugin
require 'faker'
 
class ActiveSupport::TestCase
  self.use_transactional_fixtures = true
  self.use_instantiated_fixtures  = false
 
  def invalid model
    assert !model.valid?
  end
 
  def valid model
    assert model.valid?
  end
 
  def validation_message_for model, column, message
    invalid model
    assert_equal message, model.errors.on(column)
  end
 
  def valid_identifier
    "http://test.example.com/"
  end
 
  def build_session_hash_for identity
    @session_hash = {'identity_id' => identity.id}
  end
 
  def valid_open_id
    "test.example.com"
  end
 
  def invalid_open_id
    "bad_id"
  end
 
end
 
def specify *args, &block
  test(*args, &block)
end

.. interestingly enough I find that my view that I wrote at some point expects a field called nickname

1
2
3
4
5
6
7
8
9
<% form_for(@child) do |f| %>
  <%= f.error_messages %>
  <%= f.label :nickname %>
  <%= f.text_field :nickname %>
  <%= f.label :open_identifier %>
  <%= f.text_field :open_identifier %>
  <%= f.submit "Add" %>
 
<% end %>

So I quickly create and run a migration to add the field.

~/working/chores (chores9)$ ruby script/generate migration add_nickname_to_child nickname:string
      exists  db/migrate
      create  db/migrate/20090402191006_add_nickname_to_child.rb

which can should be migrated with.

rake db:migrate

Here is the design I settled on.

As you can see, I’ve pushed it out to github. I’ve tagged it with “post_9″. I am hoping this will help those that want to follow along and reduce the amount of code that I need to post while writing this up. Maybe I’ll write up a post on git and github.

Update: I apologize this is all getting muddled in my brain as well. Between a change in rails versions and the passage of time. I hope to get this all straightened out in the next post.

Configuring gems for your rails app

Friday, May 8th, 2009

Until I found this (recently), I have been guilty of not configuring my gems for my rails apps.  Apparently these features have been around for a while and can be really helpful when deploying or doing team development with rails and gems.

What it does

  • Requires gems automatically
  • Documents the gems you used
  • Allows you to specify gems by environment.   This is key so you don’t have to require testing gems for a production environment.
  • Provides rake tasks for freezing,  installing and checking the status of the required gems.
  • helps to ensure that functionality you depend on gems for will be working.

Rake Tasks

Here is a listing of gem related rake tasks.

 rake gems                             # List the gems that this rails application depends on
 rake gems:build                       # Build any native extensions for unpacked gems
 rake gems:install                     # Installs all required gems.
 rake gems:refresh_specs               # Regenerate gem specifications in correct format.
 rake gems:unpack                      # Unpacks all required gems into vendor/gems.
 rake gems:unpack:dependencies         # Unpacks all required gems and their dependencies into vendor/gems.
 rake rails:freeze:gems                # Lock this application to the current gems (by unpacking them into vendor/rails)
 rake rails:unfreeze                   # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
  • rake gems:unpack variants are helpful to make sure that you deploy the same gems from your development environment to your production environment, even if it’s on a shared host.
  • rake rails:freeze:gems does the same for the rails gems.
  • A simple rake gems will give you an overview of your gems like this:
rubyyot@atlas:$ rake gems
(in /home/rubyyot/working/examples)
 - [F] ruby-openid
 - [F] paperclip
 - [I] RedCloth
 - [F] sqlite3-ruby
 - [F] ruby-openid >= 2.0.4
I = Installed
F = Frozen
R = Framework (loaded before rails starts)

Example

Here are my configured gems from a project I’m currently working on.

config/enviroment.rb
  config.gem "ruby-openid", :lib => 'openid'
  config.gem "paperclip"
  config.gem "RedCloth", :lib => 'redcloth'
config/environments/development.rb
  config.gem "sqlite3-ruby", :lib => "sqlite3"
config/environments/test.rb
 config.gem "notahat-machinist", :lib > 'machinist', :version => '0.3.1'
 config.gem "faker", :version => '0.3.1'
 config.gem "sqlite3-ruby", :lib => "sqlite3"
config/environments/production.rb
config.gem "mysql", :lib => "mysql"

More

If you are interested, there is a Railscast episode on this topic.