Monthly Archives: March 2011

Adding additional processing support to CarrierWave

Posted in Code, Inside TFG, Ruby on Rails, Tips and Tricks, Websites or Tools

CarrierWave is a great gem for adding image uploading and basic processing abilities to your Rails applications. By default there is no way to set the quality of the resized images, which could be a very useful feature.

One of the applications that we’re currently building has a requirement to resize and compress images to produce smaller file sizes, as well as stripping out any personal information that may be stored in the uploaded images.

Out of the box CarrierWave provides a consistent interface to process images using RMagick, MiniMagick or ImageScience. Resizing and cropping is supported for all three image processing engines but setting the quality or removing personal data is not supported. Thankfully, CarrierWave provides an easy way to extend the default functionality so we can do more.

For the examples I’ll be adding extra functionality to RMagick processing. If you’re using MiniMagick or ImageScience the methods will need to be altered to work correctly. We’ll start by adding a new initializer into our application.

 

You may be wondering about the fix_exif_rotation method. Well, some modern cameras always take photos upright, in portrait. When you take a photo in landscape mode the photo is actually saved as a portrait, but the photo will contain some extra orientation metadata. When we remove the embedded data in the photo this value gets removed, so as a result we need to manually rotate the photo, which is what this method does.

The application server will need to be restarted before the new initializer can be used. Once restarted the new filters can be used in an uploader like so:

 

That’s it! That’s all that’s needed to add extra quality and processing functionality to carrierwave.

Accessing ActiveRecord and ActiveModel Callback Chains in Rails 3

Posted in Code, Ruby on Rails, Tips and Tricks

It seems that access to the ActiveRecord callback chain has changed recently, however the documentation still contains the following :

To list the methods and procs registered with a particular callback, append _callback_chain to the callback name that you wish to list and send that to your class from the Rails console.

This used to work in Rails 2.3.x and I’d used it a few times before, however trying to use it in Rails 3 gave me problems :


HourEntry.before_save_callback_chain
NoMethodError: undefined method `before_save_callback_chain' for #

After some digging around ActiveRecord then ActiveModel and ActiveSupport I figured I could just access the _#{type}_callbacks method and then reject and select these arrays to get what was required. This is what I came up with to get the before_save callbacks that I was interested in.


HourEntry._save_callbacks.select { |callback| callback.kind.eql?(:before) }

This returns me the array of callback objects that are chained before I call save on the HourEntry model. If I want to check for a particular proc being in that chain then I can do :


HourEntry._save_callbacks.select { |callback| callback.kind.eql?(:after) }.collect(&:filter).include?(:archive_if_no_users)

It’s not optimal chaining a number of enumerable methods but I think it seems pretty readable.

I am hoping that the callback_chain methods return at some point (though I only used them sparingly) but more than that I’m hoping someone might be able to set me straight on how I’m going about getting around the callback chains on ActiveRecord and ActiveModel.

Composing Scopes On Multiple Models with Rails 3

Posted in Code, Ruby on Rails

Today I had some knowledge delivered straight to the top of my dome, by Darcy. The issue was I had two models that I wanted to join and then apply scopes to. I wanted to retrieve Story objects that were joined to HourEntry objects, that were from this week. However when I tried to use the Time scope on the query it was attempting to call those methods on Story.

ree-1.8.7-2011.02 :132 > Story.joins(:hour_entries).this_week
NoMethodError: undefined method `this_week' for #

The problem was that the .this_week call was being sent to Story rather than to HourEntry. Here’s where Darcy chimed in with the scoped.merge call. It went a little something like this :

Story.scoped.merge HourEntry.this_week

This successfully merged the scope defined by Story with the scope defined by the HourEntry.this_week call and I get all the stories that have HourEntry objects that are returned by the .this_week scope.

The final stage for this call was :

Story.scoped.merge HourEntry.worked_on_by(user).this_week

This then returns all the stories you’ve worked on this week. Add a bit of grouping/distinct and it’s done and done. The additional nice part about this is composing these scopes will allow me to do a lot more in the future, very quickly.

Broken Arrow Keys – Ubuntu 10.10 / VMWare Fusion / Macbook Pro

Posted in Inside TFG, Tips and Tricks

After installing Ubuntu 10.10 (Maverick Meerkat) in a VMWare Fusion virtual machine on a MacBook Pro, I noticed the arrow keys were broken in Ubuntu. It turned out that the Fusion install wizard had configured the keyboard to use evdev, which did not work correctly inside the VM.

The solution is to reconfigure the keyboard by running the following command:

sudo dpkg-reconfigure console-setup

To navigate the setup program without the arrow keys, I pressed the first letter of the desired option (eg. “a” for Apple Laptop), then pressed the right command key to cycle downwards through the list.

The configuration options that I chose were:

Apple Laptop for keyboard
USA as origin of keyboard
USA as keyboard layout
No AltGr
No Compose key
Enter for the rest of the defaults.

The arrow keys work fine now.

What to Call a Company That Makes Websites?

Posted in Industry Trends

A short time ago, I called out for people to let me know what names they could think of to represent a company that makes websites. The goal was to put on the shoes of a prospective client, and see if they could identify with any particular term.

Companies that work with computers in any way, usually get lumped in the IT category. There is such a wide range of companies under this banner, it is very confusing for both clients and companies alike.

Even asking for alternatives for “web designers” brought back a huge response. No wonder clients have such a hard time understanding who they need to be working with.

The responses:

  • digitial firm
  • digital creatives
  • web creatives
  • web firm
  • dev agency
  • web design agency
  • creative agency
  • web shop
  • web services agency
  • web design studio
  • digital creative agency
  • web design shop
  • web studio
  • creative studio
  • digital branding agency
  • boutique branding agency
  • digital studio
  • the IT guys
  • graphic designers

Maybe we should just split it into the following:

  • online or offline
  • print or web
  • hardware or software

Have you heard of other industries where there is as many legitimate (and widely used) names for the one branch of work? Let me know in the comments.

Search Posts

Featured Posts

Categories

Archives

View more archives