Intelligent Redirects: redirect_to_last_index

I often came across the same problem, the user starts say /users?order=email%20desc, chooses the user to edit and when he hits save/destroy he finds himself at /users/, very disturbing…. Another example comes from multiple controllers, when you start in /movies/3 and then, edit the user that created the movie, hit save and you resurface in /users/.

Fixing this redirecting issue was easier then i thought, and i refactored it to be a nice drop in and forget solution!

#app/application_controller.rb
...
  after_filter :save_last_index , :only => :index

protected
  def redirect_to_last_index
    return redirect_to session[:last_index] if session[:last_index]
    redirect_to :action => :index
  end

  def save_last_index
    session[:last_index]=request.request_uri
  end
...

#app/users_controller.rb
  def destroy
    @user = User.find(params[:id]).destroy
    redirect_to_last_index
  end

No more redirecting pain hurray 😀

Live example

Installing RCov by script

Code coverage analysis with RCov, next on my TODO :>
Another install script for your leisure…

OBSOLETE: gem install rcov works again!

wget http://rubyforge.org/frs/download.php/28270/rcov-0.8.1.2.tar.gz
tar -xf rcov-0.8.1.2.tar.gz
sudo ruby rcov-0.8.1.2/setup.rb
rm -rf rcov-0.8.1.2

svn cleanup
svn update
piston import http://svn.codahale.com/rails_rcov vendor/plugins/rails_rcov

Now you can get started with rake test:units:rcov

Unit test results

  • Show only selected parts: SHOW_ONLY=m,l,c,v //model,lib,controller,view
  • Run single tests: rcov FILE -exclude “/var/|/usr/” –rails (irgnore /var and /usr folder)
  • Add RCov parameters: RCOV_PARAMS=””
  • exclude RCOV_PARAM=”–exclude ‘var/*,gems/*'”
  • Sort by coverage: COV_PARAMS=”–sort=coverage”
  • Hide fully covered: RCOV_PARAMS=” –only-uncovered”
  • If you see something like (eval):580:in `attribute’: wrong number of arguments (2 for 0) (ArgumentError) it means one of your tests is uncoverable, this can happen while using SQS gem

Add the RCOV_PARAMS to spec/rcov.opts (or test/rcov.opts)

Clicking Links in UnitTests

Using click /change/i is a good way of testing you links/methods, since it will assure your views render the valid link and you do not have to repeat the link parameters as in get :edit, :id=>3

  1. get form_test_helper with
    script/plugin install http://form-test-helper.googlecode.com/svn/form_test_helper/
  2. patch vendor/plugins/form_test_helper/lib/form_test_helper.rb
    -  if self["onclick"] && self["onclick"] =~ /'_method'.*'value', '(\w+)'/
    -    $1.to_sym
    +  if self["onclick"] && self["onclick"] =~ /\.method = '(.*)'/
    +    $1.downcase.to_sym
  3. insert helper to test/test_helper.rb
      def click(text)
        select_link(text).click
      end
  4. click click click clickaround!
      def test_should_not_upload_from_ftp
        login_as :aaron
        assert_invalid_upload do
          `cp -f test/fixtures/videos/test.mov #{ftp_accounts(:aaron).dir}/wrong_name.flv`
    
          click /upload.*ftp/i
          assert_response :redirect
          assert flash[:alert]
        end
      end

Autoupdate jQuery with Rake

After installing jQuery on Rails (jRails) i noticed that the update task just kept overwriting my jquery.js with an older version. Effectively downgrading it…

So here is a real update task, execute it with rake update:jquery

  #lib/tasks/jquery.rake
  namespace :update do
    desc "Download recent jQuery javascripts to public/javascripts"
    task :jquery => :environment do
      puts "Downloading files..."
      files = {
        'jquery.js'=>'http://code.jquery.com/jquery-latest.min.js',
      }

      require 'open-uri'
      files.each do |local,remote|
        f = File.open "#{js_dir}/#{local}", 'w' do |f|
          f.write open(remote).read
        end
        puts "downloaded #{local} successfully."
      end
      puts "all files downloaded successfully."
    end

    def js_dir
      ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR
    end
  end

PS: If the bleeding edge breaks anything you can get your old version back by jrails:update:javascripts

The other jquery libaries(fx,ui) are missing atm, since i am just using jquery on its own. If you know an update site, please drop a comment.

HTML_TEST because RailsTidy drives me crazy

The functionality of RailsTidy is great, but it can drive you crazy, all the senceless warnings, wich as far as i looked cannot be turned off. Aditionally it cannot validate all requests, only the last one (if you followed my previouse post).

html_test features:

  • validating ALL requests
  • validating with up to 3 different validators (ok.. 1 is enought for me)
  • killing senceless warnings
  • checking all URLs (returnes :success or :redirect?)

Grab here: ruby script/plugin install http://htmltest.googlecode.com/svn/trunk/html_test

And put this into your test_helper.rb:

#--------html_test plugin
#validate every request
ApplicationController.validate_all = true
ApplicationController.validators = [:tidy]

#ignore common warnings
Html::Test::Validator.tidy_ignore_list = [
  /<table> lacks "summary" attribute/,
  /trimming empty <fieldset>/,#erros_on missing -> empty fieldset
  /line 1.*Warning: inserting missing 'title' element/,#redirect html has no title....
  /Warning: replacing invalid character code 130/, #€ has a very bad character
]

#check urls
ApplicationController.check_urls = true
ApplicationController.check_redirects = true

If you always see an extra line like 0 tests, 0 assertions, 0 failures, 0 errors, go to vendor/plugins/html_test/lib/html_test.rb and move the first 3 reuire lines into the if block