rails_best_practices is a code metric tool to check the quality of rails codes.
At the root directory of rails app
rails_best_practices .
or html output
rails_best_practices -f html .
By default rails_best_practices will do parse codes in vendor, spec, test and features directories. If you need, see the command options:
$ rails_best_practices -h
Usage: rails_best_practices [options]
-d, --debug Debug mode
-f, --format FORMAT output format
--without-color only output plain text without color
--with-textmate open file by textmate in html format
--with-mvim open file by mvim in html format
--vendor include vendor files
--spec include spec files
--test include test files
--features include features files
-x, --exclude PATTERNS Don't analyze files matching a pattern
(comma-separated regexp list)
-g, --generate Generate configuration yaml
-v, --version Show this version
-h, --help Show this message
Homepage: http://rails-bestpractices.com
Github: http://github.com/flyerhzm/rails_best_practices
RDoc: http://rdoc.rails-bestpractices.com
Team Blog http://rails-bestpractices.com/blog/posts
Google Group: https://groups.google.com/group/rails_best_practices
Wiki: http://github.com/flyerhzm/rails_best_practices/wiki
Issue Tracker: http://github.com/flyerhzm/rails_best_practices/issues
Support rails_best_practices at Pledgie
gem install rails_best_practices
If you install the rails_best_practices with bundler-installed github-sourced gem, please use the following command instead.
bundle exec rails_best_practices .
If you got NoMethodError or any syntax error, you should use debug mode to detect which file rails_best_practices is parsing and getting the error.
rails_best_practices -d .
Then give me the error stack and the source code of the file that rails_best_practices is parsing error.
First run
rails_best_practices -g
to generate rails_best_practices.yml
file.
Now you can customize this configuration file, the default configuration is as follows:
MoveFinderToNamedScopeCheck: { }
UseModelAssociationCheck: { }
UseScopeAccessCheck: { }
AddModelVirtualAttributeCheck: { }
ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
MoveModelLogicIntoModelCheck: { use_count: 4 }
OveruseRouteCustomizationsCheck: { customize_count: 3 }
NeedlessDeepNestingCheck: { nested_count: 2 }
NotUseDefaultRouteCheck: { }
KeepFindersOnTheirOwnModelCheck: { }
LawOfDemeterCheck: { }
UseObserverCheck: { }
IsolateSeedDataCheck: { }
AlwaysAddDbIndexCheck: { }
UseBeforeFilterCheck: { }
MoveCodeIntoControllerCheck: { }
MoveCodeIntoModelCheck: { use_count: 2 }
MoveCodeIntoHelperCheck: { array_count: 3 }
ReplaceInstanceVariableWithLocalVariableCheck: { }
DryBundlerInCapistranoCheck: { }
UseSayWithTimeInMigrationsCheck: { }
UseQueryAttributeCheck: { }
RemoveTrailingWhitespaceCheck: { }
UseMultipartAlternativeAsContentTypeOfEmailCheck: {}
You can remove or comment one review to disable it, and you can change the options.
Move code from Controller to Model
- Move finder to named_scope (rails2 only)
- Use model association
- Use scope access
- Add model virtual attribute
- Replace Complex Creation with Factory Method
- Move Model Logic into the Model
RESTful Conventions
- Overuse route customizations
- Needless deep nesting
- Not use default route
Model
- Keep Finders on Their Own Model (rails2 only)
- the Law of Demeter
- Use Observer
- Use Query Attribute
Mailer
- Use multipart/alternative as content_type of email
Migration
- Isolating Seed Data
- Always add DB index
- Use Say with Time in Migrations
Controller
- Use before_filter
View
- Move code into controller
- Move code into model
- Move code into helper
- Replace instance variable with local variable
Deployment
- Dry bundler in capistrano
Other
- Remove Trailing Whitespace
If you want to add your rails best practices into the gem, please post your best practices on http://rails-bestpractices.com
We provide rails consulting services, you can contact us by twitter or email.
Follow us on twitter: http://twitter.com/railsbp
Send us email: team@rails-bestpractices.com
Copyright © 2009 - 2011 Richard Huang (flyerhzm@gmail.com), released under the MIT license