Rails Application Refactoring Steps
Md. Masud Rana
Steps
- Database migration(field name, data type, relation etc.)
- Model association(belongs_to, has_*, property naming etc.)
- Model methods(naming, db changes etc.)
- Controller (naming, eager load, scope etc.)
- Routing(fix conventions, priority)
- Test code(fix rspec coverage)
- GIT branching
Database migration
- Add index to foreign_keys
- Rename table or fields by a name which describe the property itself
- Fix naming typos
- Use self referential relation instead of saving type of record in a field
Model
- Fix model associations
- Add dependant options to relational model
- Use find_by method instead of where to retrieve a single record
- Use rubocop gem to watch and fix convention mistakes
- Specify instance method and class method
- Move private methods under private block
Controller
- Eager load relational model to avoid n + 1 query where necessary
- Move model specific constant declaration to the model class
- Move private methods under private block
- Use rubocop gem to fix watch and fix convention mistakes
- Use before_action callback to restrict unauthorized access
Routing
- Additional RESTful routes should be added under collection and member
block
- Fix typo mistakes
- Don't use string and symbol both to define routes
- Fix routing priorities
Views
- Fix view script inconsistencies (try haml_lint gem to follow haml best
practice)
- Fix model layer directly access in view
- Remove business logic from views
- Write view helper method for custom view component
- Use shared partial to reuse common view templates
- * Use #find_each instead of #each when looping through a collection of
records. #each, all records will be loaded into memory. #find_each,
records will be loaded into memory in batches of the given batch size
Assets
- Replace downloaded libraries by gem for JS and CSS
- Remove unused CSS and JS files from assets directory
- Use SCSS to write application specific custom CSS
- Avoid writing custom CSS when using a CSS framework(ex- twitter
bootstrap, foundation etc.)
- Use $(document).ready function to write JS when using jQuery
- Bind DOM component using $(document).on(‘event’, ‘dom’, callback() { })
instead of $(‘dom’).event_name()
Active admin
- Update active admin registered controller as per model, migration etc.
changes. (ex - user model check_in, check_out time etc.)
- Add and implemented missing features, It would be useful for admin.
Note: If your application has active admin integration.
Test code
- Fix broken test codes (model, controller, route etc.)
- Write missing test coverage
- Improve and refactoring of existing test codes
GIT branching
- Merge feature branches to development branch
- Merge hotfix branches to master and development branch
- Remove feature and hotfix etc. branches from both local and remote
repository.
- Create release-x.x.x branch for each major deployment
- Use tags to create release branch
- Never remove release branches
Git branching example
Data migration
- Backup production database
- Migrate existing data as per DB schema changes
- Run application locally after data migration
- Fix issues if any
- Create new database in production DB server
- Import migrated database in production DB server
Deployment
- Remove environment specific confidential data (ex- smtp config etc.)
- Set environment specific data to ENV variable
- Symlink configuration files in deploy.rb (ex- application.yml etc.)
- Add environment specific config file in .gitignore
- Keep every file in config/ directory as an example file(ex-
application.example.yml)
- Cleanup releases (cap staging deploy:cleanup -s keep_releases=10)
Publish to github as an open source project
- Remove all configuration and use ENV to set environment specific config.
- Mailer smtp configuration
- Add a license
- Prepare a readme
- Add some screenshot
Thanks

Rails application refactoring steps

  • 1.
    Rails Application RefactoringSteps Md. Masud Rana
  • 2.
    Steps - Database migration(fieldname, data type, relation etc.) - Model association(belongs_to, has_*, property naming etc.) - Model methods(naming, db changes etc.) - Controller (naming, eager load, scope etc.) - Routing(fix conventions, priority) - Test code(fix rspec coverage) - GIT branching
  • 3.
    Database migration - Addindex to foreign_keys - Rename table or fields by a name which describe the property itself - Fix naming typos - Use self referential relation instead of saving type of record in a field
  • 4.
    Model - Fix modelassociations - Add dependant options to relational model - Use find_by method instead of where to retrieve a single record - Use rubocop gem to watch and fix convention mistakes - Specify instance method and class method - Move private methods under private block
  • 5.
    Controller - Eager loadrelational model to avoid n + 1 query where necessary - Move model specific constant declaration to the model class - Move private methods under private block - Use rubocop gem to fix watch and fix convention mistakes - Use before_action callback to restrict unauthorized access
  • 6.
    Routing - Additional RESTfulroutes should be added under collection and member block - Fix typo mistakes - Don't use string and symbol both to define routes - Fix routing priorities
  • 7.
    Views - Fix viewscript inconsistencies (try haml_lint gem to follow haml best practice) - Fix model layer directly access in view - Remove business logic from views - Write view helper method for custom view component - Use shared partial to reuse common view templates - * Use #find_each instead of #each when looping through a collection of records. #each, all records will be loaded into memory. #find_each, records will be loaded into memory in batches of the given batch size
  • 8.
    Assets - Replace downloadedlibraries by gem for JS and CSS - Remove unused CSS and JS files from assets directory - Use SCSS to write application specific custom CSS - Avoid writing custom CSS when using a CSS framework(ex- twitter bootstrap, foundation etc.) - Use $(document).ready function to write JS when using jQuery - Bind DOM component using $(document).on(‘event’, ‘dom’, callback() { }) instead of $(‘dom’).event_name()
  • 9.
    Active admin - Updateactive admin registered controller as per model, migration etc. changes. (ex - user model check_in, check_out time etc.) - Add and implemented missing features, It would be useful for admin. Note: If your application has active admin integration.
  • 10.
    Test code - Fixbroken test codes (model, controller, route etc.) - Write missing test coverage - Improve and refactoring of existing test codes
  • 11.
    GIT branching - Mergefeature branches to development branch - Merge hotfix branches to master and development branch - Remove feature and hotfix etc. branches from both local and remote repository. - Create release-x.x.x branch for each major deployment - Use tags to create release branch - Never remove release branches
  • 12.
  • 13.
    Data migration - Backupproduction database - Migrate existing data as per DB schema changes - Run application locally after data migration - Fix issues if any - Create new database in production DB server - Import migrated database in production DB server
  • 14.
    Deployment - Remove environmentspecific confidential data (ex- smtp config etc.) - Set environment specific data to ENV variable - Symlink configuration files in deploy.rb (ex- application.yml etc.) - Add environment specific config file in .gitignore - Keep every file in config/ directory as an example file(ex- application.example.yml) - Cleanup releases (cap staging deploy:cleanup -s keep_releases=10)
  • 15.
    Publish to githubas an open source project - Remove all configuration and use ENV to set environment specific config. - Mailer smtp configuration - Add a license - Prepare a readme - Add some screenshot
  • 16.