logo
Home   About   Blog  

Recipe: Detect whether cookies are enabled in rails

September 29, 2009
By Jo Hund

If your Rails app requires cookies, this recipe is for you: It detects whether cookies are enabled, and if not, shows a message to your users.

This recipe checks for the presence of a specific cookie (“cookie_test”). If it is present, the request continues normally. If it is not present, then it sets the cookie, redirects to the “cookie_test” action and checks there whether the cookie is present. If the cookie is present, then everything continues on normally. If the cookie is still not present, then this is a strong indication that the user has cookies disabled. Then the recipe shows a page to the user explaining the cookie requirement. I leave it up to you to write the copy for that page.

Flow chart for the cookie detection recipe

Flow chart for the cookie detection recipe

This recipe works as of Rails 2.3 (new cookie handling). It is inspired by this article. The recipe in the original article does not work with newer versions of rails. So I updated it and packaged it up in a neat little module that you can include in your application controller.

More …

Simplest way to use cron with Rails

March 30, 2009
By Jo Hund

For simple applications I use cron to run automated background jobs like sending emails or indexing sphinx.

I like to have all aspects of my application under version control. Cron is no exception. To do this, I add a file named “crontab” in /config. In there I add all my cron jobs in regular cron notation.

More …

Recipe: RESTful search for Rails

December 14, 2008
By Jo Hund

This recipe shows you how to search, filter, and sort your resource lists in a restful way. We will look at the most simple way to accomplish this and then provide some pointers to further improvements. This recipe works great with will_paginate. It is an end to end solution (model, view, and controller). This recipe requires Rails 2.2.2 or greater if you want to use :joins in named scopes. Otherwise Rails 2.0 is sufficient.

Here is what you will get from this recipe:

List options in the Quentin time tracker

List options in the Quentin time tracker

More …

strftime cheat sheet

December 7, 2008
By Jo Hund

I have used strftime a lot lately on one of my projects. I could not find a user friendly reference for strftime, so I wrote one. Here is my user friendly strftime cheat sheet in PDF format:

strftime reference

strftime reference

In case you are wondering what strftime does: It allows you to represent a date/time as a formatted string. Example:

Time.now.strftime('%A, %l:%M %p')
=> "Friday,  9:06 AM"

And here is some handy Ruby code to look at the options for strftime:

def print_line(char)
  puts "%#{char}: #{Time.now.strftime("%#{char}")}"
end

('a'..'z').each do |char|
  print_line(char)
  print_line(char.upcase)
end

Tablet pressure support on OS X for X11, Gimp, and Inkscape

December 6, 2008
By Jo Hund

Here is what I had to do to enable tablet pressure support for the X11 based Gimp and Inkscape applications on OS X Leopard:

My setup:

  • OS X Leopard: 10.5.5 on Intel
  • X11: XQuartz 2.3.2 RC2
  • Inkscape: 0.46 (Mac binary)
  • Gimp: 2.6.3 (Mac binary)
  • Tablet: Wacom Graphire ET from around 2003

Key points are:

  • get at least the 2.3.2 version of XQuartz. 2.3.1 will not work!
  • In Gimp/Edit/Preferences/Input Devices: Click on “Configure extended input devices…”, set device ‘pen’ to ’screen’.
  • In Inkscape/File/Input Devices …: Set device ‘pen’ to ’screen’.
  • You might have to restart your apps

Nice. Now we have pressure support on OS X for these two great apps. The Inkscape calligraphy tool is very nice. Makes my handwriting look nicer than on paper.

OpenOffice.org 3.0 for Mac – a big deal

October 17, 2008
By Jo Hund

it’s been officially released for a few days now and I think it is a big deal. OpenOffice.org 3.0 is a big step forward:

OpenOffice.org 3.0 spreadsheet application

OpenOffice.org 3.0 spreadsheet application

  • Native Aqua – means it looks as beautiful as we expect from an OS X app. That has a big impact on my productivity. I enjoy working with beautiful tools. It also means that I don’t need X11. Less hassle and faster startup
  • PDF import – wow, I can now edit PDFs. Very useful when I receive contracts as PDF and have to return them signed: Open contract, insert signature image, save PDF, email back. Sleek. And no need to spend a dollar on PDF editing tools.
  • Better compatibility with MS Office formats

Experts design breadth first

October 11, 2008
By Jo Hund

or What they mean with thinking outside of the box.

Reading this article reminded me of a concept that applies to software design, user interface design, hardware engineering, and pretty much to any situation where you have to solve a problem creatively. I have applied it in a number of disciplines, however I have not used it in my current software design work as much as I should.

It is the concept of designing breadth first, before developing an idea further by going deep. Most creative people I have met (including myself) have the natural tendency to go deep first. They are confronted with a problem, they immediately come up with an idea for how to solve it, they get to work developing that idea, and get stuck in one limited way of thinking.
More …

Recipe: Make request environment available to models in Rails

August 31, 2008
By Jo Hund

How often do you wish you had access to current_user in one of your models? I needed it for an app that required auditing. I had ActiveRecord call backs on the audited models to create audit entries on every record operation. The problem was that the models had no access to the currently logged in user. More …

Recipe: RESTful permissions for Rails

August 24, 2008
By Jo Hund

A common requirement for Rails applications is to check permissions for certain actions on your RESTful application’s resources. There are many ways to solve this problem, ranging from simple boolean flags to full fledged role based access control. I have tried a lot of approaches and have settled on a fairly simple way that is RESTful and quite flexible.

More …

OpenSource project: Quentin Time Tracker

August 10, 2008
By Jo Hund

Being a contract software developer, tracking my time is extremely important for my business: I need to write invoices with task break downs, I want to get better at estimating, and I am just curious about what I spend my time on.

So I filled this need by writing my own Rails time tracker: meet Quentin. I cleaned it up a bit and just published it as an MIT-licensed open source project on github.

Quentin dashboard

Quentin dashboard

More …

Categories

Meta