All posts in Rails

Rails vs Grails vs Django models

Coming back to Rails after being away from some time in Django land I discovered a huge difference in how Rails, Grails and Django treats your models. Read more…

Software architects as management deadwood

Two interesting quotes from Dietrich Kappe:

So no, we don’t hire architects. We hire developers. In a small team, there is no room for management deadwood.

I agree completely. My view is that the title “Software architect” is a misnomer for what most architects in the software industry do, or at least what they should be doing.

It is part of the weird trend that career advancement means getting away from actual programming for some reason. Maybe that is part of a bigger problem when the only way to get a higher pay is to become a manager of some sort? A couple of years ago, most programmers I knew aimed for a project management position. Programming was a dirty job that you had to put up with during the first years in consulting.

When my title was “business analyst” I tried to do as much programming I could and I haven’t regretted that for a moment. In fact, I believe that more people from the business side should get involved in programming to get a better understanding of the fundamental principles. For example, it would be great if business people could write their own acceptance tests and with the booming trend of DSL:s you will probably get involved anyway.

If you’ve made the transition from a hierarchical environment to an agile, self-organizing team, you know what I’m saying. You won’t ever want to go back.

Absolutely. It is the same thing as discovering things like Ruby/Python/Rails: it makes you wonder what the hell you were doing earlier. In many ways I feel sorry for young software developers that go straight into Rails or similar frameworks today. They are not as appreciative as the rest of us:-)

Will Rails ever run on IronRuby?

I met Ola Bini at the local Geeknight the other day and we had a brief chat about platforms, Ruby and RDF among other things. Ola mentioned that he wasn’t sure that Rails wuld run on IronRuby – Microsoft’s implementation of Ruby for the CLR.

I have been following what John Lam has been writing about their progress (and I correctly predicted him joining Microsoft:-) and it appears that running Rails is a goal of the IronRuby project. But, will that be of interest to Microsoft? MS recently launched the first version of an MVC framework for the ASP.NET platform. This seems like an attempt to satisfy the curiosity of .net developers that have seen screen casts and office mates develop apps in Ruby on Rails. The framework is part of the Visual Studio 2008 offerings.

If you were able to deploy a Rails app by dropping a DLL on a Windows web server I can see Rails popularity exploding. The .net platform seems to occupy a mid-level segment of application hosting operations, right where Rails development seem to be.

Only time will tell I guess.

Prism – web apps as desktop apps

When people started making applications available in the browser a number of interaction challenges appeared. How do you launch a web app compared to a desktop app? How do you prevent people from navigating away from your app? The Mozilla people have been hard t work with Prism – basically a customized version of Firefox, which lets you create desktop apps pre-configured to load a certain URL at startup. The desktop app is launched like any other application. Pretty sweet as the user experience becomes more consistent. You can also customize the application icon and other parameters. Read more…

When PHP makes sense

I have been looking into development frameworks for a web based software product. I want the product to be able to be installed on a variety of platforms, including Windows server with IIS. First I was looking at creating the app in ASP.NET and make it run under Mono. Unfortunately I can’t find an MVC framework for ASP.NET that works the way I want. Ruby on Rails has really lowered the threshold of what I can put up with in the form of configuration and learning curve. Damn you DHH and your rapid web framework:-)

Ruby on Rails runs happily on Linux and in Java environments thanks to JRuby. It does not work well with IIS yet (until IronRuby is here I guess). There is, however, interesting work being done in the RORIIS project (also see Dorje McKinnon’s Set up Rails on IIS blog post). The only problem I have is that I have heard a lot of reports where Rails under IIS isn’t working properly and that the RORIIS bundles several components that Windows server managers may find scary.

Django and Turbogears does not seem to work well under IIS either. For Django, the PyISAPI is required and apparently it hasen’t been actively developed for a while leading to bugs in the latest version of Django (you also have to run Python 2.4 instead of 2.5).

Turbogears seems to be close to work on IIS. Only problem is that it requires a reverse proxy filter that hasen’t been actively developed since 2005 which makes me wary about using it.

So, looking at Symfony, the Rails inspired PHP framework, I am beginning to wonder if that would be the best choice right now. The installation instructions for Windows/IIS seem straightforward. Symfony recommends the commercial ISAPIRewrite filter (lite version os free). For IIS7 it looks like Microsoft is stepping up to the plate with a decent Fast CGI module. Performance seems to be adequate too.

So, right now, Symfony/PHP seems like a decent choice for this app if I can live with the intricacies of PHP. Who would have thought…

Looking for ASP.NET MVC Frameworks…

I have been looking for an open source alternative to the default way of buildig web sites in ASP.NET with Visual Studio. After having build a couple of applications with Ruby on Rails it hard to go back to the Page Controller pattern that Microsoft introduced in ASP.NET. Coming back to the ASP.NET page event model makes it clear that they created it for VB6 application developers that were used to Windows forms-centered development. Apparently they didn’t want to those developers to have to learn about HTTP and HTML to be able to write applications. Read more…

Checking for Model Classes Before Using Them in Rails Migrations

If you are using model objects in migrations (e.g. for inserting data) you should make sure that the migration works even if that model class is removed. I discovered this when setting up a new development environment and running all migrations in an empty database.

Let’s say you have the following migration code:

class InsertCounties < ActiveRecord::Migration
def self.up
County.create :code => ’10′, :name => ‘Blekinge’
County.create :code => ’20′, :name => ‘Dalarna’
end
end

This assumes that the County model is available when the migration is run. If you checked out the most recent version of your code from svn it is possible that it doesn’t contain the County model and the migration will fail. To check if County is available before trying to use it we can use Ruby’s defined? like this:

Update: No we can’t. defined? always returns false inside an ActiveRecord migration for some reason (maybe the class isn’t loaded before the actual call?). We have to use a begin…rescue…end block instead:

class InsertCounties < ActiveRecord::Migration
def self.up
begin
County.create :code => ’10′, :name => ‘Blekinge’
County.create :code => ’20′, :name => ‘Dalarna’
rescue
puts “Could not add data…”
end
end
end

This way the rest of the migrations will run.

Bringing Ruby to the .NET environment

Things are heating up in the Ruby-as-a-dotnet-language area. Martin Fowler voiced his concerns on Microsoft not being able to look at source code and therefore having trouble implementing Ruby properly. Microsoft, with John Lam in the cockpit, is implementting Ruby for the .net platform (if you have been reading my previous blog posts I predicted way back in february 2006 that John Lam would get scooped up my Microsoft:-).

Ola Bini is also concerned about Microsoft not letting ther developers look at the Ruby implementation. If you remember the whole SCO debacle I guess it isn’t that strange. Microsoft is in the position where software they develop potentially may end up in millions of computers. Apparently the US legal system awards damages in proportion to this. Thus, any issues with a Ruby implementation on .net can quickly become costly.

It is all quite bizarre. Does this mean that the Microsoft version of the Ruby language is different from the “original” Ruby? I guess we will never know. Developers will probably write a lot of Ruby code that runs happily on the CLR. Rails applications will be deployed. But I am sure that there will be “special cases” where IronRuby will differ from “original” Ruby.

Therefore is was refreshing to see that Queensland University of Technology are progressing steadily with their Ruby.NET implementation. Currently you can actually compile a Ruby script into a .NET 2.0 assembly that other CLR languages can talk to. This may be the spearhead into the other half of enterprise deployment options.

All in all the future of software development looks bright. Will developers that invested a lot of time in Java or C# switch? Or will they move on to maintaining applications?

Working with UTF-8 in PDF::Writer and Ruby on Rails

Googling for information on how to use PDF::Writer shows that there are many european developers frustrated with the lack of UTF-8 support in PDF::Writer. As Ruby on Rails works great with UTF-8 these days this can be a bit of an issue.

Part of the problem lies in the fact that the PDF specification (at least up to 1.6) does not support UTF-8 (you can use UTF-16 if you like). I had the misfortune of plowing thorugh it a couple of years ago when developing a PDF form filler library for a customer (don’t ask).

In Ruby on Rails, this is easy to solve as long as you only use Latin characters with diacritics. The solution is to switch encoding back to ISO-8859-15 for text strings you feed to PDF::Writer.

A simple extension to the String class will do the trick:

class String
  require 'iconv'
  def to_iso
    c = Iconv.new('ISO-8859-15','UTF-8')
    c.iconv(self)
  end
end

If you are working in Rails you can put this code in the lib folder (I usually call the file string_extensions.rb).

Then, when you call the text method on your PDF::Writer intance you can easily pass a correctly encoded string.

Overriding PDF::Writer text method

A much cleaner approach, as AnĂ­bal describes in the comment below, is to override PDF::Writer’s text method.

Put the following code in a file called pdfwriter_extensions.rb (or whatever you choose to call it) in your lib directory:

CONVERTER = Iconv.new( 'ISO-8859-15//IGNORE//TRANSLIT', 'utf-8')

module PDF
	class Writer
		alias_method :old_text, :text

		def text(textto, options = {})
			old_text(CONVERTER.iconv(textto), options)
		end

	end
end

In your controller that handles the PDF output you add:

  require 'pdf/writer'
  require 'pdfwriter_extensions'

…after which you can use PDF::Writer like in the tutorial:

    pdf = PDF::Writer.new
    pdf.select_font "Helvetica", :encoding => nil 
    pdf.text "User name: <b>#{@user.name}</b>", :font_size => 16, :justification => :left
    send_data pdf.render, :disposition => 'inline', :filename => "user_details.pdf", :type => "application/pdf"

Enterprise Rails Deployment Getting Closer (thanks to Ola Bini and the JRuby team)…

Let’s forget about that for a while. Ola Bini and the JRuby team is quickly moving forward with something I would consider a breakthrough in Rails deployment options. In fact, it could well mean a breakthrough in Rails adoption in many organizations. Read more…

Why Accessibility in Rails is a Non-issue

Maybe a better title for this post would be “Why Accessibility in Rails is Currently a Non-issue”. Last night was the core team panel discussion at RailsConf and there was a question about what the core team was doing to increase accessibility in Rails.

Someone in the core team answered rather vaguely (or maybe I misunderstood the answer) how they had used Rails in a project that required high accessibility. My view is that for the Rails framework accessibility is currently a non-issue. Here is why:

As long as the developer has control over the view code (HTML and CSS) it will always be possible to create an application that is accessible. What happens if you remove that control? Microsoft tried to remove that control in the first incarnations of ASP.NET with the result that some really crappy (and inaccessible) HTML was genereated on the server.

Potential risks?

DHH presented an upcoming plugin: simply_helpful. This plugin will provide convenience methods to create markup in your views. Currently it provides an excellent support to do standard stuff. As long as this plugin doesn’t go haywire with regards to semantics Rails will continue to be an excellent framework for accessible applications.

Could something be made better in the current version of Rails? If scaffolding should be a way to show best practices in accessibility there is a small detail that could be changed right now. Add a lang attribute to the html element (lang=”en”) to indicate the natural language. But, I guess it is up to me and other accessibility advocates to submit as a patch.

Maybe it could be the smallest patch (9 bytes) ever submitted?

Deploying Rails in Amazon’s Elastic Compute Cloud

Here is a cool thing which I didn’t hear of until today. Amazon provides a service where you can upload a machine image (think VMWare), deploy it to as many instances you need and pay Amazon 10 cents for every hour you use it and 20 cents for each Gb data transfer. This will allow you to instantly scale an application if your demand temporarily increases.

With the risk of sounding like an Amazon marketing droid here are some highlights of what you get for those 10 cents:

  • Self-scaling: You can commission one, hundreds or even thousands of server instances simultaneously. This is all controlled with web service APIs, your application can automatically scale itself up and down depending on its needs.
  • Complete control of your instances. You have root access to each one, and you can interact with them as you would any machine. Each instance provides the equivalent of a system with a 1.7Ghz Xeon CPU, 1.75GB of RAM, 160GB of local disk, and 250Mb/s of network bandwidth.
  • Easily hook up to the S3 storage service.

The cool thing is that it seems relatively simple to deploy a machine. So, it would be easy to create a Linux image with your great Rails mashup application, deploy it and just switch on more instances as customers start arriving.

When I think about applications I have created earlier in my career I can see how nicely several of them would fit this business model. Who will be the first to report of a Rails deployment to the Elastic Cloud? Oh, someone already did it.

For more details see Amazon Elastic Compute Cloud (Amazon EC2) and how to set up a VM in the cloud.

Optimizing Rails template helper methods

Stefan Kaes is at it again with some interesting code to optimize Rails template helper methods. I have always wondered about some of the helper methods e.g. “< %= end_form_tag %>“. Why would you need that when it is easier to write “”?

Anyway, Stefan has shown that there is a cost to using many of the helper methods and that optimizing them may give you a performance increase. Methods involving routes are the worst performers.

By evaluating many of the methods only once (at template compile time) performance is increased. To see how your Rails application can be improved have a look at “Rails Template Optimizer Beta Test“.

Multiple annoyances with Rails (more than one security issue)

There have been a number of issues with Rails popping up yesterday and today. First there was the announcement that Rails 1.1.4 had a security issue. Discussions emerged on why the core team did not disclose the details of the vulnerability. Evan Weaver had a quick look at the diff between 1.1.4 and 1.1.5 and described the problem:

It looks like, for example, that if your Rails installation is in /www/rails/, passing a string such as /www/rails/../../tmp/ would pass the old validation, and if you had managed to upload a file such as hax_controller.rb to /tmp/, a route request to /hax/ would force Rails to run your arbitrary code.

Nice. I updated a sample app to 1.1.5 and though I was safe until I read this discussion over at Ruby Forum:

One more for 1.1.5:
Two subsequent calls:

http://127.0.0.1:3000/builder/blankslate

http://127.0.0.1:3000/active_support/dependencies

…put server to errors “SystemStackError (stack level too deep)”
constantly for all further requests.

Nice. I guess this is part of life for early adopters of new frameworks. Hopefully the community will be able to resolve these issues quickly.

Speaking at RailsConf Europe

I received an interesting email from the organizers of the european Rails conference yesterday. My talk proposal “Building Accessible Web Sites on Rails” was accepted and I will be giving a talk with the following description:

As an agile Rails developer you are expected to know a bit of everything in the MVC paradigm. The V (View) is typically considered the least attractive area as it is “only HTML” and is best taken care of “by someone else”. Accessibility often becomes a burden or is simply ignored.

In this session I will try to improve your skills with regards to accessible web interfaces. We will have a pragmatic look at why and how you increase accessibility. Without getting into too much theory we will have a look at some demographics and then move quickly into some scary examples of how you can fail miserably. I will show you how to use RAAKT (the Ruby Accessibility Analysis Kit) in your Rails testing framework (to make sure your team does the right thing) and how to fix some of the more commonly found accessibility issues. Thus, this session is for experienced Rails developers as well as beginners.

If you have any specific areas you would like me to cover, please create a comment below and I’ll try to fit it in.