Location sensitive downcase in Ruby/Rails (unicode gem)

There is one pretty annoying gotcha in Rails UTF-8 support. As it turns out, downcase is local insensitive. So if you put “HELLO”.downcase you’ll get “hello” as a result. But if you put “ŠĐČHELLOŽŽ”.downcase you’ll get “ŠĐČhelloŽŽ”. Now this is not that I would call a principle of a least surprise. On the bright side it’s supposed to be fixed in Ruby 2.0. Until then there is an easy fix:

gem install unicode
#enviroment.rb
require 'unicode'
require 'lib/string'
#lib/string.rb
class String
  def downcase
    Unicode::downcase(self)
  end
end

Rake stats – adding a custom folder

Rake stats is useful when you need to know the statistics of your project. However if you try to add custom folder to be listed – documentation is somewhat sparse.

Question is why would you need a custom folder? After all isn’t the Rails prescribed way to go… Well not everything is a model or a a lib. You can have complex domain models in Ruby that have nothing to do with the database or the general library utility classes. Those poor classes just scream for the folder of their own.

If you add code to new folder named FancyNewFolder “rake stats” is not picking it up.
Stats before adding custom folder
Here is what I did.

gvim lib/tasks/AppTasks.rake

And then I’ve added

STATS_DIRECTORIES = [
  %w(Controllers        app/controllers),
  %w(Helpers            app/helpers),
  %w(Models             app/models),
  %w(Libraries          lib/),
  %w(FancyNewFolder     app/fancy_new_folder),
  %w(Integration\ tests test/integration),
  %w(Functional\ tests  test/functional),
  %w(Unit\ tests        test/unit)

].collect {|name, dir| [name, "#{RAILS_ROOT}/#{dir}"]}.select {|name, dir| File.directory?(dir)}

desc "Report code statistics (KLOCs, etc) from the application"
task :stats do
  require 'code_statistics'
  CodeStatistics.new(*STATS_DIRECTORIES).to_s
end


Voilà there is your custom folder, I’ll bet the stats are not so good as they used to be, but at least now you know the truth :)
Stats after adding custom folder

Redgreen plugin not working with Ruby 1.9.1

Redgreen in action
You have upgraded to Ruby 1.9.1 and now (after manually changing & compiling mongrel to work) you noticed that redgreen plugin is not working. Whenever you start test you get something similar to this:

`require': no such file to load -- test/unit/ui/console/testrunner (MissingSourceFile)

Just type this in:

sudo gem install test-unit -v 1.2.3

If you are one of those “problematic” people that want to know why is this working… truth is much simpler than I would like it to be considering that I lost 2 hours over this :) Ruby 1.9.1 replaced test-unit framework with minitest (because it’s supposed to be better). Redgreen plugin depends on console-runner from test-unit so you need to install it as u gem. (Older version, because the new 2.0.3 changed API just enough so redgreen is not working)

Using Ruby Metaprogramming to Increase READABILITY of Code

Let’s assume you have a class that has some sort of hash where it stores it’s data. We want to calculate amount that we need to pay, but only if number of items that we want to sell is entered. We’ll also assume that price is already present so we don’t need to check for that. It’s easy:

@store[:total] = @store[:price] * @store[:amount] if @store.has_key?(:amount)

A bit more readable would be:

set_total(price * amount) if amount_exists

(more…)

RAILS auto_complete With Additional FILTER

Auto complete with additional filter

The best tutorial about auto complete control by far was one from Ryan Bates. It worked like a charm, and he made it look so easy. Only problem I had with it was that I wanted another control on the form to filter the auto-complete control.
The tutorial didn’t cover that, so I got stranded in a strange place. The solution was pretty simple, but it took me a while to figure it out. If you are just beginning Rails like I am – this is rigth tutorial for you.

(more…)