Wednesday, November 25, 2009

Jquery datagrid for rails applications


With jquery grid plugin along with 2dcJqgrid Rails plugin are awesome for creating datagrids in rails applications which allows users to navigate, search, add/ edit / delete operation in one place.
See the sample screenshot above.

Rails Developement Standards

Application must be under version control. Use SVN
Never put log files or database.yml file into subversion.
Use either Postrgres, Oracle or Mysql for your db.
Session data should be stored in the database
Migrations should NOT contain any db specific SQL.
Use Rspec for testing. Rspec is available as plugin
Application must have Unit and Functional tests.
All application gems should be installed in RAILS_ROOT/vendor/gems folder
Always freeze rails, ( Fix For rails freeze gems )
If your application uses CAS authentication, use the ucb_rails_security plugin
Controller Methods should be no longer than 5-8 lines of code. If you find your self with really large controller methods you are probably violating the following standard.
Application domain logic should go into the model, NOT in the controller or view.
Try to limit each controller's actions to: index, new, create, edit, update, destroy. This is not always possible. However, if you notice that a given controller has more than 10 methods, it probably time to refactor.

Preventing open a new window for each folder in linux

1. Double-click on the "Computer" icon on your desktop.
2. Go to the "Edit" menu and choose "Preferences."
3. Click on the "Behavior" tab.
4. Put a check in the box marked "Always open in browser windows

Sending Tons of emails from your Ruby on Rails Application

A correct way to send tons of massive emails from your website is queuing the emails and send them one by one. If you send all at once directly from your site some hosting servers may allows only some amount of emails per hour. Shared hosting servers allow you send only 25 emails from your site per hour. Fortunately there is one plugin available for Rails which can store the outgoing emails in database as a queue and send them eventually. That plugin is called ar_mailer you can install it and send tons of emails happlily. See the process of how to install and more at http://www.ameravant.com/posts/sending-tons-of-emails-in-ruby-on-rails-with-ar_mailer

Ruby on Rails Easy roles plugin

Easy roles is a plugin for Ruby on Rails which is very usefull for your site if it has lot of roles. So by using this plugin you can restrict the access to controllers based on the roles. Follow the below process to install it for your application.

cd yourappname

ruby script/plugin install git://github.com/platform45/easy_roles.git

Now the plugin is installed in your vendor/plugins folder

Below is the Basic Setup process you need to do after plugin installed successfully
Add the following to your enviroment.rb in the rails initializer block
config.gem 'easy_roles', :source => 'http://gemcutter.org'

Add a "roles" column to your users model, and set the default value to "--- []". Please note you can call this column anything you like, I like to use the name "roles" for meaning full.

For users table migration table add below
t.string :roles, :default => "--- []"

Then you need to add "easy_roles :column_name" and some logic to your model.


class User < ActiveRecord::Base

# Serialize roles as an array
serialize :roles, Array

# Create an empty roles array on create
before_validation_on_create :make_default_roles

# Convenience method, is user an admin?
def admin?
has_role?("admin")
end

# Checks to see if a user has requested role
def has_role?(role)
roles.include?(role)
end

# Add a role to a user
def add_role(role)
self.roles << role
end

# Remove a role from a user
def remove_role(role)
self.roles.delete(role)
end

# Clear all users roles
def clear_roles
self.roles = []
end

private
def make_default_roles
clear_roles if roles.nil?
end
end

And thats it.
The above model gives you the options like below in controllers
@user = User.first
@user.add_role 'admin'
@user.save!

@user.has_role? 'admin'
=> true

@user.admin?
=> true

@user.remove_role 'admin'
@user.save!

@user.admin?
=> false

Usage
Easy roles extends your model, and adds a few methods needed for basic role authorization.

adding a role to a user
add_role 'role'

removing a role from a user
remove_role 'role'

check to see if a user has a certain role
has_role? 'role'
# or
is_role? # role being anything you like, for example 'is_admin?' or 'is_awesome?'

== Examples

@user = User.first

@user.add_role 'admin'

@user.is_admin?
=> true

@user.has_role? 'admin'
=> true

@user.is_awesome?
=> false

@user.add_role 'awesome'

@user.is_awesome?
=> true

@user.remove_role 'admin'

@user.is_admin?
=> false

etc etc

== Protecting controllers

There are many ways to implement views for specific roles, so I did not specifically supply one. Here's an example on what you could do:

class ApplicationController < ActionController::Base

def admin_required
unless current_user && current_user.is_admin?
flash[:error] = "Sorry, you don't have access to that."
redirect_to root_url and return false
end
end

end

Then in your AdminsController or any controller that you only want admins to view:

class AdminsController < ApplicationController
before_filter :admin_required
end

class MarksController < ApplicationController
before_filter :admin_required, :only => :create, :update
end

check out more information at
http://blog.platform45.com/2009/10/05/howto-basic-roles-for-users for implementation

World's Famous IDE eclipse's support for PHP

Hi,
If you already installed eclipse in your system then you have to update it for php support. To do so follow the below steps
1. Open the eclipse IDE
2. Then goto menu Help -> Software updates
There you see Two main tabs Installed softwares and Available softwares. If phpeclipse is not there in installed softwares list then check in Available softwares list. If not avaliable there also Then click on Add site and enter below url.
http://update.phpeclipse.net/update/stable/1.2.x
Then that eclipse's update url is added to available softwares list. Check that module and click install. It will take few minutes and install phpeclipse and PDT (PHP Development Tools) to your existing eclipse IDE.
After installed successfully Goto menu Window -> open perspective
and select PHP to start working on PHP Projects.

The process is same to enable any other technologies support in eclipse for developing applications.

If you are not installed eclipse IDE in your system then you can directly install it from the official website http://www.eclipse.org/