file_column image regeneration

August 11th, 2008

If you use file_column for some projects and find that you want to change the thumbnail sizes that you’ve been using for models, then hopefully this little bit of code can help.

Add this as a rake task and run it as filecolumn:regenerate[Model, field] RAILS_ENV=production where Model is the model you want to regenerate images for and field is the filecolumn field on the model. The task uses Rake arguments, so you’ll need Rake 0.8.

namespace :filecolumn do
  desc "Regenerate filecolumn images"
  task :regenerate, :model, :field, :needs => :environment do |task,args|
    if args.any?{|k,v| v.blank?}
      puts "Usage: #{task.name}[Model,field]"
    else
      klass, field = args.model.constantize, args.field

      klass.all.each do |obj|
        puts "Regenerating #{klass} #{obj.id}"
        next if obj.send(field).nil?
        state = obj.send("#{field}_state")
        state.instance_variable_set(:@just_uploaded, true)
        state.transform_with_magick
      end
    end
  end
end
The code essentially boils down to 2 lines…
  • setting @just_uploaded to true on the state object
  • calling the transform_with_magick method on the state object

Having a process that automatically monitors your system and restarts applications when things get out of wack is pretty important to have. The 2 popular free options are God and Monit. This article is about not only setting up God to monitor your mongrels, but setting up God only ONCE to do this. If you add another mongrel cluster to your system, all you have to do is restart God.

Read the rest of this entry

Looking for Mephisto 0.8?

March 26th, 2008

I was too, so I popped into the #mephisto channel on freenode and technoweenie set me straight. The subversion trunk in mephisto will be the latest stable release (in this case 0.8) of his github repo.

But for an easy way to just download the 0.8 release, go here and then click on download and you’ll get a nice shiny tarball.

Thanks to the Mepisto team for making a great blogging app!

Updated for will_paginate 2.3.x

So you’re using the will_paginate plugin and want to use remote AJAX links. You’re not using the will_paginate plugin? Simply install it with…

script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

will_paginate is an alternative to the classic_pagination plugin, which is the pagination plugin that basically took the pre Rails 2.0 pagination implementation and packaged it in plugin form. I won’t discuss the differences between the two plugins here, but there’s a good video explaining the two from Railscasts here.

Out of the box, will_paginate doesn’t allow remote AJAX links for page links, so I’ll show you an easy way to add such functionality…

Read the rest of this entry

Do you use RAILS_ENV in your Rails app to execute different code depending on the environment you’re running?

if RAILS_ENV == 'development'
  log_some_data_or_do_something_different
else
  go_with_the_flow
end

No? Ok, great. Yes? Ok, not so great.

Those from both camps are likely to get something out of this post, so read on.

Read the rest of this entry

This is my second tutorial covering the plugins that we’ve developed.

This tutorial covers the human attribute override plugin.

The plugin allows humanized versions of attributes to be overridden with custom strings to provide a better conversion than human_name may provide.

Why?

The main reason for creating this plugin is that Rails doesn’t always provide an acceptable “humanized” version of an attribute name.

Column.new('num_employees', ...).human_name # => 'Num employees'

You could argue that perhaps the attribute should be named number_of_employees..

Column.new('number_of_employees', ...).human_name # => 'Number of employees'
But this is hardly an acceptable solution for various reasons
  1. The attribute name is too wordy.
  2. Changing an attribute name for the sake of it’s humanized version reading better results in unneeded refactoring.
  3. You’re using a legacy database that uses a specific naming convention.
Read the rest of this entry

Here’s a quick tip for anybody using a database view as an ActiveRecord model…

If you want to use your view as a model and take advantage of the association methods properly (like belongs_to, etc.), you must make sure that your view specifies a unique column that can be used as the primary key. Without a primary key for the view, you’ll likely run into some nasty bugs down the road (mine occured when I tried to use eager loading via :include with the “view model”).

First off, to use the view as a model, you don’t need to do anything different than what you’d do for a regular table.

class ViewModel < ActiveRecord::Base
end

Here are some ways to set your primary key for your view model:

1) If your view already has a unique “id” field, then you’re done. No need to do anything else.

2) If your view has a unique field, but it’s not called “id”, simply use the set_primary_key method.
class ViewModel < ActiveRecord::Base
  set_primary_key :unique_id # unique_id will be the name of a unique column used in the view
end

3) Create a new field in the view that can be used as the primary key.

create view view_model as select (field1_id::varchar + '-' + field2_id::varchar) as id, ...

The above SQL example uses two fields combined into one to form a unique field called “id” and this will be picked up automatically by ActiveRecord. The reason for adding the ’-’ seperator is so that 2 combonations of 2 seperate values don’t combine to form the same value.

With no seperator: field1_id = 10, field2_id = 10, id = 1010 and field1_id = 101, field2_id = 0, id = 1010

versus

With a seperator: field1_id = 10, field2_id = 10, id = 10-10 and field1_id = 101, field2_id = 0, id = 101-0

To those who know their databases, you’ll recognize this as basically a composite key and since RoR doesn’t support composite keys out of the box, this can be used to mimic one.

4) Use a composite key plugin like http://compositekeys.rubyforge.org to add composite key support to RoR

Now you can use composite keys instead of the method of generating one as in item 3.

class ViewModel < ActiveRecord::Base
  set_primary_keys :field1_id, :field2_id
end

note Pretty much everything here applies to a regular model based off a table, but when creating a view, it’s more common to not include a column that will be unique for each record like the “id” field used in almost all tables used with RoR.

Conditional Cache Plugin Tutorial

November 26th, 2007

Updated for Rails 2.1

This is my first of many tutorials to come covering the plugins that we’ve developed.

This tutorial covers the conditional cache plugin, which is actually the first plugin released by Redline.

Why?

By default, the caches_action method allows you to specify one or more actions to apply caching to.

class TestController < ApplicationController
  caches_action :index, :new

  def index
    ...
  end

  def new
    ...
  end
end

In the above example, action caching is being applied to the index and new action. Beyond that we have no additional control over when these actions should be cached or how these actions should be cached. It’s either all or nothing.

So to fix this “all or nothing” problem, the conditional cache plugin adds additonal functionality to the caches_action method by adding 2 additional parameter options that can be passed to the method along with the existing list of actions.

Read the rest of this entry

Selection javascript lib

October 26th, 2007

I’ve created a basic first draft for a javascript selection library. The library allows the user to select many elements on a web page with a simple mouse drag.

I call this version 0.1 as it has only been tested in Firefox and has some minor issues that need some cleaning up, but I figured I might as well make it public so people can take a look, try it, give feedback, or send patches.

You can grab the files and see a demo of it here

The library requires prototype and builder… both of which come with Rails.

Patches and feedback are welcome.

Conditional Cache updates

October 25th, 2007

I’ve recently fixed a bug in our conditional cache plugin that I introduced while making it compatible with Rails 1.2.4.

The bug has been fixed and the plugin also works with Rails 1.2.5.

The bug was causing the wrong cache paths to be created, so if you updated the plugin, update again ASAP.

The conditional cache plugin has been updated to be compatible with Rails 1.2.4. If you’re still using a previous version of Rails, there is no need to upgrade the plugin.

Update: Compatible with Rails 1.2.5 now

New in_place_editor plugin

July 23rd, 2007

I have just released a new plugin that can be found at http://agilewebdevelopment.com/plugins/improved_in_place_editor

This plugin allows all options of the Ajax.InPlaceEditor control to be set. This will also work with the new rewrite of the control, which is currently in the scriptaculous trunk.

This plugin replaces the current rails version of in_place_editor and is drop-in compatible.

Quick summary (more details at the url above)...

Missing options such as onFailure can now be specified…

<%= in_place_editor 'field_id', :on_failure => "function(transport) {alert(\"Error: \" + transport.responseText.stripTags());}" %>

Some options require quoting to be valid in the JS, such as :highlight_color.[1]

1 :highlight_color is the option name for the rewritten version of the control, :highlightcolor is the old name.

The 2 ways to specify the option are…

1) Manually quote the option
<%= in_place_editor 'field_id', :highlight_color => "’#000000’" %>
2) Use the :quoted option
<%= in_place_editor 'field_id', :quoted => {:highlight_color => ’#000000’} %>

Check out our other available plugins at http://agilewebdevelopment.com/plugins/owner/72

I recently had to deploy a Rails application for a client to a Joyent Accelerator. Paul Ingles has released a Capistrano recipe which helps automate this. I recommend using it.

But even with the recipe, I ran into a couple of snags. The first was with the the myprivateip script that Joyent tells you to use, and the second was Solaris’ ln.

myprivateip

There is a script /opt/csw/bin/myprivateip which is referenced in the mongrel_cluster.yml file they make available. I’d link to the page, but for some reason their how-to pages are all broken today.

Well that script didn’t return anything. It’s supposed to return an ip address.

I changed the script to:


  #!/bin/sh
  /usr/sbin/ifconfig -a | ggrep -A1 lo0 | grep inet | awk '{print $2}'

Which is just a fancy way to get 127.0.0.1. The mypublicip script seemed to be ok. After fixing that, you’ll want to rerun the cap setup task.

ln

Well I’m not the only one with this problem. Jamie’s blog post helped me get past a problem I was having with Capistrano not updating the symbolic link for the current release. Turns out you’ll want to use the GNU version of ln. On the box I was on though, GNU ln was gln and not ln like in Jamie’s setup, so I took a slightly different approach in order to fix the problem. I created a bin folder in my user’s directory and then put a link to gln like so:


  ln -s /opt/csw/bin/gln ~/bin/ln

Next, I put my ~/bin a the front of my path. To do so, I edited .bashrc and .bash_profile (since they both had PATH environment stuff set), but that didn’t solve my problem. There was also an environment file inside the ~/.ssh folder, which gets loaded when you run stuff via ssh, which is what Capistrano does of course. So I added ~/bin to the PATH in that file so it looks something like:

PATH=~/bin:/usr/sbin:/usr/bin:<chopped the rest out>

Some other people have replaced a few Capistrano tasks to use gln instead of ln, but that seems sorta nutty to me.

Now Capistrano is cookin with gas.

Rails TimeZone's

June 17th, 2007

I've just added TimeZone support to our Redzone Leagues application and have read a few blog posts about setting up TimeZone's in rails. One popular article on the subject is found here.

One issue that I came across while I was adding TimeZone support to our app, is that I have time fields in many places in the database that have been created with the local time zone, not UTC, which most articles on the subject seem to assume are already in the database. All of the timestamp fields in my database have no time zone support, which the rails migrations create by default (at least using Postgresql, not sure about MySql, etc.)

Read the rest of this entry

We just posted a Case Study about Redzone Leagues on RailsForAll. I’ve also copied it into this blog post so you can read it on their site or just read it here.

Read the rest of this entry