Wednesday, March 19, 2008

Rails ActiveMerchant Patch For BeanStream

I submitted the patch to active merchant plugin for Canada payment gateway BeanStream. I guess that it will take some time to get reviewed, tested and accepted ( or not accepted because the remote test can not be passed without beanstream's test account which it doesn't provide it to me), so I uploaded the patch to a google code project:

http://code.google.com/p/activemerchant-patch-for-beanstream/

The installation, description and a simple example have been already written in the project home page. Issue your bug or give me your feedback if you encounter any problem when using it.

A Rails Plugin table_styles to let you easily use up to 42 different css styles for HTML table.

Today I wrote a rails plugin based on CSS Table Gallery .

I upload to google code: http://code.google.com/p/rails-plugin-table-styles/

Installation
script/plugin install http://rails-plugin-table-styles.googlecode.com/svn/trunk/table_styles

Usage

* Use the demo to find one of your favorable style.
# Start your server: >script/server
# Visit http://localhost:3000/table_styles_demo/
# Remember to add map.connect :controller/:action in your routes.rb

* Choose one of table styles in your layout header, use "tagbox" as an example.
<%= table_stylesheet_link_tag "tagbox" %>

* Just use table tag it in your view

* Use form helper if you want to let admin user to choose table style in user preferences page.
table_style_select or table_style_options_for_select

Rails irb or script/console tips in one glimpse

Summarize most basic tips of using script/console here:
  • # script/console (note: start it)
  • # script/console production (note: start it with production environment)
  • # script/console -s (note: run in sandbox, all change to data records will rollback)
  • >>$LOAD_PATH (note: see the load_path of current environment)
  • >>reload! (note: reload the environment)
  • >>clear
  • >>$ (note: use TAB key to see all environment variables)
  • >>Str (note: use TAB key to auto complete)
  • >>@hello = "Hello word!" (note: define variable)
  • >>@hello = Object.new (note: define variable)
  • >>@order = Order.find(1) (note: get an order object, order.rb should be in one of app models)
  • >>app.get "/articles/1234" (note: run controller by route path)
  • >>app.post "/login", {:login => "xiaobozz", :password => "12345"}
  • >>helper.image_tag "logo.gif" (note: run any method of actionview helpers)
  • >>y @order (note: YAML style output)

The best way to get familiar with these tips is to use them now :-)

Monday, March 17, 2008

Rails send emails with actionmailer and gmail

The first step is to configure your rails application to work with gmail, I just copied the 2 simple steps from the Preston Lee's article:

1. Save this code as lib/smtp_tls.rb within your rails app;
2. Add this code to config/environment.rb

The second step is to learn to use actionmailer, just follow the document "How to send emails with action mailer" to do it then you will understand how it works pretty fast.

1. ruby script/generate mailer Notifier signup order_created
2. add code to your app/models/notifier.rb, such as

def signup( user )
# Email header info MUST be added here
recipients user.email
from "accounts@mywebsite.com"
subject ¡°Thank you for registering with our website¡±
# Email body substitutions go here
body :user=> user
end

3. add code to /app/views/notifier/signup.erb
Dear <%= @user.first_name %> <%= @user.last_name %>,
Thanks for signing up with My Website!

4. use the mailer in where you want.
Notifier.deliver_signup(user)

Wednesday, March 12, 2008

How to restart Webmin

Webmin provides a stop and start command itself, while it is not in any bin path. It is in the initial installation path.
su
cd /usr/libexec/webmin
webmin-init stop
webmin-init start

Done!

Tuesday, March 11, 2008

A special post to claim on Technorati

This is a special post to claim on Technorati:

Technorati Profile

Rails Mongrel and Apache servers work with SSL, certificates and https

I am already running Apache as the front end server and Mongrel Rails Cluster Servers as the back end. To make them work with SSL, certificates and HTTPS, there are several easy steps to finish.

# 1. make a directory to store ssl key and certificates
mkdir /etc/httpd/conf/ssl

# 2. create openssl key and make it secure
cd /etc/httpd/conf/ssl
openssl genrsa -des3 -out yourdomain.key 1024
chmod 600 yourdomain.key

# 3. create CSR file (certificates signing request)
# Common name is your domain name without http:// but with www.
openssl req -new -key yourdomain.key -out yourdomain.csr

# 4. I bought godaddy's ssl certificate
# And got the certificate by email in 5 mins after I submitted the CSR.
Download the CSR zip file, unzip the two certs into ssl directory

# 5. Config apache's yourdomain config part
# Assuming that you have configurated yourdomain.common and yourdomain.cluster_proxy.conf properly.
virtualhost
ServerName www.yourdomain.com:443
Include /etc/httpd/conf/yourdomain.common

RequestHeader set X_FORWARDED_PROTO 'https'

ErrorLog logs/yourdomain_ssl_errors_log
CustomLog logs/yourdomain_ssl_log combined

SSLEngine on
SSLCertificateFile /etc/httpd/conf/ssl/yourdomain.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl/yourdomain.key
SSLCertificateChainFile /etc/httpd/conf/ssl/gd_intermediate_bundle.crt
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
/virtualhost

# 6. install and use plugin ssl_requirement
script/plugin install ssl_requirement
include SslRequirement (in your application.rb)
ssl_required :login, :orders (in your controller with those actions)

Saturday, March 1, 2008

Fast install SVN server on linux

It is crazy easy to build SVN server, much easier than CVS server. Just do the following steps:

  • su root
  • # download the relatest stable version of SVN source code
  • wget http://subversion.tigris.org/downloads/subversion-1.4.6.tar.gz
  • tar xzvf subversion-1.4.6.tar.gz
  • cd subversion-1.4.6
  • ./configure
  • make & make install
  • svnserver --version # check the installation result
  • svnadmin create /svnrepos
  • vi /svnrepos/conf/svnserve.conf
  • # uncomment and edit in this config file
  • anon-access = none
  • auth-access = write
  • password-db = passwd
  • # add your username and password
  • vi /svnrepos/conf/passwd
  • # start server as a daemon
  • svnserve -d
  • # use it like:
  • svn co svn://server_ip_address/svnrepos/myproject


Reference:
Setup a Subversion Server in 4 Minutes