Refactoring of an existing application is always ignored because of complexity and time limitation. Here I tried to list down common mistakes during development which should be considered if you have refactoring plan.
2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
8. 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()
9. 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.
10. Test code
- Fix broken test codes (model, controller, route etc.)
- Write missing test coverage
- Improve and refactoring of existing test codes
11. 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
13. 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
14. 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)
15. 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