Thursday, November 26, 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/



Tuesday, November 24, 2009

File permissions in Linux

Hi,
By using chown command through terminal you can give file permissions to the users in linux operating systems without login as root. By default you are not able to access some folders in the linux os due to the security reasons. But sometimes you need to access some folders. Ex: If you want to run php files you need to place the files in the folder called /var/www/html but other than root account no one have write permission to this folder.

So if you want to enable write permissions to that folder for your account simply
open the terminal as
# applications > system tools > Terminal
enter su in the terminal to login as root
#su
it will ask you the root password, give the root password
then run the below command
#chown username /var/www/html/

Now you have the write permissions to that folder.

Flash Plugin for Firefox in Fedora11 OS

To enable flash player support for your firebox browser in Linux operating system goto the abode site using below link
http://labs.adobe.com/downloads/flashplayer10.html

and download the linux version tar file uncompress the tar file and Copy the file libflashplayer.so in to the folder /usr/lib/mozilla/plugins.

That's it you just need to restart the firefox by File -> Exit and see your firefox is now enabled with flash support.

Tuesday, November 17, 2009

Youtube clone

Hi Friends, If you are a PHP Mysql Developer then this post is exactly for you. You can develop youtube alike site with your existing knowledge. Just goto the phpmotion site at http://www.phpmotion.com/ and download the source and unzip it and upload to the hosting server. Then run that through browser. It asks you the necessary settings, after giving all details the youtube like site owns you. See the demo http://demo.phpmotiontemplates.com/. I used this script for creating media sharing site. It works more than as youtube site.

Friday, November 6, 2009

Googlemap show Directions between two places

Place the below js code in head tag
var map;
var directionsPanel;
var directions;
function initialize() {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(42.351505,-71.094455),12);
directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
directions.load("from:kakinada,india to:Kurnool,india");
}
Place the two divs in html body with ids map_convas, route
Call onload="initialize()" onunload="GUnload()" in body tag.
directions.load("from:kakinada,india to:Kurnool,india");
here you you can also place lat,long pairs in place of from and two address

Some usefull sites for Ruby on Rails newbies

From http://api.rubyonrails.org/
study all activerecord, actionview and actioncontroller classes
Ex: classes -> active record::call backs
which explains what action done before or after creating a record in database or before deleting a record from one table what needs to done in other tables etc

Ruby hashes. Means associatinve arrays in php. Each values is associated with one key like $array=new array(‘color’ => ‘whilte’ , ‘Height’ => ‘6 feet’)
http://www.rubyonrailsexamples.com/category/ruby-hash/

Validation classes and lot more
http://apidock.com/rails/ActiveRecord/Validations/ClassMethods
http://apidock.com/rails/ActiveRecord/Base
All methods and available for active record like save update delete etc
*********************************

Find Some things to look at
http://www.palmcoder.net/?p=265

Tutorials
Rails ajax (Implementing remote call is very easy in RoR)
http://onlamp.com/onlamp/2005/06/09/rails_ajax.html

Link_to_remote with passing textbox value as a parameter
http://onlamp.com/onlamp/2005/06/09/rails_ajax.html

Forum

http://railsforum.com

Check username availablility
http://railsforum.com/viewtopic.php?id=22828

Gems
http://ariejan.net/gems/

http://www.digitalmediaminute.com/article/1816/top-ruby-on-rails-tutorials

Testing
http://www.tutorialspoint.com/ruby-on-rails-2.1/rails-unit-testing.htm

Ruby on Rails Easy Deployment

Actually Ruby on Rails deployment is little bit tricky compare to PHP. PHP programs tend to require less memory to run so they tend to be easier to deploy. With the release of mod_rails and Ruby Enterprise Edition Rails becomes easier to deploy than PHP once the initial Apache add-ons are made. See from below site for full details
http://b.lesseverything.com/2008/8/25/comparing-php-to-ruby-on-rails

Pagination gem for Ruby on Rails

For ruby on rails there is a gem available for pagination called will_paginate to provide pagination to the database results. Process to install this gem to your application.
1. rake gems:install
2. gem sources -a http://gems.github.com
3. gem install mislav-will_paginate
For the first time to install gems package run step 1. If you already run this command no need to run again. step 2 is to append the gems to your rails. Step 3 will install the will_paginate gem to your application.

Usage:
After installed gem successfully follow the below steps to enable pagination to your models.

1. In your config\environment.rb file add
Rails::initializer.run do |config|
config.gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'
end


2. rake gems:install

3. In your model, mention how many records display per page

def self.per_page
5
end

4. In your controller, replace
@yourmodels = Yourmodel.find
with
@yourmodels = Yourmodel.paginate :page => params[:page], :order => 'created_at DESC'
5. In your index view, add below to see the pagination
<%= will_paginate @models %>

see http://localhost:3000/models
-----------------------End-------------------------------------------