HTML to PDF Conversion Plugin For Rails (A fork of wicked pdf)

Once a business web application reaches a certain size, the need often arises to generate PDFs from HTML/CSS.

Up until recently, the story around this for a MRI Rails application was not good. You could either use tools like Prawn, which require a description of the layout in a specific DSL, or pay for a tool like Prince XML which can convert from HTML, but which costs quite a bit. Those using JRuby were in a stronger position as they could use the Java PDF library called Flying Saucer.

The good news is that PDF generation for MRI Ruby is now easy and free, thanks to webkit, the open source webkit wrapper called wkhtmltopdf and mileszs's wickedpdf plugin. I was really excited to come across this plugin and started to use it right away. However, it had a couple of issues:

  • Temp file handling caused errors when two PDFs were being generated within the same second (eg, 2 requests at almost the same time)
  • Problems generating PDF were not reported

Galdomedia forked the code and updated it to use standard Ruby temp files. This was great for ruby 1.7, but not good for Ruby 1.6 which does not allow you to set the extension on temp files (wkhtmltopdf relies on having a .html extension).

As my production servers run Ruby 1.6, I needed a different approach. My fork uses streams rather than temporary files, and adds some basic error handling and basic integration tests.

To install in a rails app:

script/plugin install git://github.com/jcrisp/wicked_pdf.git

Or clone the code from GitHub.

12 11 2009

“Ruby for Rails” by David Black

Ruby For RailsRuby for Rails by David Black is a fun read that takes concentration but repays it with little epiphanies that explain syntax and language features that you had previously taken for granted.

The book aims to "help Rails developers achieve Ruby mastery". The coverage of Ruby features is not complete and there are some concepts missed that I would have liked to have read more about (eg, how do instance variables work under the hood?). There are also a number of introductory chapters on Ruby and Rails and some chapters devoted to a sample Rails project (R4RMusic) which I flicked through but didn't add much value for me (they are also a little dated). By far, the most interesting parts of the book for me were on the Ruby type system, 'self' in various situations and how method look up works with modules and inheritance.

An area of Ruby that I had not previously explored was adding singleton methods to instances (like what you can do in Javascript). Eg,

o = Object.new
def o.say_hi
  p "hi"
end

>> o.say_hi
"hi"

or alternatively

o = Object.new
class << o
  def say_hi
    p "hi"
  end
end

Now, the interesting thing is that this is the basis for the whole class system in Ruby!

Classes are just a special type of object, and when you add class methods, you are really adding singleton instance methods to the class object for the type.

Ie, when you do something like:

class Cars
  def self.find_all
    ...
  end

You are actually creating a new object, of type Class which has a singleton method called 'find_all'. 'self' in the code above is the Class object, so def self.xxx is adding a singleton method to it.

This also explains the alternative syntax for adding class methods:

class Cars
  class << self
    def find_all 
      ... 
    end
  end

The same thing could be done by saying:

Cars = Class.new
Cars.instance_eval { def find_all; ... end; }

In Ruby, the type and class system is not very different from the normal objects you work with every day. I find this really quite cute and internally consistent.

The way the method search path works in ruby was also nicely explained in the book. Basically, finding a method starts at the top of the list below and stops as soon as a method with a matching name is found (ie, that responds to the message sent to the object):

  • Singleton methods on the object
  • Methods defined by the object's class
  • Methods defined by modules mixed in to the class
  • Methods defined by parent class
  • Methods defined by modules mixed into parent class
  • Repeat checking parents until get to Object
  • Methods defined on Object
  • Methods defined on Kernel (module mixed into Object)

This also explains why you can always call methods like 'p' from anywhere. They are coming from Kernel which is mixed in at the top of the inheritance tree for your object. Another case of internal consistency - there's no 'special' mechanism for these seeming globals.

Overall, I enjoyed the book and would recommend anyone having a read who has worked with Ruby and Rails but would like to dig a bit deeper.

10 08 2009

Mephisto Contact Form Plugin Moved to GitHub

Sorry the the confusion, anyone who has been checking out the the Mephisto Contact Form Plugin from the old SVN repository. The latest version with an update for Rails 2.3 is at:

http://github.com/jcrisp/mephisto_contact_form/tree/master

10 08 2009

Spying on Instance Variables in Ruby

A little while back, a few colleagues and I were spiking a proxy concept based on extending an existing web server. We wanted to check out an instance variable (eg, @very_secret) in a framework object which did not have an accessor. In the past, we'd used send (eg, secretive_object.send :hello_private) to get at privates, but send is only for methods. We were just digging around doing some debugging, so we opened the relevant class and added a public accessor for the instance variable to see what was happening. However, we thought there must be a more elegant way to do access instance variables outside the class, and one has just come to mind (at last!):

secretive_object.instance_eval { @very_secret }

instance_eval lets us run the code block in the context of secretive_object. Ie, self == secretive_object, so we can get at all the hidden stuff.

It's a rather different approach to other languages like C# and Java where accessing private variables and private methods are part of a reflection/introspection API.

5 08 2009

Review: “Deploying Rails Applications” by Ezra Zygmuntowicz et al.

Deploying Rails Applications: A Step-by-Step Guide by Ezra Zygmuntowicz, Bruce Tate and Clinton Begin is a good read, if a little dated. It was published in May 2008, and you can see that things have moved on a little in the Rails world since then. None the less, quite a lot of the information is still relevant and useful.

The book covers some basic Rails and version control concerns at the start, then rapidly launches into chapters devoted to Rails hosting options available from shared hosts to virtual and dedicated servers. The advice given is good and is in line with my experiences. Unix configuration is given in depth which would be very handy if you had not set up a server before. Next is a good discussion of Capistrano and automating deployments. The examples all use subversion. However, these days I expect the majority of Rails source code is pulled with Git. There is also a chapter on managing mongrels and setting up monitoring solutions. This is still relevant if you want to use mongrels, however these days Passenger is probably the best choice, and it does not have such complex management and configuration requirements. The scaling out chapter is useful and pulls together handy information including details on MySql replication/clustering. There's a chapter on deploying on Windows and also some suggestions around performance and profiling.

I haven't come across another book that brings together a structured collection of useful information to help you move from running rails locally to having a cluster of scalable production servers and the automated deployment process required to support it. Despite being too old to cover Git and Passenger, I'd still recommend having a read of this book if you're at the stage of planning to launch a Rails site or looking to scale your VPS up to a cluster.

2 08 2009

Linux VirtualBox vs Windows for Rails Dev

So yes, everyone has heard that Mac OS X and TextMate is the epitome of Rails development, and that it is so awesome that it brings tears of joy to developers eyes, &c. However, for those of us who either don’t have a Mac, or get to work on client provided hardware (often running Windows), there are a few options available.

Developing on Windows XP, with InstantRails is workable. It is easy to get everything you need and have your apps up and running quickly. However, performance is, well, quite frankly, terrible for anything you do on the command line. Mainly, this bites when running tests, doing migrations, generating files etc. Performance running Mongrel is good enough for development.

What about Cygwin? Subjectively, I found it provided similar speed to Windows Ruby/Rails.

So this brings us to virtualisation. Recently, I've been testing out VirtualBox running Ubuntu on top of Windows XP. This has had surprisingly good results. On the same machine, the virtual Ubuntu running Rails tasks has about 4 times (!!!) faster performance, even though it has less memory and system resources!

Here are some stats to give you and idea of the advantage.

Machine is a 2.4ghz quad core, 4gig of memory running Windows XP. Using VirtualBox 2.1.4 for virtualized Ubuntu Intrepid 64 bit, with 1.5gig of memory allocated. Figures are in seconds and approximate (taken with a wrist watch).

  generate scaffold db:migrate with no changes run tests for medium sized rails app
Windows XP & Instant Rails 7 7 25
VirtualBox Ubuntu on same Windows XP 1.5 1.5 6

Some of these commands / tests rely on hitting a MySQL database. However, I’m interested in overall development speed for both platforms, not in Ruby speed in particular, so I think it is fair game to include these in the results.

So if you want to do Rails Dev on Windows, I highly recommend trying a virtual machine running Linux!

21 02 2009

Vim with find file for Rails (like TextMate)

In a fit of TextMate jealousy, several months ago, I scoured the web for a way to get find-file functionality info my favourite Ruby/Rails editor, vim. I was very happy to find that Jamis Buck had developed an aweseome plugin do to this. It is a little fiddly to install, but worth the trouble. Here's some simplified steps to get you going.

  • Install Jamis's ruby gem
    sudo gem install jamis-fuzzy_file_finder --source=http://gems.github.com
  • Download this fuzzyfinder script and pop it in your '~/.vim/plugin' directory. Note that the most recent versions of this script are incompatible with Jamis's plugin.
  • Grab the latest version of 'fuzzyfinder_textmate.vim' from http://github.com/jamis/fuzzyfinder_textmate/tree/master and pop it in your '~/.vim/plugin' directory

Then, if you want to have a similar sort of light-weight Rails 'IDE' I enjoy coding with, see my config files below:

.vimrc

source $VIMRUNTIME/vimrc_example.vim
behave xterm
set nu
set tabstop=2
set shiftwidth=2
set softtabstop=2
set ai
set expandtab
set smarttab
let g:rubycomplete_rails = 1

map f :FuzzyFinderTextMate<CR>
map n :tabnew<CR>
map c :tabclose<CR>
map m :tabnext<CR>

let g:fuzzy_ignore = "*.svn"
let g:fuzzy_ignore = "*.swp"
let g:fuzzy_ignore = "*.jpg"
let g:fuzzy_ignore = "*.gif"
let g:fuzzy_ignore = "*~"

set nobackup

.gvimrc

source ~/.vimrc
set selectmode=mouse
set columns=100
set lines=50

In this set up, there are no chords etc. Instead, when not in edit mode, 'n' will open a new tab. 'f' will let you find a new file to open in the current tab. 'c' will close the current tab and 'm' will move between tabs. So in a normal workflow, you might decide to swap the file in the current tab for a new one (simply press 'f'), or if you need another file open, hit 'n' for new tab, and then 'f' to load the relevant file. My text description doesn't do it justice, but I find this works very well to get you to the file you want quickly, and let you have the files you're interested in open all at the same time.

One last note, remember to start vim/gVim in the root of your rails directory.

Happy Vimming 🙂

UPDATE
These files are now available from my github dotfiles repository, including the gem inside of the vim/gems_required directory.

21 02 2009

Contact Form For Mephisto updated for Drax 0.8

Mephisto Drax (version 0.8) introduces breaking changes for plugins. I've just finished updating the contact / feedback form plugin. It's now working fine and tests passing.

Installation instructions are the same as before except that the 'contact_notifier' has moved from the 'lib' directory to the 'app' directory. It still needs to be updated to include your destination email address for contact mails.

If you're a Mephisto plug-in developer, you might be interested in checking out my post on migrating Mephisto plugins to Drax. It's based on my experiences with the contact_form.

25 08 2008

Migrating Mephisto Plugins to Drax 0.8

There have been some major changes to Mephisto in the latest release (0.8 Drax) that break existing plugins. If you're interested in migrating your existing plugin(s) over to Drax, read on.

Repository Move
First thing to note is that the Mephisto code base has moved from SVN to github.

Plugin Architecture Changes
There is no longer a base class for Mephisto plugins. Instead, you create Mephisto plugins using Rails Engines. If you're migrating a pre-Drax plugin to Drax and Rails engines, you'll most likely need to:

  • Remove your plugin file - there's no base class for it any more so you'll get errors like: 'superclass must be a Class (Module given) (TypeError)'
  • Move your routes into a 'routes.rb' file in your plugin root directory.
  • In your plugin root directory, create an 'app' directory, with 'views', 'models' and 'controllers' sub-directories. Move your code files into the appropriate folders in the 'app' directory. These will be auto-loaded.
  • Remove various lines in your init.rb which manually add your plugin file directories to the load paths, if you have these.
  • If you inherit from the ApplicationController, add 'unloadable' to your controller class. This will fix errors in development mode like 'A copy of ApplicationController has been removed from the module tree but is still active!'
  • An example
    You can have a look at my contact_form plugin code. Revision 18 is before Drax and engines and uses the old approach. Revisions 19 and later are using Rails engines and will work with Drax.

    UPDATE: Latest code at GitHub
    http://github.com/jcrisp/mephisto_contact_form/tree/master

25 08 2008

RiskAssess – Risk Assessments for Schools

In my spare time, I've been developing RiskAssess, an online risk assessment system for schools. RiskAssess helps teachers and lab assistants to quickly and easily produce risk assessments for laboratory experiments which meet Australian standards. RiskAssess should help decrease the number of accidents in school laboratories, and also help schools meet legal requirements.

I'm pleased to say that the site is now well and truly launched and we have schools doing risk assessments every day using RiskAssess. There's still much to do and many new features to add, but so far we've had very positive feedback from the schools using the system.

On the technical side, RiskAssess is a Ruby on Rails site.

7 08 2008

Loan Calulator: Monthly repayment and interest breakdown

Wondering if your loan repayment calculations have been performed correctly this month, taking into account interest rate rises and extra repayments? You might be interested in giving my monthly loan calculator a go.

As my home loan provider doesn't show balances online, and only sends statements every 6 months, I like to ring up every month or two to make sure things are on track. I used to calculate interest, new balances etc in a spreadsheet / calculator but spent an afternoon writing a little Rails app to calculate it for me. Hopefully my little monthly loan calculator is of some use to you too.

14 04 2008

Sydney BarCamp 3 this Weekend!

BarCamp Sydney is two days long this year, and will span the whole of the coming weekend (5-6 April). I'll be there on Saturday. It's usually quite a fun event, lots of good sessions and you're quite free to move around and find something that interests you. Part of the BarCamp manifesto is that you should also contribute as well as listen - I'll probably give a JRuby talk and demo.

Hope to see you at BarCamp!

3 04 2008

“JRuby: Enterprise 2.0” Slides

Here's the slides from “JRuby: Enterprise 2.0″ from our recent talks at Sun Tech Day and the ACS Web Technologies SIG.

7 03 2008

“JRuby: Enterprise 2.0” Presentations

Josh Price and I will be co-presenting at the Melbourne Sun Tech Day and the Sydney ACS Web Technologies SIG. Here's the low down:

JRuby: Enterprise 2.0
There's a lot of buzz around JRuby in both the Java and Ruby communities, for good reason. This talk will give you a whirlwind introduction to JRuby. We'll show you why JRuby is regarded as such a powerful and dynamic development platform. We'll also suggest where to use JRuby in product development and the enterprise and how to leverage your existing Java investments.
There will be sample applications, live demos and not many slides.

Sun Tech Day Melbourne
4 March 2008, 2:30pm
More info on Sun site...

ACS Web Technologies SIG Sydney
5 March 2008, 6:15pm
More info on ACS site...

Hope to see you there!

23 02 2008

Practical JRuby on Rails (Web 2.0 Projects) by Ola Bini

The fine folk at Apress sent me a copy of Ola Bini's new book to review. The full title is "Practical JRuby on Rails Web 2.0 Projects - Bringing Ruby On Rails to the Java Platform". Overall, it was a good read, and extremely valuable to anyone who is developing in JRuby. JRuby information and documentation is scarce and most of the time, a Google trawl does not give you good results on a JRuby related query. Ola's is the first, and currently the only JRuby book available, and in my experience, the most valuable resource available to give you an all-round picture of JRuby capabilities and usage.

Audience
Despite comments on the cover, I would suggest that this book is not ideal for people new to Ruby / Rails. Ola jumps in the deep end quite quickly, and being a talented Ruby programmer, makes use of lots of shorthand, procs, code blocks etc which would likely be hard to follow for someone new to Ruby. Although there is a section at the back called "Ruby for Java programmers", I think this would not be sufficient for somebody new to Ruby to understand all the code examples.

To get the most value out of the book, it would be good to have at least a basic understanding of Ruby and Rails (eg, having read Agile Web Development with Rails or messed around with Ruby/Rails a bit) and a basic understanding of Java syntax, deployment and Java EE.

What's Covered?
The book is project based, so as to give context and useful examples of JRuby functionality. There are 4 projects:

  • The Store (Shoplet) - a standard Rails app running under JRuby using Active Record JDBC.
  • Content management system - general Java integration and using Java libraries for content rendering.
  • Administration System - using EJBs, JMX and discussion of JRuby deployment options.
  • Library System - JRuby as the "glue that never sets". Using Java Web service frameworks and JMS from JRuby.

The Good

  • Teaches you how to do all those tricky bits which are half-Java and half-Ruby and can't be easily found online, such as converting between Ruby and Java types, including JAR files, implementing Java interfaces, etc
  • Clever and concise Ruby code - I picked up some Ruby tricks reading Ola's code.
  • Complex code snippets are generally well explained in text.
  • Useful tips on when to use Java libraries and when to use Ruby ones.
  • Generally good and interesting example projects which justified the use of JRuby and the techniques shown in the book.
  • Helpful discussion of JDBC and database connectivity options for JRuby.
  • Nice overview of the many JRuby deployment options.
  • Helpful "sidebars" about Java Enterprise Edition technologies.
  • Covers the strong areas of JRuby well - web applications and system integration.
  • Appendices provide useful reference information.
  • Nice section at the end on how you can get involved in JRuby.

The Less Good

  • Example views often contain table layouts, inline styles and other layout information that would be better done in separate CSS files.
  • Variable names in code could be more descriptive. This would make example code easier to follow.
  • Occasional odd spelling like "sur_name" and use of deprecated Rails features, such as "start_form_tag" (to be fair though, Rails API does change very quickly).
  • The title suggests that the book is about Web 2.0. There is a little token AJAX, and I suppose a content management system is a bit Web 2.0, but overall, buy the book if you want to know about JRuby, not Web 2.0.
  • Although REST is only mentioned briefly in a little sidebar, and not a focus of the book, I found the description of REST and CRUD a bit misleading, especially when considering PUT vs POST.
  • The discussion of JRuby deployment provides a good overview, but more in depth discussion of major options (eg, GoldSpike), and production configurations would be great.

Conclusion
As the best and only JRuby reference, I'd highly recommend you buy a copy if you are working in, or planning to work in JRuby. The book will help you to write JRuby applications which make good use of Ruby, Rails, Java libraries and Java Enterprise Edition features.

16 12 2007