rails_best_practices is a code metric tool to check the quality of rails codes.
it supports following ORM/ODMs:
- activerecord
- mongoid
- mongomapper
following template engines:
- erb
- haml
- slim
- rabl
rails_best_practices works well only in ruby 1.9.3, 2.0.0, and 2.1.0 so far. It is incompatible with ruby 1.8.
Railscasts - #252 Metrics Metrics Metrics
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.
To exclude folder simply call it with -e or --exclude
rails_best_practices -e "db/migrate" .
To exclude multiple folders, separate them with comma
rails_best_practices -e "db/migrate,vendor" .
To see full list of commandline options call:
$ 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-sublime open file by sublime in html format (requires https://github.com/asuth/subl-handler)
--with-mvim open file by mvim in html format
--with-github GITHUB_NAME open file on github in html format. GITHUB_NAME is like railsbp/rails-bestpractices OR full URL to GitHub:FI repo
--with-hg display hg commit and username, only support html format
--with-git display git commit and username, only support html format
--template TEMPLATE customize erb template
--output-file OUTPUT_FILE output html file for the analyzing result
--silent silent mode
--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)
-o, --only PATTERNS analyze files only 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
Online Service: http://railsbp.com
Github: http://github.com/railsbp/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/railsbp/rails_best_practices/wiki
Issue Tracker: http://github.com/railsbp/rails_best_practices/issues
rails_best_practices gem is rewritten based on ripper instead of ruby_parser to support ruby 1.9 new syntax.
gem install rails_best_practices
or add in Gemfile
gem "rails_best_practices"
Install https://github.com/asuth/subl-handler
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:
AddModelVirtualAttributeCheck: { }
AlwaysAddDbIndexCheck: { }
#CheckSaveReturnValueCheck: { }
DefaultScopeIsEvilCheck: { }
DryBundlerInCapistranoCheck: { }
#HashSyntaxCheck: { }
IsolateSeedDataCheck: { }
KeepFindersOnTheirOwnModelCheck: { }
LawOfDemeterCheck: { }
#LongLineCheck: { max_line_length: 80 }
MoveCodeIntoControllerCheck: { }
MoveCodeIntoHelperCheck: { array_count: 3 }
MoveCodeIntoModelCheck: { use_count: 2 }
MoveFinderToNamedScopeCheck: { }
MoveModelLogicIntoModelCheck: { use_count: 4 }
NeedlessDeepNestingCheck: { nested_count: 2 }
NotRescueExceptionCheck: { }
NotUseDefaultRouteCheck: { }
NotUseTimeAgoInWordsCheck: {}
OveruseRouteCustomizationsCheck: { customize_count: 3 }
ProtectMassAssignmentCheck: {}
RemoveEmptyHelpersCheck: {}
#RemoveTabCheck: {}
RemoveTrailingWhitespaceCheck: { }
RemoveUnusedMethodsInControllersCheck: { except_methods: [] }
RemoveUnusedMethodsInHelpersCheck: { except_methods: [] }
RemoveUnusedMethodsInModelsCheck: { except_methods: [] }
ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
ReplaceInstanceVariableWithLocalVariableCheck: { }
RestrictAutoGeneratedRoutesCheck: { }
SimplifyRenderInControllersCheck: {}
SimplifyRenderInViewsCheck: {}
#UseBeforeFilterCheck: { customize_count: 2 }
UseModelAssociationCheck: { }
UseMultipartAlternativeAsContentTypeOfEmailCheck: {}
UseObserverCheck: { }
#UseParenthesesInMethodDefCheck: {}
UseQueryAttributeCheck: { }
UseSayWithTimeInMigrationsCheck: { }
UseScopeAccessCheck: { }
UseTurboSprocketsRails3Check: { }
You can remove or comment one review to disable it, and you can change the options.
You can apply the ignored_files
option on any rule by giving a regexp or array of regexps describing the path of the files you don't want to be checked:
DefaultScopeIsEvilCheck: { ignored_files: 'user\.rb' }
LongLineCheck: { max_line_length: 80, ignored_files: ['db/migrate', 'config/initializers'] }
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
- Check return value of "save!"
RESTful Conventions
- Overuse route customizations
- Needless deep nesting
- Not use default route
- Restrict auto-generated routes
Model
- Keep finders on their own model (rails2 only)
- the law of demeter
- Use observer
- Use query attribute
- Remove unused methods in models
- Protect mass assignment
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 (disabled by default)
- Simplify render in controllers
- Remove unused methods in controllers
Helper
- Remove empty helpers
- Remove unused methods in helpers
View
- Move code into controller
- Move code into model
- Move code into helper
- Replace instance variable with local variable
- Simplify render in views
- Not use time_ago_in_words
Deployment
- Dry bundler in capistrano
- Speed up assets precompile with turbo-sprockets-rails3
Other
- Remove trailing whitespace
- Remove tab (disabled by default)
- Hash syntax (disabled by default)
- Use parentheses in method def (disabled by default)
- Long line (disabled by default)
- Not rescue exception
If you want to write your own check list (some check list only for your rails projects), please read this first, How to write your own check list?
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@railsbp.com
Copyright © 2009 - 2013 Richard Huang (flyerhzm@gmail.com), released under the MIT license