Tutorials

Setup Passenger 4 (pre-release RC4) for Apache2 and Ruby 2.0.0

Setting up Apache2 and passenger for Ruby 2.0.0 is actually pretty simple. As of right now, for Passenger to work with Apache2 and Ruby 2.0.0 you will need to install Passenger 4 RC4 or above, which currently isn’t (technically) a stable release. I have found it’s pretty stable though.

Install passenger through RubyGems

First, install passenger through gems:

gem install passenger --pre

Continue reading

Install multiple Ruby versions on Ubuntu 12.04 using RVM

RVM or Ruby Verion Management is a library written for managing multiple version of Ruby on a single machine, and even for a single user. Using this set of libraries you can easily install a version of Ruby and switch between different version as and when you see fit. This is great for developers, especially those having to maintain multiple different applications, using different Ruby and potentially rails versions.

Installation of RVM on Ubuntu 12.04 for a single user

To start, download RVM and automatically install the latest stable version of Ruby

curl -L https://get.rvm.io | bash -s stable --ruby

Here we download a file which has a series of commands for installing RVM and the latest stable version of Ruby. `-s` tells bash to read commands from the standard input, which is the downloaded file from the rvm website. This process will take a while, depending on your connection speed and processors as it downloads and compiles Ruby and Rubygems. Continue reading

Example configuration with God – Monitor nodejs

God is a very powerful process monitoring library written in Ruby. This tutorial describes how to setup a basic monitoring configuration file for a NodeJS application, telling God to restart the NodeJS app upon the process being killed (for what ever reason).

To get started, create a god/ directory within your Node app.

mkdir /var/www/sites/nodeapp/god

In this directory, create a god configuration file

vi /var/www/sites/nodeapp/god/nodeapp.god

Update the global God configuration file (Refer to previous tutorial on installing God on Ubuntu 12.04) with the location of your Node app config.

sudo vi /etc/god.conf
1
God.load '/var/www/sites/nodeapp/god/*.god'

Great, when you reload God it will start using the configuration, but first we must write the configuration file. Open your nodeapp.god config file for editing. We will start off by creating a basic configuration file that will allow you to:

  • Start node
  • Stop node
  • Restart node

Continue reading

Install God background process monitoring on Ubuntu 12.04

God, a monitoring framework written in Ruby is easy to configure, easy to extend and easy to use. God allows you to monitor the state of a process (whether it is running or not) and perform specific tasks dependent upon the state. These tasks can be as simple as restarting the process or go as far as alerting you via email, text, a phone call – basically, anything you can programmatically do in Ruby.

This tutorial shows you how to setup God on Ubuntu 12.04 (and I’m pretty sure it would work for earlier versions of Ubuntu too). I will cover the installation and layout of configuration files, as well as setting up an init service script allowing for God to be started|stopped|restarted easily, and automatically started at boot. Continue reading

Paginate arrays with will_paginate

There may come a time when you wish to paginate an ordinary array of objects, as opposed to an ActiveRecord collection. Thankfully will_paginate supports this out of the box, with very little work.

Firstly, you will need to include the will_paginate array library, which comes with will_paginate. To do this either create or add to an existing initializer in config/initializers/. With all projects I tend to create a file named project_library.rb. Continue reading

How to: Detect if validating inside model

In a model I needed to modify a getter to append a string if a certain attribute evaluated to true. This works great, until I needed to detect the uniqueness of the attribute against a column in the DB through the :uniqueness validator.

The problem

1
2
3
4
5
6
7
8
9
  def label
  new_value = read_attribute(:label)
 
  if self.something == true
    new_label += 'Str'
  end
 
  new_label
end

When validating against the label’s uniqueness the validator will read the label attribute from my custom getter, thus, if a record in the DB has a label of ‘Test’, and our new record has a label of ‘Test’ and self.something evaluates to true, the validator will get ‘TestStr’ from the getter and compare the custom value against the DB. Based on this setup, no match would be found. Continue reading