Ruby Tea with Sugar Rails

Step by step to deploy application using SVN and Capistrano

Here process deploying of application in a hosting (in this case i deploy my application in FastCGI on HostingRails.com). There are 3 major steps of deployment :


  1. Create SVN Repository (Next I will explain about GIT)
  2. Setup Capistrano & Deploy Application with Capistrano


Create SVN Repository

  • Create REPOSITORY to your hosting account, please ensure that your hosting is already installed capistrano & SVN or ask your hosting admin/support to install SVN and Capistrano.
username@mydomain.com [~]$ mkdir -p ~/svn/your_app_name
username@mydomain.com [~]$ svnadmin create ~/svn/your_app_name
username@mydomain.com [~]$ mkdir -p ~/tmp/your_app_name/{tags,branches,trunk}
username@mydomain.com [~]$ svn import ~/tmp/your_app_name file:///home/username/svn/your_app_name -m "Created Repository"

  • At this point you have enough setup on your Hosting account for single-user access to Subversion. You can now check out your repository at:
username@mydomain.com [~]$ svn co svn+ssh://username@mydomain.com/home/username/svn/your_app_name/trunk

  • Next we are going to add your existing Rails app to the Subversion repository. If you don't have an existing app, you will need to create one.
username@mydomain.com [~]$ svn co svn+ssh://username@mydomain.com/home/username/svn/your_app_name/trunk your_app_name_dev

  • Ok, it looks fine if you do right. The next step is copy your application for repository. Before this process, upload your existing application folder to your host in zip or tar.gz file. After uploaded, extract it to up folder of public_html (do not extract your application in public_html). After extracting, we will modify some data and permission file.

  • Your database.yml(.online) file simply needs to look like this:

production:
 adapter: mysql
 database: [your_hosting_username]_[your_database_name]
 username: [your_hosting_username] OR [your_hosting_username]_[your_database_name]
 password: your_password


  • And in your environment.rb(.online) file you'll just need to uncomment the following line to confirm you're in production mode:
ENV['RAILS_ENV'] ||= 'production'

  • Make sure your .htaccess file sends requests to dispatch.fcgi. Please Note - as of Rails 2.1.0 the .htaccess file no longer exists in the default skeleton.  Run a "rails _2.0.2_ testapp" and move over the public/.htacess from there.  This one is easy but also critically important.   In your local Rails application's public/.htaccess file change the :

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

  • EXACTLY where it is already in the file, don't change its position, to: 
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

  • also - very important make sure the 'AddHandler fastcgi-script .fcgi' is commented out at the top of your .htaccess:  
    # General Apache options
    # AddHandler fastcgi-script .fcgi
    And that's it

  • Now look into public folder and edit all files with name "dispatch" (they are 3 files), edit the first line and change to :
#!/usr/local/bin/ruby

  • Do not forget to change permission into 755 for all files in public folder specially all dispatch.* files and .htaccess.

  • Next, you will need to copy your existing Rails app on your local machine into the version we just checked out
username@mydomain.com [~]$ cp -R your_app_name/* your_app_name_dev

  • Now you can see if Subversion recognizes the updated files and directories:
username@mydomain.com [~]$ cd your_app_name_dev
username@mydomain.com [~/your_app_name_dev]$ svn status

  • You should get something like this:

?      test
?      app
?      tmp
?      log
?      Rakefile
?      script
?      components
?      config
?      db
?      doc
?      lib
?      README
?      vendor
?      public


  • Now, just add them to the repository:

username@mydomain.com [~/your_app_name_dev]$ svn add test app tmp log Rakefile script components config db doc lib README vendor public

  • You will see a list of the files that were added with an 'A' next to indicating that the files will be added on the next commit. You will see something like this:

A         test
A         test/fixtures
A         test/fixtures/contact_mailer
A         test/fixtures/contact_mailer/notify
A         test/fixtures/contact_mailer/validate_email
A         test/fixtures/contacts.yml
A         test/functional
A         test/functional/website_controller_test.rb
A         test/integration
# and so on


  • Next, lets commit the Rails app to the repository
username@mydomain.com [~/your_app_name_dev]$ svn commit -m "Added The Application"

  • This may take a few minutes depending on the size of your application. You will see the list of files appear again followed by this:
  • Transmitting file data .................................................................
    Committed revision 2.

  • Now your will be able to edit your code on your local machine and have the changes reflected in newer revisions simply by adding and committing files.


Setup Capistrano & Deploy Application


  • Setup your Rails application to work with Capistrano. Once you have checked out your code from the svn repository, change to the root of the Rails app on your development machine and run (look carefully there is a dot after capify command, just type it completely with a dot):

    username@mydomain.com [~/your_app_name_dev]$ capify .

    # This is the output
    [add] writing `./Capfile'
    [add] writing `./config/deploy.rb'
    [done] capified!
    Capistrano created two files: /config/deploy.rb and Capfile. (Note: /config/deploy.rb might have been skipped if you already had a deploy.rb file in your app.) The config/deploy.rb file will contain your application variables used to deploy your app, while the Capfile will contain your deployment tasks.


  • So open the deploy.rb file and we'll start customizing the deployment process. The first thing we want to do is set our application variables. So let's modify this first section of the file:

set :application, "your_app_name"   set :domain, "mydomain.com"  set :user, "username"        set :repository,  "svn+ssh://#{user}@#{domain}/home/#{user}/svn/#{application}/trunk"  set :use_sudo, false     set :deploy_to, "/home/#{user}/#{application}"   set :deploy_via, :checkout set :chmod755, "app config db lib public vendor script script/* public/disp*"  set :svn_username, "username" set :svn_password, "hosting_password" set :password, "hosting_password" default_run_options[:pty] = true ssh_options[:keys] = %w(/Path/To/id_rsa) role :app, "mydomain.com" role :web, "mydomain.com"
role :db, "mydomain.com", :primary => true

  • Now that everything is in place, you can commit your changes to the Subversion repository if you want:

    username@mydomain.com [~/your_app_name_dev]$ svn add config/deploy.rb Capfile
    username@mydomain.com [~/your_app_name_dev]$ svn commit -m "Configured application for Capistrano"

  • Last Step - Setup Your Hosting Account. Next we need to setup our Hosting account. This is where our hard work pays off with Capistrano. From your local machine enter the following commands:
username@mydomain.com [~/your_app_name_dev]$ cap setup


  • Next you need to run a cold deploy.

username@mydomain.com [~/your_app_name_dev]$  cap cold_deploy


  • Next we can do a full deploy

username@mydomain.com [~/your_app_name_dev]$  cap deploy


  • Or if you want Capistrano to run your migration files (be sure to have your database and database.yml setup properly).

username@mydomain.com [~/your_app_name_dev]$  cap deploy_with_migrations


  • If you ever run into problems and need to rollback to a previous revision of your code simple enter:

username@mydomain.com [~/your_app_name_dev]$  cap rollback


  • As you make revisions to your code on your local machine and are ready to deploy your updated application to the production server all you have to do is:

username@mydomain.com [~/your_app_name_dev]$ $ svn commit -m "Your Custom Reference Message";cap deploy


Case Abtractions [Indonesian Language]

Sorry for you guys, i write this article for my friends in my country, indonesia, to support their final thesis projects.


1. SPPK Pembelian Property atau Real Estate.

  • Description : Sistem ini ditujukan bagi user atau konsumen yang memiliki keinginan untuk membeli rumah namun memiliki pengetahuan terbatas tentang lokasi mana saja yang masih tersedia sesuai dengan keinginan mereka. Input user konsumen dapat berupa range budget atau harga yang di inginkan konsumen, luas tanah, luas bangunan, lokasi, dll. Diharapkan sistem ini dapat bekerja sama dengan para realestate group sehingga pengusaha realestate dapat menginputkan informasi tipe rumah, harga, lokasi, gambar rumah/design dan peta (dapat menggunakan peta dari google), harga basis investasi dapat didesign berdasartkan permintaan user developer untuk ditampilkan semakin meningkat berdasarkan periode waktu. Output bagi user konsumen adalah daftar real estate, diikuti dengan sublist informasi rumah yang masih tersedia (belum laku terjual) yang terdiri dari gambar rumah atau denah rumah, harga, dan detail property serta prediksi nilai investasi jika membeli rumah tersebut serta fasilitas anggunan yang ditawarkan developer jika tersedia.
  • Contoh www.propertyfinder.com
  • Skala user : masih dalam satu k0ta
  • Software Target: property group atau real estate group.
  • Tantangan: gunakan mushup google map untuk realtime peta jika tidak terdapat peta kota. Sistem dapat mendukung upload gambar atau video promosi jika diperlukan.
  • Teknologi basis web.

2. Event Management
  • sebagai salah satu contoh event management ditujukan untuk dosen universitas atau fakultas. Sistem ini dapat dikatakan sebagai perencanaan aktivitas dosen yang memudahkan mahasiswa untuk memantau keberadaan dosen untuk keperluan konsultasi. Dosen juga dapat memberikan keterangan terhadap jadwal konsultasi dan mahasiswa dapat mendaftar langsung untuk konsultasi, seperti pasien mendaftar dokter dirumah sakit, sehinga dosen dapat mengetahui siapa saja mahasiswa yang ingin berkonsultasi atau bertemu dengan dosen. Mahasiswa juga dapat memilih tanggal konsultasi dan jam yang diinginkan untuk bertemu dengan dosen, dosen akan menerima laporan berupa pesan pada halaman pribadinya. Output hampir mirip dengan kalender agenda dan email portal. untuk registrasi konsultasi fisik, system menampilkan daftar user pada tanggal tertentu berdasarkan urutan tanggal, deskripsi keperluan, dan nama mahasiswa.
  • Hal-hal yang dapat dilakukan : booking dosen, email portal, mahasiswa dan dosen, dapat upload dan download file dari email portal tersebut, calendar agenda.
  • Tantangan : dosen dapat menginput agenda dia melalui sms lalu event portal memprosesnya dan menampilkannya ke agenda event dan portal memberi report sms bahwa agenda telah dibentuk.
  • Contoh kasus lain sebagai perbandingan dapat dilihat disini : http://www.peoplecube.com/solutions-event-management.htm

3. Electric card or business card
  • Web ini ditujukan buat user yang ingin membuat kartu nama, kartu pos, atau poster, user dapat mengupload designnya untuk dicetak atau dijual memalui perantara yang punya website. owner website akan mengirim pesanan user jika telah selesai, pembayaran dapat berupa transfer bank.
  • Tantangan : sistem mendukung user mendesign sendiri postcardnya secara online atau mendesign ulang yang sudah jadi atau gunakan ajax dalam aplikasi ini.
  • kasus tidak hanya untuk kartu tapi dapat diganti dengan design baju online.
  • Contoh kasus : http://www.harboarts.com/shirtdesigner/productpage01.php
  • Tantangan : gunakan flex atau ajax untuk redesign atau design online

4. Web Operation System
  • Silahkan pelajari web ini : http://www.cloudcomputing.gopc.net/?gclid=CIr-xbi03pQCFQWxsgodJnu_SQ
  • Dapat diimplementasikan sebagai notebook untuk dosen atau mahasiswa, untuk menyimpan kuliah praktikum yang dpt dilakukan secara online, sehingga user dapat membuka kembali hasil praktikumnya diwarnet atau hasil kerja dan kuliahnya diwarnet.
  • Solusi : Gunakan Flex atau RJS atau AJAX.

5. Posyandu Online atau Apotik Online
  • Web ini serupa SPPK yang membantu user memilih obat berdasarkan penyakit yang dihadapinya atau ciri-ciri penyakit yang dihadapinya dan alergi user terhadap obat jika ada, output berupa daftar obat dan indikasinya serta aturan pemakaian.
  • Tantangan terhubung dengan apotik, artinya jika output obat sudah ditampilkan user dapat memesan langsung obat yang bersangkutan, dan obat dapat diantar. (lingkup hanya dalam kota saja.)

How to use find_by_sql without Model

many cases, we use model to use find_by_sql like ModelName.find_by_sql, but now how we can find_by_sql without mode?

ActiveRecord::Base.connection.execute 'select * from users'

To return array of hashes, you can use it :

ActiveRecord::Base.connection.select_all

.