header image

page3

Tar/Gzip Simple How-to

Source: http://www.gzip.org/#faq14

With GNU tar:

1 gtar cvzf file.tar.gz filenames

With tar:

1 tar cvf -  filenames | gzip > file.tar.gz

Decompression:

1 tar -xzvf file.tar.gz

Language: Shell/Scripting / Tags: compression / Posted by: spicycode / On: December 24th, 2006

Simple method to find age of person

1 def age
2   ((Time.now - birthday)/(60*60*24)/365.2422).to_i
3 end

Language: Ruby / Posted by: spicycode / On: December 23rd, 2006

Turn Deprecation Warnings Off in Rails

Source: http://ryandaigle.com/articles/2006/12/04/how-to-turn-deprecation-warnings-off-in-rails

To turn off all deprecation warnings, just do the following:

1 ActiveSupport::Deprecation.silenced = true

Language: Ruby / Tags: rails, logging / Posted by: spicycode / On: December 16th, 2006

Add back date, time, and logging level to logger

Put something like this at the end of your environment.rb.

 1 require 'logger'
 2 
 3 class Logger  
 4     alias format_message old_format_message
 5 
 6     def format_message(severity, timestamp, progname, msg)
 7       "#{timestamp.to_s(:log_format)} [#{severity}] - #{msg}\n"
 8     end
 9 end

Language: Ruby / Tags: rails, logging / Posted by: spicycode / On: December 13th, 2006

Rotating your Loggers in Rails

1 # Rotate the log at 10 megabyte, keeping the last 10
2 RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", 10, 10000000)

Language: Ruby / Tags: rails, logging / Posted by: spicycode / On: December 13th, 2006

Production Routing Issues? Customized code in lib? Reload young man, reload!

We recently experienced some challenging issues when pushing fully tested code to production. Wholesale functionality ceased to work. What to do? We had prior experience with an old mangy dog hangin' on to long. After killing that mongrel, all was well. This time, however, the solution was not easy found. After a bit of mucking around, I remembered that the model in question is not handle properly with the inflections built in with rails. This was cured with a bit of code in our project_init.rb in lib:

1 Inflector.inflections do |inflect|
2   # inflect.irregular('tax', 'taxes')
3   inflect.plural /^(tax)$/i, '\1\2es'
4   inflect.singular /^(tax)es/i, '\1'
5 end

This worked well in development, but the routes were failing in prod. Solution Reload!!

1 Inflector.inflections do |inflect|
2   # inflect.irregular('tax', 'taxes')
3   inflect.plural /^(tax)$/i, '\1\2es'
4   inflect.singular /^(tax)es/i, '\1'
5 end
6 
7 ActionController::Routing::Routes.reload

Reloading the routing after the new inflection rules are loaded is the key. ActionController::Routing::Routes.reload

Language: Ruby / Tags: rails, restful / Posted by: kbarker / On: November 21st, 2006

db_rollback.rake

Source: http://blog.labratz.net/articles/2006/11/17/quick-rollbacks-for-your-database

 1 namespace :db do
 2       namespace :rollback do
 3         rule "" do |t|
 4           back = t.name.gsub("db:rollback:", '').to_s
 5           back = "1" if back == "db:rollback"
 6           ENV['back'] = back
 7           Rake::Task["db:rollback:default"].invoke
 8         end
 9 
10         desc "Rollback the database schema"
11         task :default => :environment do
12           schema = ActiveRecord::Migrator.current_version.to_i
13           if ENV.include?("back") && ENV['back'].to_i <= schema
14             rollback = schema - ENV['back'].to_i
15           else
16             rollback = schema - 1
17           end
18           ActiveRecord::Migrator.migrate("db/migrate/", rollback)
19           puts "schema rolled back to verison #{rollback} from #{schema}"
20         end
21       end
22     end

Now you can do stuff like:

1 rake db:rollback:4

This will rollback the database 4 versions back. What if you want to move it forward though? Just pass a negative integer like so:

1 rake db:rollback:-3

Language: Ruby / Tags: rake / Posted by: spicycode / On: November 17th, 2006

schema_version.rake

Source: http://jayfields.blogspot.com/2006/11/rails-get-schemainfo-version-number.html

shame.rake

Source: http://codefluency.com/2006/10/23/rake-shame

If you’ve ever been working on a Rails application and found yourself [stupidly] slipping in your testing, I have just the tool for you. Using rake and rubyosa, you can now automatically post your Code to Test Ratio as your iChat status message—meaning you can now use shame and self-humiliation to keep you motivated. After you’ve installed rubyosa, add shame.rake to your lib/tasks directory. The file contains:

 1 require 'rbosa'
 2 require 'code_statistics'
 3 
 4 task :shame do
 5   stats = CodeStatistics.new(*STATS_DIRECTORIES)
 6   code  = stats.send :calculate_code
 7   tests = stats.send :calculate_tests
 8   ichat = OSA.app('ichat')
 9   msg = "Code To Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}" 
10   ichat.status_message = msg
11   $stderr.puts %|iChat status set to: #{msg.inspect}|
12 end

Language: Ruby / Tags: rake, subversion / Posted by: spicycode / On: November 15th, 2006