Description
rails_best_practices is a code metric tool to check the quality of Rails code.
It supports the following ORM/ODMs:
rails_best_practices alternatives and similar gems
Based on the "Code Analysis and Metrics" category.
Alternatively, view rails_best_practices alternatives based on common mentions on social networks and blogs.
-
Rubocop
DISCONTINUED. A Ruby static code analyzer and formatter, based on the community Ruby style guide. [Moved to: https://github.com/rubocop/rubocop] -
SimpleCov
Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites -
Flog
Flog reports the most tortured code in an easy to read pain report. The higher the score, the more pain the code is in. -
Traceroute
A Rake task gem that helps you find the unused routes and controller actions for your Rails 3+ app -
Flay
Flay analyzes code for structural similarities. Differences in literal values, variable, class, method names, whitespace, programming style, braces vs do/end, etc are all ignored. -
undercover
undercover warns about methods, classes and blocks that were changed without tests, to help you easily find untested code and reduce the number of bugs. It does so by analysing data from git diffs, code structure and SimpleCov coverage reports -
bundler-leak
Known-leaky gems verification for bundler: `bundle leak` to check your app and find leaky gems in your Gemfile :gem::droplet:
Nutrient – The #1 PDF SDK Library, trusted by 10K+ developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of rails_best_practices or a related project?
README
rails_best_practices
rails_best_practices is a code metric tool to check the quality of Rails code.
It supports the following ORM/ODMs:
- activerecord
- mongoid
- mongomapper
And the following template engines:
- erb
- haml
- slim
- rabl
rails_best_practices supports Ruby 1.9.3 or newer.
External Introduction
Railscasts - #252 Metrics Metrics Metrics
Usage
At the root directory of a Rails app, run:
rails_best_practices .
Or for HTML output:
rails_best_practices -f html .
By default rails_best_practices will parse code in the vendor
, spec
, test
and features
directories.
Excluding directories
To exclude a directory simply call it with -e
or --exclude
:
rails_best_practices -e "db/migrate" .
To exclude multiple directories, separate them with comma:
rails_best_practices -e "db/migrate,vendor" .
Other command-line options
To see the full list of command-line options, run:
$ 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-vscode open file by vscode 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
-c, --config CONFIG_PATH configuration file location (defaults to config/rails_best_practices.yml)
-v, --version Show this version
-h, --help Show this message
Resources
Homepage: http://rails-bestpractices.com
GitHub: http://github.com/railsbp/rails_best_practices
Team Blog http://rails-bestpractices.com
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
Install
gem install rails_best_practices
or add it to the Gemfile
gem "rails_best_practices"
--with-sublime
Install https://github.com/asuth/subl-handler
Editor Integration
TextMate 2
If you use TextMate 2
, you can install the RailsBestPractices.tmbundle bundle.
Issues
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 encounter a NoMethodError exception, or a syntax error, you can use debug mode to discover which file is to blame:
rails_best_practices -d .
That will provide the error's stack trace and the source code of the file which is causing the error.
Custom Configuration
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: { }
#CheckDestroyReturnValueCheck: { }
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: { }
Now, at the root directory of a Rails app, run:
rails_best_practices . -c config/rails_best_practices.yml
You can remove or comment a 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'] }
Implementation
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
- Destroy return value (disabled by default)
Mailer
- Use multipart/alternative as content_type of email
Migration
- Isolating seed data
- Always add database 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 precompilation with turbo-sprockets-rails3
Other
- Remove trailing whitespace
- Remove tab (disabled by default)
- Hash syntax (disabled by default)
- Use parentheses in method definition (disabled by default)
- Long line (disabled by default)
- Not rescue exception
Write Your Own Checklist
If you want to write your own checklist (some checklist only for your Rails projects), please read this first, How to write your own check list?
Contribute
If you want to add your rails best practices into the gem, please post your best practices on http://rails-bestpractices.com
Contact Us
We provide Rails consulting services, you can contact us by Twitter or email.
Follow us on twitter: http://twitter.com/railsbp
Send us email: [email protected]
Copyright © 2009 - 2022 Richard Huang ([email protected]), released under the MIT license
*Note that all licence references and agreements mentioned in the rails_best_practices README section above
are relevant to that project's source code only.