If you're a rails developer you've probably tackled forms which update multiple models and have their own custom validations and/or business logic involved. I'm talking about forms like Signup which may need to create a few other models after the User has successfully been saved.

I've recently had to build a signup form which would create or organization, then create a new postgres schema for the organization, then build the administrative user model and also capture payment. Doing this with simple after_create calls would most likely wreak havoc later in life so I've split this out into a separate Signup model.

Continue Reading

If you need to customize the field name for ActiveRecord validation messages, I wrote this quick little include that allows you to remap column names.

For example, if you have a User with a name field and you didn't like the default "Name cannot be blank." message, and instead wanted "Your name cannot be blank" you can include this module and use rename_fields :name => 'Your Name' to rename the column.

Continue Reading

I was writing an API in rails a while back and as I started to write the client I noticed that, while using Devise's TokenAuthenticatable strategy, there was no other way to pass the token in besides the ?token=XXXX parameter.

This was a little odd since you would expect something like a token to be able to be passed in through the Authorization headers. I ended up writing a custom devise strategy that will now allow you to pass your user's token in via the Authorization header.

Continue Reading

Making advanced search forms for your backend kinda sucks, right? I ran into this problem with a project and decided to come up with something similar to how Google handles search queries. No dropdowns, no checkboxes, no radios, none of that stuff. I just wanted to be able to type status:active user:rob [term] and get what I asked for. I came up with a pretty nice utility class that can handle some basic query syntaxes and give you back hashes or arrays and the left over query.

Continue Reading

I may be a devout rubyist, but I still have a soft spot for WordPress -- and maybe even a little for PHP too. One of the things I think is lacking is a good way to manage WordPress updates since, as we all know, WordPress updates constantly. I wrote this quick thor script to help me update to the latest version from the command line so I can do quick version testing. As an added bonus, there is also an export task as well which will simulate doing a wordpress XML export from the dashboard.

So, for all you WordPress guys out there who hate doing manual updates, check this out. It may make life a little bit easier.

Continue Reading

If you look around this site you'll notice that I'm hosting all my code snippets on GitHub's awesome gist feature. While this allows me to not maintain syntax highlighter plugins and libraries, as well as lets me change the code snippet without updating the blog is nothing short of awesome. The only thing that kind of sucks is that I/we are losing the ability to have our code be search engine friendly since a gist is a 1-line javascript include. This sucks not so much from the "index me!" perspective, but more of the "if I google a line of code/error, will I find relevant code?"

To tackle this, I ended up with a small project: given a gist ID, generate the embed code along with NOSCRIPT tags so the content is indexable. This will only solve the initial problem, as updates will not propagate to the blog, but... you can always manually update.

Continue Reading

This one is a pretty specific hack. I've had the need to build mini reporting apps, using Sinatra, that aggregate data from multiple databases and produce a report. This is all fine since ActiveRecord is awesome, but I've always missed AR's query caching ability.

After some digging I found that query caching is only enabled on AR's Base connection and all other models must use that awkward Model.cache{} construct -- which is annoying to use. I've hacked up a solution that works, although it relies on :send and instancevariableset which isn't very elegant or solid.

For what it's worth, here is a "simple" way to get Sinatra to use ActiveRecord's native query caching for more than 1 database:

Continue Reading

I use contentfor a lot in Rails code, but a large percentage of my projects are done as a simple Sinatra app, so I find myself missing a lot of features that Rails has. One big one was contentfor -- so I drafted this up to fill the void.

It is similar to how rails 2.x implements it, so in your main layout you can check if that content is there and if so display it, otherwise you can use defaults, or ignore it. This particular gist uses it to create custom sidebars based on what page you are on: if you are logged in, in a particular area, etc.. I like this approach as opposed to having a single sidebar partial with tons of display logic, I just think it keeps thing tidy.

Continue Reading

I was creating a project the other day that used anonymous pages for users and I wanted to make it a little less "guessable" but more human friendly than a SHA1 hash, so I decided to make a small home-brew URL shortener similar in format to Tiny URL.

The neat thing about ruby is that when you turn a Fixnum to a String with tos you can specify the base, and when you turn a String back to a Fixnum you can also specify a base in toi. This lends itself perfectly to turning (large) IDs into a shortened URL thats easier to remember.

In the first gist you can see how this would work with a model to make simple url shortening in rails -- or, you can use randomized values to generate more user friendly "unique" tokens (as opposed to using SHA1 hashes for user confirmation/password reset pages).

Continue Reading

I recently had a need to parallel process some tasks for a non-critical app, but really didn't want it to get out of hand. I wrote this as a simple way to multi-thread without the pain of worrying about synchronizing and queues and being thread safe.

It achieves ghetto thread pooling by chunking the result set into whatever size slice is needed and spins up some threads, once that set is done it will repeat until the data is all processed before moving on.

It ended up working out really well as a direct drop-in, and speeded up the processing greatly. I wouldn't recommend this for production code, however.

Continue Reading

I've been spending a lot of time creating reports lately, and I generally live in a terminal, so naturally I started to think of ways to easily send emails with these reports attached. I'm stuck using Microsoft Entourage here at work, but thankfully they have a pretty robust AppleScript library. Mixing some stuff together I pulled together this Ruby script which will allow me to do some neat stuff:

  • pipe data into the email command to create a body
  • use aliases as groups to send emails to common groups of people, etc
  • add multiple attachments to emails straight from the command line!
  • not have to move my hands from the keyboard when sending emails!

At its core, it is nothing more than a simple ruby shell script which compiles AppleScript and uses osascript to execute the AppleScript. When run it will pop open a new Entourage mail window with the To/CC/Subject filled out, as well as any attachments and moves focus to the window. If I had piped anything to STDIN that would be the body, otherwise it would blank and ready for a quick message. Then a quick CMD-ENTER and off the mail goes. Simple and elegant, yet kind of powerful :)

Continue Reading

I was writing a small shell script in Ruby today that needed to check STDIN for data, but I didn't want to prompt the user to enter data when nothing was found (I only wanted piped in data). This turned out to involve a little more effort than just checking STDIN.read.

Luckily, I found a solution at Footle that worked out great. Heres to anyone who needs only piped in data from STDIN without prompting the user.

Continue Reading

Sinatra is awesome, but Rails has a lot of useful features. One of those is Active Record's Query Caching. I never realized how useful this is until I started to optimize Sinatra apps and felt its absence. Here is a simple way to enable active record query caching in Sinatra.

Continue Reading

This is a simple, but useful little script I ended up writing to test some things. I found it easier to use this than to constantly use the mail command. It basically wraps action mailer in a small ruby script so you can call it from the command line.

./spoofmail.rb --to=who@where.com --from=sjobs@apple.com --subject='Hey You!' --body='whatsup guy?'

or using STDIN

./spoofmail.rb --to=who@where.com --from=sjobs@apple.com --subject='Hey You!' < bodyofmessage

cat|./spoofmail.rb --to=who@where.com --from=sjobs@apple.com --subject='Hey You!'
whatsup guy?
^d

It might come in handy one day when you need to mess with a co-worker or test a mail receiver... or something else?

Continue Reading

I was recently overhauling my permissions for a big rails app and needed to see which filters were on which controllers as an overview. I came up with this little snippet which will go through each of your controllers and print out which filters are on them, as well as their options.

Continue Reading

This is admittedly ghetto, but I needed a super simple small-scale solution to creating unified diffs based off large blocks of text. The easiest way to do this was by creating temp file and using the native diff command to do the comparison.

I've used this method for a really small snippets app for our intranet, which can be found on GitHub.

Continue Reading