Saturday, October 10, 2009

Simple Navigation Plugin Version 2

About six months ago I wrote about the first release of the simple navigation plugin. It's a GemPlugin to generate the navigation for your ruby on rails applications.

As far I can judge from the feedback I got from the community it seems like a lot of people are using the plugin. So I'm really happy to announce version 2 of the simple navigation plugin.

The main new features are
  • create as many navigation levels you like (so far it was limited to primary and sub navigation
  • the active navigation item gets highlighted automatically (no more explicit configuration of the active item in the controllers needed)
Please check out the
and see some live examples on the demo page.

For providing feedback or get help visit the discussion group or drop me a line on github.

Tuesday, October 6, 2009

Making ActionMailer testable

If you ever create a custom subclass of ActionMailer you'll be having a hard time to test it. That's because - by default - you cannot access the ActionMailer instance after creating a mail. To make ActionMailer testable you have to make its new-method public:

    1 class ActionMailer::Localized < ActionMailer::Base
2
3 #to make ActionMailer testable
4 public_class_method :new
5
6 private
7
8 #we would like to test that the correct template is set
9 def initialize_defaults(method_name)
10 super
11 @template = "#{I18n.locale}_#{method_name}"
12 end
13
14 end

With that change it's easy to test the initialize_defaults-method (as shown here using rspec):

    1 describe ActionMailer::Localized do
2
3 before(:each) do
4 @localized_mailer = Class.new(ActionMailer::Localized) do
5 def mailer_method(mail_object); end
6 end.new
7 @localized_mailer.stub!(:render)
8 I18n.locale = 'de'
9 end
10
11 describe 'after being initialized' do
12 before(:each) do
13 @localized_mailer.create!('mailer_method', Object.new)
14 end
15 it "should have the localized template name set" do
16 @localized_mailer.template.should == 'de_mailer_method'
17 end
18 end
19
20 end

Localized ActionMailer Templates for Rails

Rails actually does support localized templates, however, this does not apply for localized ActionMailer templates (yet?). Even if you add the correct locale to your mailer template names, ActionMailer does not choose the one based on the current locale.

It's really easy to fix this:

Just create a subclass of ActionMailer::Base and override the initialize_defaults method as shown below. Then let your mailers inherit from your new subclass.

    1 class ActionMailer::Localized < ActionMailer::Base
2
3 private
4
5 # we override the template_path to render localized templates (since rails does not support that :-( )
6 # This thing is not testable since you cannot access the instance of a mailer...
7 def initialize_defaults(method_name)
8 super
9 @template = "#{I18n.locale}_#{method_name}"
10 end
11
12 end

Define your desired template name as shown on line 9, just do not add your locale as dot-notation at the end of the file. E.g. method_name.de.text.plain.erb will not work.

Thursday, May 14, 2009

Slow ruby 1.8 performance on MacBook Pro with Leopard

Recently I started to get really really bored about how sluggish my rspec-tests were running. The weird thing was: the specs run about 5 times faster on my iMac than on the MacBook. Same situation for loading the rails-environment (which is part of running the specs...).

After googling some time I found this blog entry by Barry Hess.

Conclusion: I followed the Hivelogic tutorial to install ruby. They recommend to compile ruby with certain options (e.g. --enable-pthread). This leeds to bad ruby performance on MacBooks (at least the model I have). If you compile ruby without any options, it runs much (and I mean much) faster. Loading the rails enviroment dropped from 5.5s to 1.5s. Specs also run about 5 times faster! If you're a test driven developer, you know what that means in terms of productivity.

I'm quite sure that a lot of rubyists have that problem, but are not aware of it.

Update: this article also applies for Snow Leopard.

Wednesday, April 1, 2009

Installing the mysql gem for ruby 1.9.1

I recently tried to install the mysql gem for ruby 1.9.1 on my macbook (OSX 10.5.6). I started with the 'usual' command to install the (modified) mysql gem:

$ sudo gem19 install kwatch-mysql-ruby --source=http://gems.github.com/ -- --with-mysql-config=mysql_config

The gem was compiled and installed correctly, but I got the following error when requiring 'mysql':

LoadError: dlopen(/usr/local/lib/ruby19/gems/1.9.1/gems/kwatch-mysql-ruby-2.8.1/lib/mysql.bundle, 9): Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /usr/local/lib/ruby19/gems/1.9.1/gems/kwatch-mysql-ruby-2.8.1/lib/mysql.bundle
Reason: image not found - /usr/local/lib/ruby19/gems/1.9.1/gems/kwatch-mysql-ruby-2.8.1/lib/mysql.bundle

The problem is that the mysql gem tries to load libmysqlclient.dylib from the wrong directory. The following command resolved the problem:

$ sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby19/gems/1.9.1/gems/kwatch-mysql-ruby-2.8.1/lib/mysql.bundle

the first param ist the incorrect path which is used by the mysql.bundle, the second param is the correct path to be used instead and the third param is the path to the mysql.bundle. Adapt the params to your needs.

Tuesday, March 31, 2009

Simple Navigation Plugin for Rails

Over the last two weeks, I've been working on the simple-navigation plugin which you can use to create the main navigation for your rails app.

In the last year, I built several apps and had to repeatedly implement similar code for creating a navigation. Thus, I thought it would be helpful to have a plugin which generates the code for the navigation and can easily be configured.

Please check out the online demo on simple-navigation-demo.andischacke.com or checkout the wiki on github.

I'd appreciate your feedback on any missing features or bugs, just go to the simple navigation discussion group or leave a comment here.

Update: This post is about an old version of the simple navigation plugin. Please see this post for the new version.

Update: The gem is now ruby 1.9.1 compliant (as of version 1.3.1)

Thursday, March 26, 2009

Select the first n lines of a file

On linux, to select the first 1000 lines of a file and write it to another file use

head -n1000 file_name > new_file_name

Remove the first line of a file with sed

If you need to remove the first line of a file use (on linux)

sed 1d file_name > new_file_name

Convert a file to UTF8

On linux, if you want to change the encoding of a file from ISO-8559-1 to UTF8 use

iconv -f ISO-8859-1 -t UTF8 file_name > new_file_name

To get a list of supported encodings use

iconv -l

Tuesday, March 24, 2009

Problem with rcov and ruby 1.8.7

I'm running ruby 1.8.7 and got the following error when running rcov (0.8.1.2.0):

usr/local/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `[]': no implicit conversion from nil to integer (TypeError)

To fix it change the following line in lib/rcov/report.rb of your rcov gem

if RUBY_VERSION == "1.8.6" && defined? REXML::Formatters::Transitive

into
if ["1.8.7", "1.8.6"].include?(RUBY_VERSION) && defined? REXML::Formatters::Transitive

With that modification rcov runs without problems again.