Ruby Journal

Minitest Let() Is Lazy

| Comments

Don’t you know that minitest also have a similar let() helper that does exactly the same job as the one of RSpec. But some people do not fully understand the difference between defining object in let() and before() block.

How to Block Old IE Version With Rails

| Comments

There are many ways to detect browser agent, it could be front-end side with Javascript or backend. In this short tutorial, I’ll walk you through on how to detect browser version with Ruby On Rails

This applies for Rails > 2.x

Install Postgres.app on OSX 10.7+

| Comments

Traditionally, pogstgresql is installed manually with MacPort or Homebrew on Mac OSX 10.7+. I used to have lots of problem with the setup for the installation as it requires Xcode, this libs and that libs, etc. In summary, it is not convenient enough and I want something as simple as dragging an OS app to my /Application. Thanks to Heroku, they took the heed and create Postgres.app. A wrapper bundled with binary postgresql server. It is not only easy to install but also easy to setup config file if you are using Rails.




Install Ghostscript on Heroku Cedar

| Comments

In this tutorial, I will show you how to install ghostscript on Heroku Cedar. As you might have known that Heroku virtual machine does come with a system-wide ghostscript version which is located at /usr/bin/gs. You can find out the location of this version:

1
2
3
$ heroku run bash
$ /usr/bin/gs --version
8.71

However, explicit dependencies is not recommended, you could read 12 Factor Approach on dependencies at http://www.12factor.net/dependencies. Credit to Ryan Daigle who pointed it out for me and I agree with him.

To install ghostscript, we fetch the source under heroku console, fetch the source, configure and compile the software:

1
2
3
4
5
6
7
$ heroku run bash
$ curl -O http://downloads.ghostscript.com/public/ghostscript-9.05.tar.gz
$ tar xzvf ghostscript-9.05.tar.gz
$ cd ghostscript-9.05
$ ./configure --disable-cups --disable-gtk --with-drivers=FILES
$ make
$ cp bin/gs ~/bin

You might notice that I only specify configuration parameters --with-drivers=FILES. It is because I don’t need printer drivers for my app which only does think like images and PDF manipulation.

Once the compilation is completed, copy the binary ghostscript-9.05/bin/gs to ~/bin. All binaries in ~/bin will be available for your Heroku app now. You can verify if the binary works by:

1
2
$ gs --version
9.05

And please do not forget to clean up:

1
2
$ cd ~
$ rm -rf ghostscript-9.05*

If you feel lazy, you could download my Ruby-wrapper of gs at https://github.com/joneslee85/ruby-ghostscript.

Exposing ActiveRecord Model Attributes to Liquid Dynamically

| Comments

Liquid is a powerful templating tool especially when used with rails. It is quite common that you have to expose ActiveRecord attributes to liquid. You can achieve that by implement to_liquid method in your ActiveRecord model so it acts as if it were Liquid::Drop, OR you can use the helper liquid_methods to tell which attributes / call-able methods of the instance that could be passed with the liquid_methods call. In most of cases, people tend to use the latter method because they could narrow the exposure scope to liquid.

However, what if your model has so many attributes and typing all them out for the liquid_methods seems arduous, you can dynamically mapping attributes by creating a module:

lib/attributes_to_liquid_methods_mapper.rb
1
2
3
4
5
6
7
8
9
module AttributesToLiquidMethodsMapper
  def self.included(base)
    base.class_eval do
      base.attribute_names.each do |attribute|
        liquid_methods attribute.to_sym
      end
    end
  end
end

And you also need to include the above module in the ActiveRecord classes that you want to be exposed to Liquid:

1
2
3
4
class MyObject < ActiveRecord::Base
  require 'attributes_to_liquid_methods_mapper'
  include AttributesToLiquidMethodsMapper
end

Hope you enjoy this tutorial. Comments and feedbacks are greatly welcomed :)