Archive for the 'Code' Category

When JavaScript and Flash aren’t playing well together

Making small changes to an existing website presents its own unique challenges, as ideally you’d like to change only what is absolutely necessary. When the previous builders have used their own CMS, and Javascript which was automatically generated by a Macromedia/Adobe product (oh, yes), not to mention a little flash thrown in for good measure, it can amount to basically reading pages of code to figure out what’s going on before you do anything.

In this case, when all was said and done, we had a Javascript dropdown menu which dropped over a little design element that had been done in flash. Problem was, the Flash always wanted to be overtop of the menu. Setting unreasonably high z-index properties for the menu elements did not seem to help.

Solution

This may or may not help others, but in this case the secret sauce was to add <param name="wmode" value="transparent"> among the other params, and also to add wmode="transparent" as an attribute in the embed tag for the Flash object.

I think a better solution would be to not use flash in this particular place — but I’m not really interested in fighting that battle this time.

RailsGuts, Blank

I’m in the process of working my way through the “Rails Routing From the Inside Out” guide at RailsGuts.com. I’m not all the way through the document, yet, but so far it looks like the best such document I’ve seen, short of reading the code yourself with someone holding your hand as you do so. In fact, there is a fair bit of the more informative bits of the Rails source code sprinkled into the guide, so the image of someone guiding you through the Rails source is not too far off the mark.

The site also includes a walkthrough of the Rails initialization process, which looks very informative, as well.

—–

Also interesting is Blank, a starter Rails-app along the lines of Bort, but assuming use of shoulda rather than RSpec, among other things.

acts_as_monetized

Well, Jason Calcanis has written something about startups and the economy.

One of his suggestions is that if your company doesn’t currently have a revenue stream, it needs to get one, like yesterday. Or at least by Monday.

I propose a Rails plugin, which we will call acts_as_monetized. It will automatically track your new users, and after a 30-day trial period will stop letting them use any models which include act_as_monetized until they start paying you.

The age old formula will now be:

  1. Create application.
  2. script/plugin install acts_as_monetized
  3. Profit!

You’re welcome.

(Building something people are willing pay you for will not be included in the plugin.)

Ruby Tidbits: Sentencizing A List of Things

Say you have an array. Maybe it is ["Larry", "Moe", "Curly"]. You’d like to display this as a sentence, not just a join, so it would read "Larry, Moe and Curly". That would be nice. Of course, you can’t guarantee that your array will have three items. Maybe tomorrow it will be just ["Frodo"], or ["Groucho", "Chico", "Harpo", "Zeppo"].

No problem. Let’s say your array is list. You could write:

1
2
list.size > 1 ? "#{list[0..(list.size - 2)].join(', ')} \
  and #{list.last}" : "#{list.first}"

Redtip: Rails plugin

I’ve finally got around to putting Redtip, a Rails plugin I’ve been working on, up onto github. It can be found here; I also have a minimal project page (for now) here.

Redtip is mainly a few helper methods and a lot of JavaScript that gives you some tooltip-like popouts to use, or some that act a little more like dropdown menus. I needed that sort of functionality for some features I was working on, and wound up basing the plugin off of Craig Ambrose’s Redbox, which was in turn based on Thickbox. So, hooray for code reuse. In the words of Hal Abelson, if I have not seen as far as others, it is because giants were standing on my shoulders.

If you want to check it out, try script/plugin install git://github.com/philcrissman/redtip.git

Stupid Ruby Tricks

I can remember, once, long ago, being entranced with the idea of writing programs whose output was identical to its own code. This is, of course, entirely impractical and not really good for anything.

Ruby has a built-in variable, $0, which refers to the name of ruby program currently being executed, and which therefore makes the writing of self-describing programs trivial. So, you can write a ruby program which outputs itself like so:

1
2
#!/usr/bin/ruby
IO::readlines($0).each{|n|puts n}

Pretty nifty. Can it get any shorter? If you’re willing to use bash commands, yes it can:

1
2
#!/usr/bin/ruby
system("cat #{$0}")

If you want to dispense with the shebang and run it with ruby stupid.rb (or whatever you’d like to title your work of art) then you can shave another 15 characters off of it. The shortest way I could see to get it would be

1
puts `cat #{$0}`

which assumes you have to run it with ruby ... rather than just with it’s own name. That’s all rather pointless, of course.

Of course, the $0 variable also means that you can do exciting things like rewrite the file which is currently executing while you’re executing it:

1
2
#!/usr/bin/ruby
File::open($0,mode="w").write('puts "Hello #{`whoami`}"')

Have fun with that. Oh, and, um… only try this at home.

Some Ruby & Rails Tricks

I certainly can’t claim to have discovered these tips; but they’ve been pretty helpful to me. Maybe this will just make them a little easier to find for whoever is looking for them next.

Getting A Collection’s Index From Within a Partial

Lets say you’re passing a collection to a partial, but that somewhere in that partial you are interested in knowing which index you’re at. Do you need to implement a counter? Nope; there already is one. A collection passed to a partial implicitly creates a counter called [partial_name]_counter. So, if you called with

1
< %= render :partial => 'thing', :collection => @a_few_of_my_favorite_things %>

then in your partial, you could use:

1
< %= thing_counter %>

to return the index as you plow through the collection. Life is good. Rails is looking out for you. (Hat tip to GMarik, which is where I saw this one.)

Obtaining a Class name From a String

Today I found myself wanting to take a string, let’s say "Thing", and use it as a class name, Thing. Again, it turns out this can be done. In Ruby, class names are constants, so you can do

1
2
str = "Thing" #=> "Thing"
Kernel.const_get(str) #=> Thing

Hooray!

Make Finding Easier

Find yourself writing long, complicated find expressions in your code? There is a better way.

Perhaps in your code… maybe even in a view… maybe you want to find all Things that don’t belong to the current use, and which are flagged with some property, and you’re temped to write something like:

1
Thing.find(:all, :conditions => ["user != ? and flag = true", current_user])

What we could do is put some has_finders in the Thing model, something like

1
2
3
class Thing < ActiveRecord::Base
has_finder :exclude_user, lambda {|user| {:conditions => "things.user_id != #{user.id}"}
has_finder :with_flag, :conditions => "things.flag = true"

Having done that, you could now replace that first bit with:

1
Thing.find(:all).with_flag.exclude_user(current_user)

It’s a little shorter, sure, but more importantly, when you read it, it just makes more sense. A completely new person coming to look at this line would have a good idea what it was doing without even looking in the model to see what the has_finders were doing.

Okay then

If you’ve spent any amount of time using rails, these probably aren’t new to you. Everything’s new at least once, though, so hopefully these are helpful to someone.

Other favorite ruby or rails tricks? Leave a comment! Heck, we’re not prejudiced, here, go ahead and comment with your favorite Python or Perl or [other language] tricks, too, if you like.

Flex and Rails

I’ve started to be interested in Flex, recently. This interest piqued when I saw some of the rates that were being offered for Flex developers in this area, so I suppose my interest is completely mercenary and not particularly motivated by an interest in the technology, per se. I think that I’m okay with that.

That said, it does look interesting. And, naturally, some people are using Flex front-ends for Rails applications: one good resource in particular looks to be http://flexonrails.net/.

Also interesting is Project Sprouts, which is a gem and seems to make good use of ruby and rake to automate some aspects of ActionScript, AIR, and Flex development. I’ve had no time (yet) to give it a test drive, so this is nothing more than a pointer: if it sounds interesting, give it a look.

Since I’m thinking about ActionScript, this is as good a time as any to revisit the venerable ActionScript Jabberwocky. Enjoy.

The ActionScript Jabberwocky, from http://www.turdhead.com/2004/08/16/the-actionscript-jabberwocky/

The ActionScript Jabberwocky, from http://www.turdhead.com/2004/08/16/the-actionscript-jabberwocky/

Startlingly Obvious Truism Becomes Revealed As More True Than Previously Thought

I’ve had the opportunity to work with Rails for a good three weeks straight, now. Not just evenings and “spare time”, like I might have before, but actually all day. Most of that has been front-end work, views and the like, but enough of that is tied to other parts of Rails that you definitely start to get a better picture of how it all fits together.

Hence the title, as I realize anew what everyone already knows, that you learn more by doing something than by reading about it.
Continue reading ‘Startlingly Obvious Truism Becomes Revealed As More True Than Previously Thought’

Shiftspace for Firefox 3.0

I’ve been lamenting the loss of Shiftspace silently and sullenly ever since I upgraded to the beta versions of Firefox 3 last year. I really didn’t want to downgrade and wait, though, since I was experiencing a few unhappy issues with Firefox 2 on Mac OS which happily disappeared with version 3. So, I did without Shiftspace, cool though it may be.

No longer: there is a version of Shiftspace available which works with Firefox 3.0… you can find it here; you will want to click on the “release userscript” in the bottom right-ish corner.

Should I add you’ll need Greasemonkey, first? Well, I guess I have, now.

While searching for this, I happened upon an app called Lily, which is a visual programming environment written in Javascript:

Lily is a browser-based, visual programming environment that lets people create programs graphically, without writing code, by drawing connections between data, images, sounds, text and graphics. Lily’s cross-platform, free, open source and is written in JavaScript. Did we mention it’s fun?

What does it all mean? I don’t know! Isn’t “it’s fun” enough for you?