Dumping your data into fixtures

The following code will dump your table data into fixtures that you can use for your tests. This is useful if the database needs to contain a known set of data before your tests are run.

This is modified heavily from this DZone snippet: http://snippets.dzone.com/posts/show/4468

namespace :db do
  namespace :fixtures do
    desc 'Dumps all models into fixtures.'
    task :dump => :environment do
      (ActiveRecord::Base.connection.tables - ['schema_migrations']).each do |table|
        puts "Dumping #{table}"
        rows = ActiveRecord::Base.connection.select_all("SELECT * FROM #{ActiveRecord::Base.connection.quote_table_name(table)} ORDER BY id ASC")
        out = {}
        rows.each_with_index { |mi, i| out["#{table.singularize}_#{i + 1}"] = mi }
      
        model_file = RAILS_ROOT + "/spec/fixtures/#{table}.yml"
        
        File.exists?(model_file) ? File.delete(model_file) : nil
        File.open(model_file, 'w') {|f| f << YAML.dump(out).gsub("<%", "<%%") }
      end
    end
  end
end

03:50 Mon 2009-06-22 (Index)

Removing validations from a class in ActiveRecord

Ever wanted to remove validations from an ActiveRecord object, after you’ve defined them? For example, you’ve defined validations on a Member object, and you have an STI subclass called SpecialMembers. SpecialMembers are special and don’t have as many validations applied to them, so you want to ignore some of the validations placed by the super class.

Well. look no further. Paste the code below into lib/validations_extra.rb and require 'validations_extra' in environment.rb. You can then remove all the existing validations with a given name by putting remove_existing_validations(:<validation_name>) in your subclass definition; for example remove_existing_validations(:validates_presence_of). There’s not a more specific way to remove validations because ActiveRecord doesn’t provide one. You can remove all existing validations with remove_all_existing_validations. Enjoy!

This code works with Rails 2.0 and should work with later versions.

module ActiveRecord
  module Validations
    module ClassMethods
      def remove_existing_validations(key = :validate)
        write_inheritable_attribute(key, (read_inheritable_attribute(key) || []).select { |m| m.is_a?(Symbol) and m.to_s =~ /^validate_associated_records/ })
      end
      def remove_all_existing_validations
        [:validate, :validate_on_create, :validate_on_update].each { |m| remove_existing_validations(m) }
        define_method(:validate) {}
      end
    end
  end
end

07:48 Thu 2009-06-18 (Index)

Load articles in Wikipedia and Uncyclopedia side-by-side

Uncyclopedia is a parody/satire/humor encyclopedia, offering funny versions of encyclopedia articles.

Its fun to compare the serious version (Wikipedia) and the funny version (Uncyclopedia) of an encylopedia side-by-side. MultiWiki makes it easy: just type in the wrticle you want to look up and it will show the article in Wikipedia and Uncyclopedia side-by-side.

12:12 Wed 2009-05-27 (Index)

Make Wikipedia articles more interesting

Paste the following code in the address bar after you’ve loaded a Wikipedia page and have some fun – I suggest replacing the name of the article with ‘lollipops’ as a starting point.

You can drag this link in the bookmarks bar to use this script again and again or just paste the code below into the address bar to try it out:

Replace page text

javascript:var a=prompt("Replace what:");var b=prompt("With what:");document.body.innerHTML=document.body.innerHTML.replace(RegExp(a, "ig"), b);void(0);

16:06 Sat 2009-05-23 (Index)

webstats: because top is SO 1985

Want to see the CPU/Memory/Disk usage stats on your server without logging in and having to squint at top’s output?

WANT NO MORE: http://github.com/bloopletech/webstats/tree/master

webstats is a little ruby program that displays your server’s vital statistics on a web page that updates itself via AJAX.

Check it out now, works only on Linux 2.6+ kernel.

12:49 Wed 2009-04-08 (Index)

Older posts