Rails on the Run

Rails experiments by Matt Aimonetti

Browsing Posts tagged plugin

I recently bugged Rick Olson so much about attachment_fu that he gave me SVN access to fix few bugs.

Rick being really busy with ActiveReload he didn’t spend too much time maintaining attachment_fu.

On my side of things, I’ve been using attachment_fu on a lot of projects and I’ve been fixing bugs and adding new features.

My first contribution to attachment_fu is a fix for the ImageScience processor.

Attachment_fu is very flexible and let you use your favorite image processor:

Like many rubyists, I like ImageScience for its simplicity and efficiency. However, attachment_fu had few problems when being used with ImageScience.

  • File sizes for thumbnails were not saved correctly in the database. Fixed
  • Thumbnails based on a gif files were not processed properly. So, this was the big problem. FreeImage has issues dealing with resizing gif files because of the gif palette limitation (256 colors). to avoid this problem thumbnails of gif files are converted to png. However the thumbnail content type info in the database was not saved properly. That’s now fixed.
  • Because of the gif bug reported above, any thumbnail link was broken since it was trying to link to the thumbnail version with a gif extension instead of a png one. Fixed

I also fixed a small bug related to S3 storage and the fact that a_fu had issues loading the config file. (Fixed)

I’ll also try be able to add some of the S3 features I’ve been working on. As well as maybe enhancing the validation process.

In the mean time, you might want to read this blog post about better validation with attachment_fu.

If you are heavily using attachment_fu or starting using it and think that a google group would be great idea, please let me know in the comment and I’ll try to convince Technoweenie that we need to set that up :)

Ooohh I almost forgot, if you fixed some bugs you found while using afu, please contact me so we can get afu bug free.

I just released a simple plugin that I use to backup and restore databases instead of using a mysql dump.

check out the plugin page

File structure will look like that:

ar-backup

I use this plugin to backup my apps running in production. When I need to debug an application, I just need to restore the db locally and debug. The fact that it uses fixtures helps me finding data quickly and I can easily decide to only restore few tables.

I also have a Capistrano recipe to create backups before each deployment.

oohh and the plugin is Sake compatible, give it a try :)

Enjoy

resftul_authentication is a great plugin, but not always resftful… especially if like me you dropped unit test for RSpec.

I have a set of RSpec examples I use all the time on all my projects requiring restful_authentication, obviously I always end up tweaking them because each application has its specific needs.

However, I was recently asked what’s the way of dealing with controllers specs when a controller has a limited access ?

Let’s look at the following controller for instance:

class AssetController < ApplicationController
 before_filter :login_required, :except => [ :index, :show ]

my assets_controller_spec.rb file will have the following code:

describe AssetController, "handling GET /assets/new" do

  before do
    @asset = mock_model(Asset)
    Asset.stub!(:new).and_return(@asset)
  end

  def do_get
    get :new
  end

  it "should be successful" do
    do_get
    response.should be_success
  end

end

and guess what… it will fail! Why? simply because we are trying to access an action requiring login. Right, so what’s the best way of dealing with this issue and get our test to pass?

The best solution I found was to use a simple helper that I put in my spec_helper.rb file

def login_as(user)
  case user
    when :admin
      @current_user = mock_model(User)
      User.should_receive(:find_by_id).any_number_of_times.and_return(@current_user)
      request.session[:user] = @current_user
    else
      request.session[:user] = nil
  end
end

You might wonder why I use when :user, well, in a lot of the application I’m working on, I have different levels of access and I want to tests different accounts so I the case conditional loop.

Anyway, let’s implement our helper in our previous RSpec example:

describe AssetController, "handling GET /assets/new" do

  before do
    login_as :admin
    @asset = mock_model(Asset)
    Asset.stub!(:new).and_return(@asset)
  end

  def do_get
    get :new
  end

  it "should be successful" do
    do_get
    response.should be_success
  end

end

and there you go, now your example passes properly ;)

Have fun