Review: Ruby on Rails E-Commerce

Beginning Ruby on Rails E-Commerce: From Novice to Professional … separates itself from the oncoming barrage of introductory Rails texts by guiding readers through the development of an online bookstore from start-to-finish.

The rest of the article can be read at Dr. Dobb’s.

From Ruby to PHP: not a cakewalk

I’ve read a few opinionated pieces that mention how learning to program in PHP can make it difficult to pick up advanced object-oriented techniques down the road.

Is the reverse also true? Behaviour I’ve come to expect from other high level languages doesn’t always seem to translate to PHP.

Consider this simple (but meaningless) program written in Ruby:

class MyParent
  @classname = 'Parent' # static member variable

  def self.classname # static method
    return @classname
  end
end

class MyChild < MyParent
  @classname = 'Child' # override the static member variable
end

puts MyParent::classname # gives me 'Parent'
puts MyChild::classname # gives me 'Child'

Makes sense, right? Consider the same program, this time written in PHP:

class MyParent {
  static public $classname = 'Parent';

  static function classname() {
    return self::$classname;
  }
}

class MyChild extends MyParent {
  static public $classname = 'Child';
}

echo MyParent::classname(); # gives me 'Parent'
echo MyChild::classname(); # gives me 'Parent'

Which behaviour is “right”? Or is that even a fair evaluation?

As a side note, some folks in #php on freenode came up with this workaround, which seems to defeat the whole purpose of subclassing anyways.

Firefox: the web developer's killer app

Jeff Atwood over at Coding Horrror recently wrote a post entitled Firefox as an IDE, where he describes how a few brilliant plugins have made Firefox his primary web development environment. He focuses on the magnificant Firebug and Web Developer add-ons, which not only make development way easier, but have the additional upside of being free and open source.

Side note: Coding Horror is a great read.

I couldn’t agree more with Jeff. I’ve got every major browser installed on my PC for testing purposes, but when the real work’s to be done, I reach for Firefox. I’ve been using both those add-ons for several months now, and they’ve become completely indispensable to my work. Honestly, the idea of writing JavaScript code without Firebug scares the hell out of me.

It doesn’t end with Jeff’s article though. I’ve been building a reputation around the office of running the fattest Firefox client imaginable, loaded to the max with developer gadgets. If you’re a web developer or designer, and you’ve got RAM to spare, there’s a few more you might be interested in learning about.

Selenium IDE

Selenium IDE is a member of Open QA’s [gasp] Selenium suite of web testing applications. This particular version is essentially a user interface recording tool driven through Firefox. It captures all of your input edits, link navigations, checkbox clicks and so on, allowing you to easily play back your actions, or save them to a format usable by one of the other Selenium apps. It’s also got a thorough set of assertion statements for making sure all’s well.

Now, whether or not your organization uses the Selenium suite for regression testing, Selenium IDE is an incredibly useful tool, particularly in ad-hoc testing situations. For example, if you’re tweaking a web form that has a long list of fields (like a user signup page), why not do yourself a favor and record/playback all the test data you’ve been entering a dozen plus times? Basically, wherever you find yourself repeating a set of redundant actions, there’s a place for Selenium IDE.

ChatZilla

ChatZilla is a pure-JavaScript IRC client that harkens back to the original Mozilla web browser. It has the appearance of an app written in 2001, but it’s still a great tool.

Basically, if you’re a web developer, you ought to have an IRC client handy. There’s a number of (generally) good development communities out there, and they’re great places to turn when Google isn’t turning up the answers you’re looking for.

ChatZilla isn’t as full-featured as mIRC (the undisputed king of IRC connectivity), but it gets the job done for me. One upside of having ChatZilla embedded in your browser though is built-in support for IRC URLs. You can create bookmarks to your favourite networks and channels (like #mephisto on Freenode) for quick and easy access straight from your browser. You spend all your time there anyways, right?

What do you use?

There’s tons of useful web development add-ons downloadable from the Firefox website. Use any that we haven’t covered? Share them below.

Pagination done right

You might have noticed dzone has just undergone a facelift. Looks great, but above all I love their new auto-pagination feature. Basically, once you scroll far enough down their list of developer links, it expands in place using an Ajax request. The end result is a bottomless pit of development links, and it’s so well executed, you might have never noticed it.

My question is, why haven’t I seen more of this?

See also: Endless Pageless: No More Next Page

A look at ActiveRecord association setters

If you’ve been working in Rails for more than a short while, you should be familiar with ActiveRecord class methods, and how they modify your model definitions with helpful magic methods.

For example, the has_many declaration gives you access to a host of accessors and setters that make it easy to manipulate associated child objects:

Class Family < ActiveRecord
  has_many :members
end

class Member < ActiveRecord
  belongs_to :family
end

# Create a new family and save it
family = Member.new(:name => “The Johnsons”)
family.save

# Add a family member through array insertion
family.members << Member.new(:name => “Sam”)

# Add a family member through association creation
family.members.create(:name => “Sally”)

In the example code above, we created our new family members through the “members” collection. By doing so, the family_id attribute/column is initialized for us. Otherwise, we’d have to set it manually like this:

sunny = Member.create(:name => “Sunny”, :family_id => family.id)

Okay, you might not find that impressive. I mean, we’re only saving a few dozen characters of code here, right?

Turns out there’s another convenient way of building associated objects that I feel doesn’t get enough press. It’s the collection=objects method, which takes an array of associated objects like so:

family.members = [ Member.new(:name => “Jean”), Member.new(:name => “Tom”) ]

You’ll find that for each of these objects the family_id attribute was set, just as they would have been above. As for Sam, Sally and Sunny? They’ve been usurped by Jean and Tom, the once-loyal babysitters. That’s right, in addition to creating new database records for Jean and Tom, the old records are deleted too. That’s a lot of good stuff going on for a single line of code.

Editor’s note—I need to come up with better examples.

It’s also worth mentioning this setter method also works when initializing ActiveRecord objects using a hash. The example code below will create a new family, add a couple family members to it, and save all three objects to the database.

Family.create :name => "The Johnsons", :members => 
  [ Member.new(:name => "Sam"), Member.new(:name => "Sally") ]

Okay, so this isn’t revolutionary, but it’s another solid tool to add to your Rails arsenal if you haven’t already.