Thursday, October 1, 2009

Using Questions Answers Foo Plugin to add Q&A to your Rails app

Questions Answers plugin adds linkedin like Questions and Answers functionality to your
Rails application. Plugin assumes existance of model named User and that model
contains information about users of system. If User model does not exist the
plugin will print warning message on console at startup. If you dont already
have authentication user model i recommend you install restful_authentication
plugin.

The plugin uses login attribute of user model in views by default. You can
change that default by declaring this constant in your environment.rb file


VIEW_QA_USER_ATTRIBUTE = "full_name"


To change look of the module change the css refrenced in
ui/views/layouts/qa.html.erb

Install this plugin in your rails application by running


./script/plugin install git://github.com/hasham2/questions_answers_foo.git


To complete installation of plugin run following rake task from root directory
of your application.


rake questions_answers:install


This will create database migration to hold Questions and Answers data. After
running this task run:


rake db:migrate


to commit changes to database structure.

Monday, May 11, 2009

Deploying rails applications with vlad

Vlad is application deployment automation tool much like Capistrano but with much less complexity and it integrates right into rake. so no need to capify your project again. Vlad is based on standard nix tools like ssh and rsync .

To make it even better is that it comes with built in support for number of version control systems like Darcs, Subversion, Mercurial and best of all Git. To get started you need to install vlad gem on your machine and on each of servers you wish to deploy your ruby app.

sudo gem install vlad


The variables for deployment are typically setup in config/deploy.rb file just like capistrano. For trival one server deployment all you have to do is set four vaiables in deploy.rb file


set :application, "myblog"
set :domain, "blog.com"
set :deploy_to, "/var/rails/myblog"
set :repository", "http://svn.myserver.com/myblog/trunk"


Also add following lines to Rakefile as well to require vlad gem


Begin
require 'vlad'
Vlad.load
rescue LoadError
#do nothing
end


Just run following commands in this sequence to deploy your application


vlad:setup #run first time only
vlad:update
vlad:migrate #optional
vlad:start


As you might be aware Capistrano is not going to be maintained anymore by Jamis buck and there is lot of active development going on for vlad it should be good idea to give vlad a spin as alternative to capistrano.

DHH speaks of 5 years of ruby on rails and rails 3

DHH's keynote on Railsconf 2009 speaks about five years of ruby on rails. Rails 3 and secret of high productivity.

Tuesday, March 10, 2009

using multiple contexts block in test case with shoulda

Test::Unit which is a default unit testing framework with rails. It has only one context for each test case class. If we can have more than one context per class than it could reduce number of files and classes that we are required to make to test cases in multiple contexts.


Answer to this issue is support for multiple contexts in Shoulda. Shoulda is rspec like testing framework built upon Test::Unit. Shoulda allows you to have multiple context with thier own setup and teardown methods. For example see this context block in a test case




class UserTest < Test::Unit::TestCase
context "with user" do
setup do
@user = User.create(:login => "john", :password => "secret" )
end

should "be able to authenticate" do
assert User.authenticate("john", "secret")
end

should "return authenticated user" do
assert_equal @user, User.authenticate("john", "secret")
end
end
end


For more details on shoulda testing framework take a look at official rails wiki page

Tuesday, July 15, 2008

Install Phusion Passenger on Cent OS 5

Phusion Passenger (mod_rails/mod_rack) is an apache module that makes deployment of Ruby/Rails applications as simple as PHP applications. It does not require use of any ruby specific tools to deploy application. All you have to do is copy your source files in appropriate directories on server and your code changes will be deployed. Main advantage of passenger is it allows to deploy rails application reliably on shared hosting environment. Passenger is not available for windows. 

Lets see how to install Passenger on Cent OS. Cent OS 5.1 comes with Apache 2.2.3 installed. First of all make sure Ruby, Gems, Rails and ruby development headers is installed on the system, Issue following commands as root: 

yum install ruby
yum install irb
yum install rdoc
yum install ruby-devel
yum install rubygems
Now install rails gems:

gem install rails -y

Also install Apache development header files.

yum install httpd-devel
Now we have all prerequisites covered. Lets proceed with installation of Passenger gem.

gem install passenger
After installing passenger and its dependencies make sure that Apache is stopped, launch passenger installer for Apache module from command line.

passenger-install-apache2-module

The command line based installer is pretty good it will check all dependencies and inform you if any dependency is missing. After the module is installed. create a rails application in /var/www/htdocs folder.

rails demo
Now open httpd.conf file in /etc/httpd/conf folder. Add following lines in this file.

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.1/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.1
PassengerRuby /usr/bin/ruby

These paths could be different for your system. Now lets configure virtual host for rails application at bottom of httpd.conf. Add these lines at bottom of file:

<VirtualHost *:80>
      ServerName www.demo.com
      DocumentRoot /var/www/html/myapp/public
</VirtualHost>
Make sure DocumentRoot points to public directory of your rails application. Also add server name alias to hosts file in /etc/ folder. Now start apache server from console.

service httpd start
Your new rails application should be running now with Phusion Passenger.

Sunday, March 23, 2008

Radiant CMS 0.6 has extension now


Radiant is most promising content management system to come out from fast growing Ruby on Rails community. The Radiant site advertises it as no fluff cms intended for small teams.

Radiant is fairly simple to install as gem and has structure common to all Ruby on Rails applications. No fluff means that there is no layers of code hiding core cms code and whole application code is simple to understand and change in true open source way.

So what is keeping Radiant from replacing Joomla or Drupal as most popular open source cms out there. In my opinion at the moment the Ruby on Rails community is small (even when it is growing at stellar pace), let alone developer community around Radiant

The reason for popularity of Joomla and similar PHP open source CMS is fact that they have been around for quite some time now. There is significant developer community around these projects which have been developing and extending these CMS. There is very large number of extensions available for Joomla allowing people to quickly mash up web applications into core cms.

So most important thing for Radiant to be adopted by more teams especially ones outside Ruby on Rails community is to have popular extension mechanism and number of extensions available to extend core cms. Release 0.6 comes with extension mechanism which appears to be fairly simple and there are some extensions available as well. Lets wait and see how many members of rails community contribute extensions to this project and will it be able to become leading open source cms as Joomla

Monday, March 17, 2008

Getting started with Merb and Sequel Part 2

Continuing from my last post. My previous post is based on merb 0.5, but since then newer merb 0.9.1 is available from merbivore. There has been several changes in this release from previous version.
Ok! Now lets add another model to our application to track user status. Type this command from your application root
% merb-gen model status
This will create model named status.rb in app/models directory as well as migration file named 002_add_model_statuses.rb, Go on and edit this file to create the statuses table.
class AddModelStatuses < Sequel::Migration
def up create_table :statuses do |u|
primary_key :id
integer :user_id
varchar :status, :size => 100
text :note
datetime :time_at
end
end

def down
execute "DROP TABLE statuses"
end

end
Now that we have status table setup lets manage the user status restfully. Lets create a controller for REST enabled actions.
% merb-gen controller status
This will create a new contoller now add four actions to allow for REST protocol.
class Statuses < Application

# GET /statuses
# GET /statuses.xml
def index
@statuses = Status.filter('user_id = ?', session[:user_id])
end

# GET /statuses/1
# GET /statuses/1.xml
def show
@status = Status.filter('id = ?', params[:id])
end

# GET /statuses/new
def new

end

# GET /statuses/1;edit
def edit

end

# POST /statuses
# POST /statuses.xml
def create
Status.insert(:user_id => session[:user_id], :status => params["select_status"], :note => params["status_note"], :time_at => Time.now)
end

# PUT /statuses/1
# PUT /statuses/1.xml
def update
Status.filter('id = ?', params[:id]).update(:status => params["select_status"])
end

# DELETE /statuses/1
# DELETE /statuses/1.xml
def destroy
Status.filter(:id => params[:id]).delete
end

end
Now that we have this in controller, lets setup router.rb to use this controller restfully, Add thi s line to router.rb
r.resources :statuses
Now you can call the the statuses controller restfully from anywhere. Check out Ezra's post on Restful routes