public
Description: rails_best_practices is a gem to check quality of rails app files according to ihower’s presentation from Kungfu RailsConf in Shanghai China
Homepage:
Clone URL: git://github.com/flyerhzm/rails_best_practices.git
Click here to lend your support to: rails_best_practices and make a donation at www.pledgie.com !
name age message
file .gitignore Thu Nov 05 06:13:00 -0800 2009 update rails_best_practices.gemspec [flyerhzm]
file LICENSE Tue Nov 03 05:50:24 -0800 2009 first commit [Richard]
file README.textile Thu Nov 19 19:42:36 -0800 2009 add Issue section in README [Richard]
file Rakefile Mon Nov 09 00:43:33 -0800 2009 add dependency version [Richard]
file VERSION Fri Nov 27 19:31:41 -0800 2009 Version bump to 0.3.10 [flyerhzm]
directory bin/ Wed Nov 04 22:38:23 -0800 2009 make rails_best_parctices executable [Richard]
directory lib/ Fri Nov 27 19:31:33 -0800 2009 only care about self.up in always_check_db_inde... [flyerhzm]
file rails_best_practices.gemspec Fri Nov 27 19:31:46 -0800 2009 Regenerated gemspec for version 0.3.10 [flyerhzm]
file rails_best_practices.yml Wed Nov 18 21:14:03 -0800 2009 rename use_filter_check to use_before_filter_check [Richard]
directory spec/ Fri Nov 27 19:31:33 -0800 2009 only care about self.up in always_check_db_inde... [flyerhzm]
README.textile

rails_best_practices

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.


Resources

ihower’s presentation
Wiki


Install


sudo gem install rails_best_practices --source http://gemcutter.org

Usage

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

Issue

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.


Customize Configuration

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: { }
MoveCodeIntoHelperCheck: { }
ReplaceInstanceVariableWithLocalVariableCheck: { }

Progress

  • Lesson 1. Move code from Controller to Model
    1. Move finder to named_scope
    2. Use model association
    3. Use scope access
    4. Add model virtual attribute
    5. Use model callback
    6. Replace Complex Creation with Factory Method
    7. Move Model Logic into the Model
    8. model.collection_model_ids (many-to-many)
    9. Nested Model Forms (one-to-one)
    10. Nested Model Forms (one-to-many)
  • Lesson 2. RESTful Conventions
    1. Overuse route customizations
    2. Needless deep nesting
    3. Not use default route
  • Lesson 3. Model
    1. Keep Finders on Their Own Model
    2. Love named_scope # same as Move finder to named_scope
    3. the Law of Demeter
    4. DRY: metaprogramming
    5. Extract into Module
    6. Extract to composed class
    7. Use Observer
  • Lesson 4. Migration
    1. Isolating Seed Data
    2. Always add DB index
  • Lesson 5. Controller
    1. Use before_filter
    2. DRY Controller # not implement, use http://github.com/josevalim/inherited_resources
  • Lesson 6. View
    1. Move code into controller
    2. Move code into model
    3. Move code into helper
    4. Replace instance variable with local variable
    5. Use Form Builder # not implement, use http://github.com/justinfrench/formtastic
    6. Organize Helper files # not implement, it’s rails default behaviour

Copyright © 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license