20110626

Time-buffered WUI update /javascript

When it comes to UI (and it naturally comes to js), there often are routines that you would like to perform on a certain event, but not necessarily on each such event, that is, just enough to keep a view updated at most times.
For instance, there are items being added to a sorted list: you'd like to sort the list whenever a new item arrives, but when a bunch of items arrive at once (over a short period of time), you'd like to postpone sorting to the last item in the bunch (for obvious reasons). And you want to keep things simple and refrain from event queuing, optimizing the sort routine and other complex stuff.
Well, in that case, something I call time buffering may help you. If your event handler looks like this:

function _onNewItem(item) {
_addItem(item);
_sortList();
}

With time buffering it'll look like this:

function _onNewItem(item) {
_addItem(item);
_timeBuffer("sort_list", _sortList, 500, 2000);
}

Not much of a change, eh? And the killer routine?

I'm sure you can fix my style (you're welcome) and add support for removing such list elements -- and associated actions -- cleanly (with something like function _timeBufferNoMore(act)), but you get the idea.

20110624

the boolean virtual attribute's gotcha (a checkbox in a rails form)

I am not sure where to post this, suggestions are welcome.

Whenever you create a virtual boolean attribute in your model, e.g.

attr_writer :some_boolean
def some_boolean; defined?(@some_boolean) ? @some_boolean : true; end  # defaults to true
attr_accessible :some_boolean

And make it a checkbox in the model's input form, e.g. (simple_form, haml)

!= f.input :some_boolean, :as => :boolean

And try to do some reasoning with it, e.g...

after_create { ... if @some_boolean }

You may be surprised as @some_boolean will always resolve to true (actually to 0/1, but both are true in Ruby).

A quick and dirty workaround would be... well... getting your controller dirty quickly:

before_filter :boolean_fix
...
def boolean_fix
  params[:some_model][:some_boolean] = false if params[:some_model] && params[:some_model][:some_boolean] == '0'
end