Thinking Sphinx is a fantastic plugin for Ruby on Rails that makes using Sphinx pretty much a point-and-drool experience.

But sometimes it may be tricky or inconvenient to perform some operations using Sphinx, and you want to mix in some regular SQL. Here’s a very easy way to combine Thinking Sphinx searches with named scope:

  named_scope :sphinx, lambda {|*args| {
    :conditions => { :id => search_for_ids(*args) }
  }}

Ordering ActiveRecord result sets by RANDOM() with Sqlite3 can be very handy. For example, here’s a named scope that would order records randomly and limit the result set to 5 results:

named_scope :random_five, :order => "RANDOM()", :limit => 5

The only problem with this is, if you’re using SQLite for testing and local development, and MySQL for deployment, your code is going to break because MySQL does not use the same random syntax that SQLite does. You can’t entirely blame the frequently-standards-incompliant MySQL here though, because ordering by random isn’t a part of standard SQL and is added as a stored procedure or vendor extension in most RDBMS’s.

To solve this problem, I’ve created a small monkey-patch to Rails which allows you to use code like the following:

named_scope :random_five, :order => :random, :limit => 5

You can access the code here.

What do you think? If you find this useful, let me know and I’ll submit it as a patch to Rails.

A pastie is worth a thousand words:

http://pastie.org/249500

It’s not pretty, but lets you create a simple in place editor that works with RESTful controllers. Ideally, this should be done unobtrusively, but if you need to pass in a lot of options (for example, to change language used in the controls), then doing this unobtrusive can become a bit of a PITA.

The only “weird” thing you need to do is make your show method have a block that responds to requests for JSON if you want to use the loadTextURL option.

I’ll see about doing this up as a simple plugin if anyone is interested.

If you want to see a nice rails plugin that does some of this in a more unobtrusive manner, check out Pat Nakajima’s better_edit_in_place plugin. It’s still being actively developed but looks like it will soon be an excellent resource.

HasImage is a plugin/gem that allows Ruby on Rails applications to have attached images. It is very small and lightweight: it only requires one column (”has_image_file”) in your model to store the uploaded image’s file name.

It is, by design, very simplistic: It only supports using a filesystem for storage, and only supports MiniMagick as an image processor. However, its code is very small, clean and hackable, so adding support for other backends or processors should be fairly easy.

HasImage works best for sites that want to show image galleries with fixed-size thumbnails. It uses ImageMagick’s crop and center gravity functions to produce thumbnails that generally look acceptable, unless the image is a panorama, or the subject matter is close to one of the margins, etc. For most sites where people upload pictures of themselves or their pets the generated thumbnails will look good almost all the time.

It’s pretty easy to change the image processing / resizing code; you can just override HasImage::Processor#resize_image to do what you wish:

  module HasImage::
    class Processor
      def resize_image(size)
        @image.combine_options do |commands|
          commands.my_custom_resizing_goes_here
        end
      end
    end
  end

Another image attachment library? Why?

The three chief virtues of a programmer are: Laziness, Impatience and Hubris. - Larry Wall

Attachment_fu is too large and general for some of the places I want to use images. I sometimes found myself writing more code to hack attachment_fu than it took to create this gem. In fact, most of the code here has been plucked from my various projects that use attachment_fu.

The other image attachment libraries I found fell short of my needs for various other reasons, so I decided to roll my own.

Examples

Point-and-drool use case:

It’s probably not what you want, but it may be useful for bootstrapping.

  class Member < ActiveRecord::Base
    has_image
  end

Single image, no thumbnails, with some size limits:

  class Picture < ActiveRecord::Base
    has_image :resize_to => "200x200",
      :max_size => 3.megabytes,
      :min_size => 4.kilobytes
  end

Image with some thumbnails:

  class Photo < ActiveRecord::Base
    has_image :resize_to => "640x480",
      :thumbnails => {
        :square => "200x200",
        :medium => "320x240"
      },
      :max_size => 3.megabytes,
      :min_size => 4.kilobytes
  end

HasImage also provides a view helper to make displaying the images extremely simple:

<%= image_tag_for(@photo, :thumb => :square) %>

Getting it

Has image can be installed as a gem, or as a Rails plugin. Gem installation is easiest, and recommended:

gem install has_image

and add

require ‘has_image’

to your environment.rb file.

Alternatively, you can install it as a Rails plugin:

./script plugin install git://github.com/norman/has_image.git

Rails versions before 2.1 do not support plugin installation using Git, so if you’re on 2.0 (or earlier), then please install the gem rather than the plugin.

Then, make sure the model has a column named “has_image_file.”

Git repository:

git://github.com/norman/has_image.git

Hacking it

Don’t like the way it makes images? Want to pipe the images through some crazy fast seam carving library written in OCaml, or watermark them with your corporate logo? Happiness is just a monkey-patch away:

  module HasImage
    class Processor
      def resize_image(size)
        # your new-and-improved thumbnailer code goes here.
      end
    end
  end

HasImage follows a philosophy of “skinny model, fat plugin.” This means that it tries to pollute your ActiveRecord model with as little functionality as possible, so that in a sense, the model is acts like a “controller” and the plugin like a “model” as regards the image handling functionality. This makes it easier to test, hack, and reuse, because the storage and processing functionality is largely independent of your model, and of Rails.

My goal for HasImage is to keep it very small. If you need a lot of functionality that’s not here, instead of patching this code, you will likely be better off using attachment_fu, which is much more powerful, but also more complex.

Bugs

Please report them on Lighthouse.

At the time of writing (July 2008), HasImage is in its infancy. Your patches, bug reports and withering criticism are more than welcome.

With the first release candidate of Rails 2.1 now out, I’ve tested friendly_id against it today. Yeah, I’m not really a big “edge” guy. In case anyone was worried, it appears to work fine. Let me know if you discover otherwise.