Design Element
Comment on "WebObjects Could Learn From Rails"
by hitoro — May 31
IMHO Rails is overrated. While it sounds great to hide the data model and to only bother about the controller and the presentation layer, I think it tries to solve the wrong side of the problem. The difficulty is not in defining and using the data model but to manage the flow between controllers and views. It still requires to think how to go from one view to another and to break the controller into pieces.

I have tried a few weeks ago to adapt the Rails' Todo-list tutorial to Seaside and Mewa; while I have written a bit more code on the data model side, mainly accessors and convinient methods, the controller and the views were almost free of code.

The problem I have with the Rails' tutorial is that the Todo model is used to represent both a single item and an array of items. The other problems are that you still have to use SQL statements and HTML templates to get the things done.

What's the deal? Compare this method for retrieving pending items:

def self.find_not_done
find_all("done = 0", "description"<Wink>

... with this one written in Smalltalk:

^self items reject: [ :each | each done ]

TodoList is a subclass of a Collection and contains instances of TodoItem. The object tree is nicely defined and self contained, and the method is, well, nice.

Deleting a Todo item with Rails is even trickier:

def destroy
item = Todo.find(@params["id"])
redirect_to(:action => "list"<Wink>
rescue ActiveRecord::ActiveRecordError
render_text "Couldn't destroy item"

In that "destroy" method, you have to retrieve the id and do a redirection to make the things tick. In Seaside, you don't to worry about such details:

TodoListComponent>>renderItems: items on: html
html form: [ batcher batch do: [ :item | html span: item description. html anchorWithAction: [self delete: item] text: 'Delete'. ].

In short, this method visits the items and renders their descriptions on a stream with a link to delete the corresponding item in a row.

The most interresting line is the call to the anchorWithAction:text: method. It binds the block [self delete: item] to a link with the text 'Delete'. When the visitor will click on that link, Seaside will execute the block and delete the item corresponding to that link.

Yes, that's inline binding of code. No need to worry about passing ids, inserting values in HTML templates or using SQL statements, you work with first class objects.

Sorry for the long post, but I have that thing in mind since a long time ago.
Back to "WebObjects Could Learn From Rails"
Design Element

Copyright © Scott Stevenson 2004-2015