Skip to main content

Posts

Unit/Functional Testing RubyAMF

One of my current projects is using RubyAMF to communicate with Flash (http://rubyforge.org/projects/rubyamf/). On the whole this is really nice because it allows you to transfer Ruby objects directly to ActionScript ones (as opposed to translating the object into XML, sending the XML and then recreating the object in ActionScript). However, Rails does not provide a built in transport mechanism for AMF, so we cannot run functional testing directly on the data call (as we could for an XML or HTML transport layer). This is a show stopper for a lot of people (Rails w/o Unit testing = a big mess of trouble when something goes wrong). We can though serve both the HTML and the AMF formats depending on the request format. This means that we can test the object instantiation logic and make sure there are no errors in the controllers (though we cannot check the actual format of the data being served). In the controller, instead of rendering AMF alone, do the following respond_to do |format| ...

Writing Web Components in Rails

One of the problems most people run into when writing web components is the fact that you have to tie together 2-3 disparate elements (HTML partial, JavaScript file and/or CSS) into one contextual component. As JavaScript and CSS files are normally placed in the head and the HTML partial occurs somewhere in the body, it can be hard to track down any missing pieces should they arise. In RJS (built in Ruby JavaScript components) the normal procedure is to place the script tag next to the element in the HTML itself. This not only goes against Unobtrusive JavaScript principles, it is also butt ugly if you ever have to view the source code. Thankfully, there is a more elegant solution. Instead of rendering your JavaScript directly in the page, i.e. <%= javascript_include_tag "my_component" %> you wrap it in a content_for(:head) (this assumes you have placed a <%= yield :head %> in your application.html.erb template). i.e. <% content_for(:head) do %> <%= javasc...

Unobtrusive JavaScript and href="#"

Back in the old days of inline JavaScript, people would often attach JavaScript functionality to links either by using: <a href="#" onclick="doSomeJavaScript()">Click Here</a> or by using <a href="javascript:doSomeJavaScript()">Click Here</a> Anyways, there are problems with both. In the first instance, the user will get booted up to the top of the page in some browsers (that's what href="#" basically does). The second instance is better, but it is inline and that is something we want to avoid. In the Unobtrusive JavaScript world we would basically have to use the first instance because the "a" tag will not display as underlined in some browsers without an "href" attribute. Or would we? I am not really sure where the metaphor for using links as actions came from, but as long as it is there and people are used to it we will often get designs from UI designers using them as such. Links are really supp...

Unobtrusive JavaScript and the "lang" attribute

One issue that has come up in using Unobtrusive JavaScript is how do you assign instance specific data to the element. For example, in the old days (pre UJ) you could iterate over a bunch of objects (each with it's own ID) and hard code the ID into the on{Event} attribute i.e. <% for item in items %> <div onclick="someFunction(<%= item.id %>)">Click here to select item</div> <% end %> But in UJ, on{Event} attributes are no longer considered good practice. If only there was a way we could somehow encode the id with the HTML like we could in Flex (Flex supports dynamic attributes on objects). You can assign a dynamic attribute using JavaScript, but putting a script tag into the loop seems very inelegant and against UJ principles. The other way would be to render all the objects in JavaScript first and then re-render them in HTML. This does seem rather wasteful in terms of processing however and full of index dependencies when you want to merge ...

Checkbox arrays in Rails

Rails has got some nice default form options available in the FormHelper , however the check_box still leaves a little to be desired. It's great if you have one or two disparate elements, but what about when you want people to choose from a list of items like the following? Choose your favorite fruits. Apple Orange Banana Code <form> Choose your favorite fruits. <div><input value="apple" id="apple" name="fruit" type="checkbox"><label for="apple">Apple</label></div> <div><input value="orange" id="orange" name="fruit" type="checkbox"><label for="orange">Orange</label></div> <div><input value="banana" id="banana" name="fruit" type="checkbox"><label for="banana">Banana</label></div> </form> In the example above, they all have the name fru...

Turning autocomplete off

Most modern browsers have a handy little feature which will check to see if you have filled in a text box before, and if so it will present you with a history of what you selected. For the most part this is a very useful piece of functionality but for one caveat. The browser does not fire any kind of JavaScript event when this happens. Dynamic form validation based on user input thus becomes a (to use Dave Chappelle's word) "biatch". Fortunately after digging around a little I found that there is a handy little attribute called "autocomplete" which you can set to "off" on the form element. <form method="post" action="/registration/create" autocomplete="off"> This will keep those nasty little history drop downs from messing up your input validation.

A couple cool FaceBook games

I just got turned onto a couple of cool FaceBook games today. Bumper Stars http://apps.facebook.com/bumperstars/ Bumper Stars is a cross between pinball, pool and Pacman. Basically you have a little beaver whom you shoot off to eat fruit and you bounce off all sorts of things. Kind of stupid, but fun and addictive. Who Has The Biggest Brain http://apps.facebook.com/biggestbrain/?pf_ref=sb Who Has The Biggest Brain is one of those rare games which makes you think and makes analytical thought into a competitive sport. There are about 7 or 8 varieties of brain tests and teasers from simple math to figuring out which random objects weigh more than the others, finishing puzzles and counting blocks. The first few times you play are the most fun as you are presented with brand new kinds of tests which make you learn new tasks (after that it gets more competitive but stretches your brain less). In any case I am a scholar!