ruby

Decorating Sorcery's current_user with Draper

I already wrote about how to apply your decorator to the current_user when you’re using Devise. However, the trick is a bit different when applied to Sorcery. Instead of being nil when no user is signed in, Sorcery uses an explicit false value, no nil. In your ApplicationController at app/controllers/application_controller.rb add this: def current_user UserDecorator.decorate(super) unless super == false end I’m using the most recent version of Draper by the way.

Ruby: regex scanning in a case statement

Here’s a handy ruby snippet that might come in handy one day. When the regex matches (input should end with “ today”), you can directly grab the matched value using the special $1 variable. case input when /(.*)\stoday$/i then puts "Today: #{$1}" end I think you can see how you can bend this to your own needs.

Running a different ruby with Passenger 3.2 and RVM

Passenger 3.2 will have quite some nice new features. 1 2 The features I’m looking forward to most is the ability to specify - per virtual server - which ruby to use. Before, you installed passenger and specified the required ruby version using passenger_ruby, like this in your nginx.conf: http { passenger_root /opt/passenger; passenger_ruby /usr/local/bin/ruby; server { server_name ariejan.net; passenger_enabled on; } } Now, if you added another server it would be forced to use the same ruby version.

Showing Ruby, Rails and git info in your app

Some people’ve asked me how I show rendering information on ariejan.net. There are a few things going on here, let me explain them one by one. Rails version The current Rails version is probably the easiest you see here. Rails exposes its version information like this: Rails.version Ruby version Ruby also exposes version information, albeit using constants: RUBY_VERSION => "1.9.3" You may know that ruby also has different patch levels for each release.

From 11.34s to 0.625s for opening a .rb file in Vim

Would you believe me if I told you that opening a simple Ruby file on my 2011 MacBook Pro takes 11.34 seconds? To test this, I’ve used this command: $ vim --startuptime log-before.txt app/models/user.rb This command will time everything that Vim does until the file is ready for you to edit down to the millisecond. This is a great way to find out what’s slowing things down. I’ll highlight the most interesting parts of log-before.

Fast specs - Run your specs in less than 1 second

Okay, let me clarify that title first. I, as most of you, have two sets of tests for my Rails application: rspec and cucumber. rspec heavily focusses on testing models and business logic while cucumber focusses on testing the entire application stack and user interaction. The problem is that as your app grows, your test set grows - and so does the time it takes to run those tests.

RSpec speed-up (24.6%) by tweaking ruby garbage collection

Today I learned that Ruby garbage collection can be of huge importance to performance. More precisely, if Ruby does a lot of garbage collection it may slow down your code. Running garbage collection only every 10 or 20 seconds when running specs may increase performance dramatically. At this time specs for Ariejan.net take an average of 25.29s to run. This is not bad, but in my opinion faster specs are better.

Rake with namespaces and default tasks

Rake is an awesome tool to automate tasks for your Ruby (or Rails) application. In this short article I’ll show you how to use namespaces and set default tasks for them. ~ Let me first tell you what I want to accomplish. I have a Rails application that needs to be cleaned up occasionally. Essetially we delete old data from the database. There are several models that each implement a cleanup!

Narf: A Ruby Micro Test Framework

I’m a happy user of RSpec, Cucumber and sometimes Steak. Great tools to write specs and features and prove my application does what it’s supposed to do. But sometimes I have the need for something more light weight. ~ For example, sometimes I need to write a single ruby method. Just something ‘quick’ to import a file, convert some data or whatever. Being a good citizen I want to test that method.

Firefly 1.1.0 adds QR Codes for your shortened URLs

I just pushed Firefly 1.1.0 (code) to Rubygems containing a very nice new feature: QR Codes. Why would you care? Well, almost anything can scan QR Codes nowadays. Simple add .png to the end of your shortened URL and you’ll get a nice QR Code that you can print or embed on somewhere on the web. Give it a try: http://aj.gs/2Y.png Simply scan the image and it will yield your short URL, ready for use.