Posts Tagged ‘tool’

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.

It’s the little things I like about Linux

Tuesday, January 8th, 2008

Today I wrote some handy syncronization-scripts with the help of my freebsd-loving co-worker Marius. Till today I managed the syncronization of my local database with our production database with the help of phpMyAdmin.
An export of the 13mb sql file at the production server and an import at my machine took me at least five minutes (~2mb gzipped). Now take a look at this:

[hans@zommuntu:~]$ time ssh user@production_server "mysqldump --add-drop-table database_to_process -uUSER -pPASSWORD | gzip" | gunzip | mysql -uLOCALUSER -pLOCALPASSWORD database_name
real    0m4.389s
user    0m0.592s
sys     0m0.052s

Under five seconds! woot!!
Some words to explain this: First, you establish a ssh connection to the production_server. The gzipped dumped sql of the beloved database_to_process is transferred to your machine. Here it is gunzipped and written to your local database server into the database_name. The quotes are necessary as brackets to let the script know that the gzip command belongs to the ssh command.