Monthly Archives: November 2007

Little Things I love about Rails – This Time ActiveRecord

When you are setting a foreign key in an object you can do it using the id directly or through the helper method. In this example comments belong_to users.


# Using an object reference
u = User.get(1234)
c = Comment.new
c.user = u
# Old school
x = Comment.new
x.user_id = 1234

Yeah – that is what I like to see – a framework that respects programmer intelligence and gives us choices!

Making TinyMCE work in Ajax With Prototype and Ruby on Rails

Well – it was not nearly as easy as one would expect to make this work – but lots of folks have muddled their way through and I followed in their footsteps and fell into a few holes.

Smart folks should just use this code / plugin – which is pretty and clean

http://inplacericheditor.box.re/demos

I have not used it – but the demos are gorgeous.

So in this post, I do it the hard way. I won’t go into detail – it is all in the code below. Someday this might make a nice plugin – but it still has a flaw that I compensate for. Here is the basic outline: (1) include TinyMCE in the portal once – also init TinyMCE in portal.rhtml like this:


<%= javascript_include_tag "tiny_mce/tiny_mce.js" %>
tinyMCE.init({
mode : "none",
theme : "simple"
});

(2) In the div that will hold the editor – at the end of the output put in some Javascript to activate TinyMCE on the textarea in question, (2A) While you are there resize the columns so the text area is nice and wide, (2B) – before activating TinyMCE – figure out if TinyMCE has been pithed by too much Ajaxing – if a getInstanceById throws an error (even if not found it should return a null) – throwing an error indicates that MCE can’t even tell if something is *not* there. We declare MCE hosed, don’t even try to make it work – because it actually works half way, (2C) if it turns out getInstanceById works (returning null is OK) – we assume MCE is alive and activate MCE on the text area in question.

The next trick is catching the Save and Cancel (1) First we do a save from TinyMCE back to the div with a catch block so that when we are not even using TinyMCE because is unconscious – we will not confuse Prototype throwing errors, then (2) once the Ajax request is complete (onSuccess) we deactivate the TinyMCE attached to out textarea (again in a try/catch to eat errors if MCE has been knocked silly). Putting this in onSuccess is perfect because by then the server is updated and we are milliseconds away from wiping out the div – so we de-associate the MCE from the text area *just* before it vanishes in a puff of smoke.

So what puts MCE into a sleep-like state? When you are on an exit screen and you overwrite the div containing the editor from an external navigation operation. In Toozday – this is like going into Wiki, Editing a page, and then without pressing Save or Cancel you switch to Profile (oops – MCE left in limbo), then when you come back to Wiki and Edit – MCE is in a state unresponsiveness – waiting forever for its lost textarea to come back to it – which will never happen :(

I detect this in the Javascript and simply don’t use a zoned-out MCE – let users type in a text area I say! I warn the user to press Save or Cancel before continuing – hey if I charge their credit card twice – it is their fault!! Sooner or later users will learn to press Save or Cancel.

This condition is naturally reset when the whole page is refreshed – in Tooz this is on a Refresh, All Sites, or Site to Site Navigation. So MCE will start working again – leaving the user to wonder about the IQ of the system they are dealing with! MCE seems to come and go almost on a whim :)

Someday I will figure this out – but it is now on to more other fun stuff. Golly – everything I do ends up with some sticky wicket. Ah that is the fun of it all!

Below is the sweet code – with a lot of cool helpful urls that saved my arse and led me to the solutions. The URL to source is http://toozday.rubyforge.org/svn/trunk/app/

Continue reading

My First Monkey Patch for a Rails routes.rb bug

For the past five days I have been struggling with a bug in the route code in rails 1.2.3. I finally monkey patched url_for to essentially force it to do the obvious.
In this blog post, I describe the bug and how I reproduced it and then the patch to work around the bug.
There is a 10% chance that the routes is actually working and I just do not understand it. I read in the Agile book something about routes trying to use the shortest URL where possible – but the problem is that I have these long URLs to pass bits of session-like information around between my ajax calls – particularly when I want to go cross domain with my Ajax (i.e. I want to use a Rails tool from one server included into a web page from another server where cookies and session information will not be usable).
The bug comes down to when I call something with a long url with lots of stuff defined and then I generate a new url adding an id which is always at the end of the routes, url_for decides to “forget” the stuff that was to the left of it in the incoming request URL. Changing action only generates the *right* url while changing the action and id generates the wrong URL.
My Urls have three basic forms
/imsti.fe828-12345-bfea.1.right-content/comments/ajaxstart
/ajax.1.right-content/comments/ajaxstart
/comments/ajaxstart
The first two are for use in divs and never shown to the user. The imsti one both establishes a session and sets up the context and div name when the tool is being used cross-domain. The second is used when you are not going cross-domain and cookies are available to establish session because the ajax and the enclosing container are coming from the same server – so incoming ajax requests get the cookies.

Continue reading

Two Days – Two Rails Bugs

Rails is pretty awesome – but it is clearly a moving target in terms of how it works. Things work and then they break in different versions.
Today I am playing with routes and all I want it to do is check the routes in the priority order from first to last – but it seems to get confused when I add routes that don’t match at all in the routes.rb.
Yesterday’s bug was the breakage of the cookie_only feature by people who were intent on “fixing” a security bug – their fix broke a feature.
I get the sense that the bright folks in Rails have been working on 2.x for a while and that the second string in doing the maintenance on the 1.x.
I have a bad feeling that I will be checking out a copy of the rails source and fixing the damn bugs myself. Perhaps I should upgrade to the latest rails. I just don’t know – stuff that is solidly documented in the Agile development book is simply broken in the versions of rails that I run across.
Perhaps I am just down after wasting 20+ hours – first doubting myself and trying 50 ways to see if something was my fault – only to realize that it was a bug in the Rails framework.

Continue reading

Nifty Leopard Image Trick with the Dock

Here is my Leopard dock:

From left to right – these folders are my outreach, research, personal, publishing, rails, and teaching folders…
The trick is getting the image to show in the dock.
In each folder put an image named aaaa-something.gif so it sorts to the top.
Then tell the dock to view the folder sorted by name.
Your image wil be the icon!

Leopard Upgrade Redux

I upgraded to Leopard again – this time I did it the way I have always done it. Back everything up – blast the hard drive completely and then a fresh install.

I figured that some clever gadget would help me restore my home directories from my backup – perhaps I just cannot figure out newfangled gadgets. So here was my process – feel free to educate me.

Boot into the CD and run the Disk Utility

Make an image from my main partitions onto a USB drive – I did not really want an image – but it was all Disk Utility wanted to let me do.

Blast the main disk in my laptop and install Leopard

Create a new account called “csevadmin” as part of the install – do not create the all important “csev” account yet.

Once log in is complete, you are logged in as csev admin.

Make a new account called “csev” – make it an admin.

Still in csevadmin pop up a terminal window (finally a decent user interface) – Become root

sudo sh
cd /Users
mv csev csev.org
cp -r /Volumes/Blah/Users/csev /Users

Note: Using cp was a bad mistake – I reset all my dates. I should have read my own blog post and done this instead:

cd /Volumes/Blah/Users
tar cf – csev | ( cd /Users ; tar cfv – )

That would have saved my file dates . Ah well onward and upward. Once the directory /Users/csev is in place you need the magic chown command – which is now different in Leopard.

chown -R csev:staff csev

The “staff” is new – it makes a lot more sense than making a group for every user – but it is different. Also I now learned the modern “:” form of the chown so I don’t get nicked for using deprecated syntax every 18 months.

All in all I am much happier with a clean install – even though it took me hours to reinstall all the apps – I lost media for iWork06 so I went and bought iWork08 and I will need to go buy iLife 08 as well because of lost media – heck I need to upgrade anyways.

Oh yeah – and Time Machine is working – It was easy to exclude my Final Cut Express Documents. It did not work on the first try – but I went to sleep – in the morning all was better. I like that kind of software.

I have never really ever backed up my computer until now – thanks Apple.

Really cool trick with Leopard – Drag a directory into the doc and put an image in the directory named aaa-something.gif so it is the first thing in the directory – then the doc will see it and the image will appear on the folder – my rails_apps folder on my dock has the cute rails logo on it using this technique by naming the image aaa-rails.png

I am so loving the Rails and Ruby install on Leopard.

Plaxo still does not work – even with a fresh install of everything. I guess I will just book mark Google
calendar on my iPhone and give up using my iPhone’s calendar – too bad – Plaxo was sweet while it worked (4 weeks). Plaxo felt like such a Rube Goldberg contraption byways – it is not surprising that the wheels came of on on OS upgrade.

Luckily – Google Calendar has an iPhone view.

I will let the dust settle on calendars – if Spanning Sync releases a 10.5 version – I may just buy – it some things you should pay for to motivate folks to work.

Here is my holiday wish for Apple:

In the migration tool – Allow me to (a) mount disk images and (b) find the place where the old /User’s directory is. The current UI is too simple and does not handle the common case – it really wants to back up and restore onto the *same* computer – not an external drive.

Also – we need new operating system releases more often so I don’t forget how to upgrade my system in between releases. Next time it will be too long and I won’t even come back and read this blog post. And again I will be whining – but oh well.

Here is my New Years Resolution:

Since I am an AppleSeed Participant – I promise to test the upgrades for the next O/S – this last released I focused on testing the X/Server stuff – next release I will test the desktop stuff as well.