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…)

Tweak CalendarDateSelect plugin to work with European datetime format

Calendar date select in action

If you want to use calendar_date_select plug-in for date time picker, and you would like to use dd.mm.yyyy format read on!

I wrote this post after many wrong turns and many tutorials that just didn’t work. This informations applies for RAILS 2.3.2, and Calendar Date Select 1.15. I you are anything like me you want proof that my tutorial is working? (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…)

Ruby BLOCKS Explained 101

Blocks

Block is basically anonymous method that gets called from another method. If you have some code that executes passed in method like this:

def execute_method_in_block(named_method)
    named_method.call
    named_method.call
end

execute_method_in_block Proc.new {puts "Hello, blocks are cool!"}

You can write it like this using blocks:

def execute_method_in_block
    yield
    yield
end

execute_method_in_block  {puts "Hello, blocks are cool!"}

We’ve lost “Proc.new” and a method name, we are just saying

Whenever you find yield, execute block that is defined within braces.

You might be wondering what’s the fuss all about, (more…)

How to Install TabNav Plugin?

Tabnav in action

If you need tabbed navigation on your pages (well who doesn’t? :) ) please read on about cool plugin for Rails. It’s work of Italian guy named Paolo Dona. Documentation is a bit scarce, especially for me. But Paolo helped me himself to get it working.

(more…)