Install RailsTidy Plugin by Script

I try to keep some script to do everything, just in case i get to lazy to do it again by hand, which is … always ๐Ÿ™‚

This will install RailsTidy from your Rails directory:
(Ubuntu 7.10) change the paths according to your system

sudo apt-get install tidy
sudo gem install tidy

wget http://www.cosinux.org/~dam/projects/rails-tidy/rails_tidy-0.2/tidy.patch
sudo patch /var/lib/gems/1.8/gems/tidy-1.1.2/lib/tidy/tidybuf.rb < tidy.patch
rm tidy.patch

#script/plugin/install fails with strange warnings and does not install anything...
cd vendor/plugins/
wget http://www.cosinux.org/~dam/projects/rails-tidy/rails_tidy-0.2.tar.bz2
tar -xf rails_tidy-0.2.tar.bz2
rm rails_tidy-0.2.tar.bz2
 
 
 

In case you see something like “tidybuf.rb:39: [BUG] Segmentation fault” downgrade to the last stable version:
apt-get install libtidy-0.99.0/20051018-1 (Ubuntu 8.0.4)

now, inside test_helper.rb you can put this:

class Test::Unit::TestCase
  def teardown
    assert_tidy if @request
  end
end

happy nagging ๐Ÿ™‚

PS:
If this is still not enought validating fun for you, you can go a step further with html_test (requires RailsTidy) a project that combines html validation with url checking(do all my links return :success or :redirect?)(Project).

js_tag and css_tag

Since quiet some time i fail to remember how to include javascript and css.

stylesheet_include_tag and javascript_link_tag or
javascript_tag and stylesheet_tag ???

Since im so pragmatig (read: forgetful+lazy) i came up with a simple solution:

#application_helper.rb
module ActionView::Helpers::AssetTagHelper
  alias js_tag javascript_include_tag
  alias css_tag stylesheet_link_tag
end

now it starts looking nice and clean ๐Ÿ™‚

    <%= js_tag 'ufo' %>
    <%= js_tag 'jquery.min.js' %>

Validate All Models

UPDATE: Please have a look at valid_attributes

Problem: New validations vs. old data

Solution:

  • find all models
  • validate all
  • print out those that are not valid

code-base + idea

#task: rake db:validate_models
namespace :db do
  def all_models
    #get all active record classes
    Dir.glob(RAILS_ROOT + '/app/models/**/*.rb').each do |file|
      begin
        require file unless file =~ /observer/
      rescue
        #require any possible file dependencies
        if $!.to_s =~ /^uninitialized constant (\w+)$/
          require $1.underscore + '.rb'
          retry
        else
          raise
        end
      end
    end
    klasses = Object.subclasses_of(ActiveRecord::Base)
    #throw out session if it is not stored in db
    klasses.reject! do |klass|
      klass == CGI::Session::ActiveRecordStore::Session && ActionController::Base.session_store.to_s !~ /ActiveRecordStore/
    end
    klasses.select{ |c| c.base_class == c}.sort_by(&:name)
  end

  desc "Run model validations on all model records in database"
  task :validate_models => :environment do
    #validate them
    puts "-- records - model --"
    all_models.each do |klass|
      begin
        total = klass.count
      rescue
        #tableless, session...
        if $!.to_s =~ /Table .* doesn't exist/im
          puts "No Table for: #{klass}"
          next
        end
        raise
      end
      printf "%10dx %s\n", total, klass.name
      chunk_size = 1000
      (total / chunk_size + 1).times do |i|
        chunk = klass.find(:all, :offset => (i * chunk_size), :limit => chunk_size)
        chunk.reject(&:valid?).each do |record|
          puts "#{record.class}: id=#{record.id}"
          p record.errors.full_messages
          puts
        end rescue nil
      end
    end
  end
end