lib/mir_extensions.rb
def sort_link(model, field, params, html_options={})
      if (field.to_sym == params[:by] || field == params[:by]) && params[:dir] == "ASC"
        classname = "arrow-asc"
        dir = "DESC"
      elsif (field.to_sym == params[:by] || field == params[:by])
        classname = "arrow-desc"
        dir = "ASC"
      else
        dir = "ASC"
      end
  
      options = {
        :anchor => html_options[:anchor] || nil,
        :by => field,
        :dir => dir,
        :query => params[:query],
        :show => params[:show]
      }
  
      options[:show] = params[:show] unless params[:show].blank? || params[:show] == 'all'
  
      html_options = {
        :class => "#{classname} #{html_options[:class]}",
        :style => "color: white; font-weight: #{params[:by] == field ? "bold" : "normal"}; #{html_options[:style]}",
        :title => "Sort by this field"
      }
  
      field_name = params[:labels] && params[:labels][field] ? params[:labels][field] : field.titleize
  
      _link = model.is_a?(Symbol) ? eval("#{model}_url(options)") : "/#{model}?#{options.to_params}"
      link_to(field_name, _link, html_options)
    end
def sort_link(model, field, params, html_options={})
      if (field.to_sym == params[:by] || field == params[:by]) && params[:dir] == "ASC"
        classname = "arrow-asc"
        dir = "DESC"
      elsif (field.to_sym == params[:by] || field == params[:by])
        classname = "arrow-desc"
        dir = "ASC"
      else
        dir = "ASC"
      end
  
      options = {
        :anchor => html_options[:anchor] || nil,
        :by => field,
        :dir => dir,
        :query => params[:query],
        :show => params[:show]
      }
  
      options[:show] = params[:show] unless params[:show].blank? || params[:show] == 'all'
  
      html_options = {
        :class => "#{classname} #{html_options[:class]}",
        :style => "color: white; font-weight: #{params[:by] == field ? "bold" : "normal"}; #{html_options[:style]}",
        :title => "Sort by this field"
      }
  
      field_name = params[:labels] && params[:labels][field] ? params[:labels][field] : field.titleize
  
      _link = model.is_a?(Symbol) ? eval("#{model}_url(options)") : "/#{model}?#{options.to_params}"
      link_to(field_name, _link, html_options)
    end
Returns a link_to tag with sorting parameters that can be used with ActiveRecord.order_by.

 To use standard resources, specify the resources as a plural symbol:
   sort_link(:users, 'email', params)

 To use resources aliased with :as (in routes.rb), specify the aliased route as a string.
   sort_link('users_admin', 'email', params)

 You can override the link's label by adding a labels hash to your params in the controller:
   params[:labels] = {'user_id' => 'User'}