UPDATE: Demo offline permanently
UPDATE: I moved my development site to a workstation here at home. It is a slow old box so be gentle, it is serving a little under 10req/s, but it works.
Over the last few weeks I have been seeing what I can do with Ruby on Rails. My conclusion? In the future I want to use RoR for web applications: PHP just isn’t as fun after you have worked with RoR.
At the Open Source Lab I work on a project written in PHP/MySQL called Maintain (demo), it is a DHCP/DNS management tool that is used by Oregon State, the Open Source Lab, math.ku.edu, and several others. It is a really great package, and one that many people are getting joy out of using. And given its size and complexity and my familiarity with its code, it made a great canidate for me testing out RoR.
Last night I decided to add LiveSearch support into my little pet project that is exploring how I would implement some of Maintains features in RoR. The first thing that needed to be done was add search methods to my models. Browsing around Tobias’ typo source tree I figured out how I wanted to implement searching for one of my domain class and came up with this:
# This search defaults to searching for all strings that match with no
# restriction on beginning or end. Also it will OR together statements in
# the query with a space in them.
def self.search(query)
if query
tokens = query.split.collect {|c| "%#{c.downcase}%"}
r = find_by_sql(["SELECT * from domains WHERE #{ (["LOWER(name) like ?"] * tokens.size).join(” OR “) }”, *tokens])
s = Array.new
r.each {|domain| domain.children.each{|child| r.push child}}
return r.uniq # No need for duplicate entries
end
end
The line with r = find_by_sql( is something that is just amazing to me, and would have required a whole looping structure and some if statements to implement in PHP, but in Ruby I was able to use prepared statements, Array#join, and the * operator on the array to quickly build a restraint on the domains; all in one line.
After implementing the search methods, and looking at the Typo code the rest was a breeze, I created a couple of partials for live search results, and now with two lines of code I can add new objects to the LiveSearch sidebar. Very cool.
If you want to take a look at it in action click here (site down permanently, code available here). The handshake starts with a testguy and ends with a testt. Oh, and this is a limited time offer, the site is running on my laptop.
And speaking of the OSL Scott Kveton has made a great post about OSU’s College of EECS strategy to take on the big research schools and focus less on undergraduate education. Its pretty clear that they should be focusing on open source software and education especially given the current climate around the state but I guess they would rather Go Big than think smart.


One Comment
re: Maintain, that’s hilarious. Way back when, I almost went to work for Scott. Ever since I’ve been thinking about how much fun it would have been to work on Maintain and the other stuff you guys do. And in the past few weeks I’ve been playing with RoR and have actually spent time thinking about writing in in RoR
Oh well, opportunities missed blah blah blah.
One Trackback
[...] arch in Ruby on Rails
Posted in ruby by Rory on the July 7th, 2005
I found this great post on Brandon Philips blog, which showed an example of how to search your da [...]