Veerasundaravel's Ruby on Rails Weblog

February 23, 2010

Check list for Rails application

Filed under: Ruby On Rails — Tags: , , , , , , , , — Veerasundaravel @ 11:34 am

I just surfed through google for good optimized and general check list for a rails application, here I listed some useful tips which will help you lot.

Code formatting:

1. Indentation – The blank space between a margin and the beginning of an indented line. Use 2 space indent, no tabs.

2. Add required comments and reference links where ever possible.

3. Remove unwanted spaces and new lines. That will reduce the size of file and give better readability.

* Use spaces around operators, after commas, colons and semicolons,
around { and before }.

* No spaces after (, [ and before ], ).

* Use two spaces before statement modifiers (postfix
if/unless/while/until/rescue).

* Use an empty line before the return value of a method (unless it
only has one line), and an empty line between defs.

* Use empty lines to break up a long method into logical paragraphs.

* Keep lines fewer than 80 characters.

* Avoid trailing whitespace.

http://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=RubyCodingConvention

General App:

1. Try to use maximum of scaffold to generate resources(model, controllers, views)

2. Never put log files or database.yml file into version control systems(git/svn).

3. Never put temporary files and cache files like files under /tmp folder etc into version control systems(git/svn).

4. Unpack all required gems and freeze rails gems.

5. Session data should be stored in the database.

6. Application must have Unit and Functional tests.

7. Use Rspec for testing. Rspec is available as plugin

8. Controller Methods should be no longer than 5-8 lines of code.

9. 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.

http://www.rorexperts.com/rails-application-development-standards-t1302.html

10. Use custom configuration files

11. Use constants for repeated strings like APP_DOMAIN = ‘mydomain.com’

12. Keep time in UTC format.

13. Keep your controllers and view files Skinny.

Database and Model:

1. Use indexing for applicable table columns.

2. Store big size of data like images contents and bigger text in separate table.

3. Always don’t select all the columns from your table, when it is not required.

4. Design your table with minimum number of columns.

5. Avoid eager loading of active records.

6. Always use model associations to create associated records etc.

7. Use generic names for model association, for example users in our application is going to use email optoins.

Models: User-UserMessage-Message

so the associations will look below is not a good name
user has_many :messages
message belongs_to :user

instead of above try the association names like below:
user has_many :sent_messages
user has_many :received_messages
message belongs_to :sender
message has_many :receivers

8. Try to include associated table in your find query, if you want to use them.

General – Extended

1. Readme file should be application specific and mentions the functionality and usage information
2. Automated deployment with capistrano wherever possible
3. log all exceptions
4. expose API for all models with admin credentials, this would make it easy to populate the data
5. send a release notes for each deployment

Advertisements

February 15, 2010

Rails3 Beta installation

This post will explain how I proceeded with latest beta version Rails3 and the issues I faced.

My current version of rails and ruby:

veera@client25:~$ rails -v
Rails 2.3.5
veera@client25:~$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
veera@client25:~$

trying to install beta version of rails3.

gem install tzinfo builder i18n memcache-client rack  rake rack-test rack-mount erubis mail text-format thor bundler
gem install rails –pre

I forget to add sudo, so that I got so many errors. Let me try with sudo.

Ok some what better am getting error message only while installing generating documentation for builder-2.1.2

gem install rails –pre – was not successfull. As it haven’t installed rails3 version as I expected.

Getting error like below:

WARNING:  RubyGems 1.2+ index not found for:

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 1 gems from http://gems.rubyforge.org/

surfed google for the error, http://bparanj.blogspot.com/2008/11/warning-rubygems-12-index-not-found-for.html

lemme try the idea mentioned in that

veera@client25:~$ sudo gem sources -a http://gems.github.com
http://gems.github.com added to sources
veera@client25:~$

or else remove all the gem sources from you location, so that it can any of the source directly from net

I assume installing sudo gem install rack-mount –version=0.4.0, solved my issue.

Now I can see that rails3 is installed in my machine.

after this try the following options:

$ sudo gem install tzinfo builder memcache-client rack rack-test erubis mail text-format bundler thor i18n
$ sudo gem install rack-mount –version=0.4.0
$ sudo gem install rails –pre
$ rails -v
Rails 3.0.0.beta

then create the app in rails3 version

rails RailsBeta3

I can find a new file application.rb under config directory. Seems we have to use it instead of environment.rb

* Issues found:

1. Couldn’t find support for postgresql with Rails3.
2. Not able to start the rails app using mongrel_rails start.

My initial scaffold is working fine. rails generate scaffold group title:string description:text

Referred http://www.slideshare.net/GreggPollack/rails-3-beautiful-code-3219240 for the updated methods and options in Rails3.l,
Cool, simply and effective slide show about Rails3. Some of the important points from that:

1. ruby script/* is modified as rails(ex ruby script/server -> rails)
2. amazing and simplified routes. map.resources is modified as resources and lot more.
3. Improved syntax for respond_to method.
4. Simplified action mailer option.
5. Active record syntax is also modified, Person.all(:conditions=>”blah blah) to Person.where(“blah blah”)
6. form_remote_tag, link_to_remote tag is also simplified and the respective html source is also looks very simple. (no more onclick”new Ajax request ………..”)

looking for few more documents for the modified methods in rails3.

Tried to generate a gem in Rails3.

restful-authentication plugin is not working in rails – May be auth_logic plugin will work.

Grrrrrrr, I can’t create a older version of rails app  – rails _2.3.5_ blog235

/usr/local/lib/site_ruby/1.8/rubygems.rb:827:in `report_activate_error’: RubyGem version error: railties(3.0.0.beta not = 2.3.5) (Gem::LoadError)
from /usr/local/lib/site_ruby/1.8/rubygems.rb:261:in `activate’
from /usr/local/lib/site_ruby/1.8/rubygems.rb:68:in `gem’
from /usr/bin/rails:18

Hope rails3 alpha version should have fix for this. So now am reverting back to old version of rails by simply uninstalling all the gems related to rails3.

%d bloggers like this: