Archive for the ‘Ruby on Rails’ Category

Use rails-footnotes to open files in NetBeans from Firefox

Friday, January 16th, 2009

Here is how to use rails-footnotes with NetBeans on linux!

Thanks to José Valim we have a nice new plugin for better debugging of Rails applications! In his blog post he describes how to open links to files on your hard disk with notepad++ on windows. I adapted this idea for opening files with netbeans on linux, and here is your way to go:

open about:config in firefox and add:

  • string network.protocol-handler.app.editor with content ~/.editor.rb
  • bool network.protocol-handler.external.editor and set it to true

create the file ~/.editor.rb and make it executable:

touch ~/.editor.rb && chmod +x  ~/.editor.rb

And here is the content:

#!/usr/bin/ruby
file = ARGV.first.split('file://').last.split('&').first
line = /\&line\=(\d+)/.match(ARGV.first)[1] rescue 0
`"/path/to/your/netbeans-dir/bin/netbeans" "#{file}:#{line}"`
`wmctrl -a "NetBeans IDE"`

install wmctrl

sudo aptitude install wmctrl

We’ll need this to give focus to the NetBeans window.

Use to_money to convert a string to an integer

Monday, November 3rd, 2008

Just a quick note: You can use the following code in your helpers/application_helper.rb to convert a string to an integer in your Rails application:

class String
  def to_money
    fee = self.match(/(\d+)(,|.?)(\d{0,2})/)
    if %w(, .).include?(fee[2]) # 56,99 1.00 5,-
      return "#{fee[1]}#{fee[3].to_i.to_s.ljust(2, '0')}"
    elsif fee[1] && fee[2].blank? # 5
      return "#{fee[1]}00"
    end
  end

  def to_money!
    self.replace to_money
  end
end

Now you can do some fancy stuff like the following:

"12 Euro".to_money => 1200
"12 Dollar".to_money => 1200
"12.00 Dollar".to_money => 1200
"12,00 Euro".to_money => 1200
"12,- Euro".to_money => 1200
"1200 Euro".to_money => 120000
money = "12,8 Euro"
money.to_money!
puts money # => 1280

You can read about the motivation why to use integer in favor of decimals in the database here: 7 Top Tips for Coding With Currency.

Tiny suggestion to mimetype_fu

Thursday, February 28th, 2008

I made a suggestion to mimetype_fu and send it to the autor:
Instead of calling file with -ir and scanning the result like the plugin actually does in /vendor/plugins/mimetype-fu/lib/mimetype_fu.rb you could pass the -b option like this:

# mime = `file -ir #{file.path}`.scan(/.*: (.*);(.*)/)[0][0]
mime = `file -bir #{file.path}`.strip

The man page of file says: -b Do not prepend filenames to output lines (brief mode)