rails_best_practices is a gem to check quality of rails app files according to ihower’s presentation Rails Best Practices from Kungfu RailsConf in Shanghai China.
rails_best_practices is a code static parser tool.
sudo gem install rails_best_practices --source http://gemcutter.org
At the root directory of rails app
rails_best_practices .
notice the period at the end, it can be the relative or absolute path of your rails app.
And default rails_best_practices will do parse codes in vendor, spec, test and stories directories. If you need, see the command options:
$ rails_best_practices -h
Usage: rails_best_practices [options]
-d, --debug Debug mode
--vendor include vendor files
--spec include spec files
--test include test files
--stories include stories files
-h, --help Show this message
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.
Copy rails_best_practices.yml
in the root directory of rails_best_practices gem to config
directory
Now you can customize the configuration, the default configuration is as follows:
MoveFinderToNamedScopeCheck: { }
UseModelAssociationCheck: { }
UseScopeAccessCheck: { }
AddModelVirtualAttributeCheck: { }
# UseModelCallbackCheck: { }
ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
MoveModelLogicIntoModelCheck: { called_count: 4 }
# ManyToManyCollectionCheck: { }
# NestedModelFormsCheck: { }
OveruseRouteCustomizationsCheck: { customize_count: 3 }
NeedlessDeepNestingCheck: { nested_count: 2 }
NotUseDefaultRouteCheck: { }
KeepFindersOnTheirOwnModelCheck: { }
LawOfDemeterCheck: { }
UseObserverCheck: { }
IsolateSeedDataCheck: { }
AlwaysAddDbIndexCheck: { }
UseBeforeFilterCheck: { }
MoveCodeIntoControllerCheck: { }
MoveCodeIntoModelCheck: { }
MoveCodeIntoHelperCheck: { array_count: 3 }
ReplaceInstanceVariableWithLocalVariableCheck: { }
- Lesson 1. Move code from Controller to Model
Move finder to named_scopeUse model associationUse scope accessAdd model virtual attribute- Use model callback
Replace Complex Creation with Factory MethodMove Model Logic into the Model- model.collection_model_ids (many-to-many)
- Nested Model Forms (one-to-one)
- Nested Model Forms (one-to-many)
- Lesson 2. RESTful Conventions
Overuse route customizationsNeedless deep nestingNot use default route
- Lesson 3. Model
Keep Finders on Their Own ModelLove named_scope# same as Move finder to named_scopethe Law of Demeter- DRY: metaprogramming
- Extract into Module
- Extract to composed class
Use Observer
- Lesson 4. Migration
Isolating Seed DataAlways add DB index
- Lesson 5. Controller
Use before_filterDRY Controller# not implement, use http://github.com/josevalim/inherited_resources
- Lesson 6. View
Move code into controllerMove code into modelMove code into helperReplace instance variable with local variableUse Form Builder# not implement, use http://github.com/justinfrench/formtasticOrganize Helper files# not implement, it’s rails default behaviour
http://railsenvy.com/2009/12/07/rails-envy-podcast-episode-102
http://ruby5.envylabs.com/episodes/29-episode-28-november-17-2009
Copyright © 2009 Richard Huang ([email protected]), released under the MIT license