Your Ad Here

Tag Archive for 'rails'

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.)

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

The haml of the gods

I really like haml.

Haml is just a markup language that avoids a lot of the repetition and extra cruft which xml-based markup requires.

It’s so easy to install and use that writing Yet Another Short Haml Tutorial would be a colossal waste of time. Just visit the above link and you’ll be running in moments.

What? You aren’t using Ruby for web applications? (Not that there’s anything wrong with that) Oh. Well. Then you can’t use haml. Sorry!

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’

Twitter off the Rails? And, so what?

There’s a rumor flying around that Twitter is going to abandon Rails, so obviously this calls for us to stop and ruminate without making even the barest efforts to confirm or deny said rumor. Facts may be added, like a garnish, to later blog posts, like Parmesan cheese to your spaghetti.

For example, Evan Williams says that Twitter has no plans to abandon RoR. Hm. I guess that ought to just stop this particular line of musing right out of the gate.

Never mind. Let’s talk about it anyways.
Continue reading ‘Twitter off the Rails? And, so what?’

Frameworks Are Like Sonnets

Yesterday evening Ruby on Rails came up while talking to my mom, and I decided to try to explain what a framework was. I wound up comparing a framework to a sonnet.

If you’re writing a poem, and you decide to write a sonnet, you have a lot of little things decided for you right away. You now know the length of the poem, and the general structure. You know how long each line will be, and you even have some criteria for the content (iambic pentameter and a certain rhyming scheme). Other than that, you can do whatever you want.

It’s by no means a direct analogy, but because my mom knows what a sonnet is, the concept of a framework made sense to her right away.

I may have overreached just a little to use sonnets as the example. Maybe frameworks are more like limericks. Same idea, though.