Sep 18, 2008

Step by step setting up an OS X Leopard server for a Merb/DM/MySQL5.1 application with Phusion Passenger

1. Working env. set up
    a. Update your OS X Leopard
    b. Install Developer pkg
    c. Install MacPort
        - download at http://www.macports.org/
    d. Install git
        $sudo port install git-core

2. Install the latest MySQL server
    http://zhhz.blogspot.com/2008/08/how-to-tune-mysql-server-performance.html
    sudo gem install mysql

3. Install edge Merb/DataMapper
    a. Install Thor
        $ sudo gem install thor
    b. Local installation for a bundled application
        I like to bundle my dependencies with the application, make it independent of my system's gems. This will make it easy to trace with the edge Merb and DM.
        
        - install thor
            $cd your-merb-app
            $curl -L http://merbivore.com/merb.thor > merb.thor
            you can see the tasks by:
            $thor list
        - install edge merb
            $thor merb:tasks:setup
            if you have some errors here, you probably should install rspec first: 
            $sudo gem install rspec
            You will get bin and gems folders
            Install edge merb and dm
            $bin/thor merb:edge --install
            You will get a src folder
            $bin/thor merb:edge:plugins --install
        - install edge dm:
            $bin/thor merb:source:clone git://github.com/sam/dm-core.git
            $bin/thor merb:source:clone git://github.com/sam/dm-more.git
            $bin/thor merb:source:clone git://github.com/sam/do.git
            $bin/thor merb:source:install dm-core
            $bin/thor merb:source:install dm-more
            $bin/thor merb:source:install do/data_objects
            $bin/thor merb:source:install do/do_mysql
            $bin/thor merb:source:install dm-more/merb_datamapper
        - install mongrel
            $bin/thor merb:gems:install mongrel
            You should be able to start your merb app by:
            $bin/merb

4. Install Phusion Passenger
    Details from: http://www.modrails.com/
    a. $gem install passenger
    b. $passenger-install-apache2-module
    c. Modify your httpd.conf
        # c&p from the console of passenger installer:
        LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
        PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-2.0.3
        PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
        # config for your app
            <VirtualHost *:80>
                ServerName app.yourdomain.com
                DocumentRoot "abs_path_to_your_merb_app/public"
            </VirtualHost>
        d. Config app for passenger:
            go to your merb app directory:
            $cd your-merb-app
            1. create a tmp folder
                $mkdir tmp
            2. create a file config.ru with following content:
                require 'rubygems'

                if File.directory?(gems_dir = File.join(File.dirname(__FILE__), '.', 'gems'))
                    $BUNDLE = true; Gem.clear_paths; Gem.path.unshift(gems_dir)
                end

                require 'merb-core'

                Merb::Config.setup(:merb_root   => ".",
                    :environment => ENV['RACK_ENV'])
                Merb.environment = Merb::Config[:environment]
                Merb.root = Merb::Config[:merb_root]
Merb::BootLoader.run

                run Merb::Rack::Application.new
            e. Restart your apache server.

5. Modify your spec_helper.rb
    to make your specs working, you have to do the same as the config.ru:
    if File.directory?(gems_dir = File.join(File.dirname(__FILE__), '..', 'gems'))
        $BUNDLE = true; Gem.clear_paths; Gem.path.unshift(gems_dir)
    end
You are ready to go!


UPDATE: The merb part may not good now.

[RUBY] Formating numbers with commas

I want to format the numbers with commas, there maybe a better way to do it, but this one works for me:

  # Return String formated as "123,456.78"
def format_number(number)
  s = "%0.2f" % number
  s =~ /([^\.]*)(\..*)?/
 int, dec = $1, $2
 int.gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,') + dec
end