CSC/ECE 517 Spring 2019 - E1919 CodeClimate Issues
CSC/ECE 517 Spring 2019 / E1919. Fix Code Climate issues in models with names beginning with ‘H’ thru ‘Sc
This wiki page is for the description of changes made according to the specification of E1919, OSS project. --Sbhadau 21:53, 25 March 2019 (EDT)
About Expertiza
Expertiza is a platform where assignments and related quizzes are managed. On this portal instructor upload assignments/questionnaires, create list of topics for students and edit the existing assignments. Students can form teams, work on projects and submit their assignments as URL or files. It also helps in improving the work quality by allowing students to provide anonymous reviews. It is an Open source platform which is based on Ruby with Rails.
Problem Statement
There were code smells detected in the application by code climate which violated the best practices of Ruby Rails. The goal was to fix those code smells by refactoring. Consider the following examples.
1. Add inverse_of option.
File: Participant.rb belongs_to :topic, class_name: 'SignUpTopic'.
In the class 'SignUpTopic', we have has_many :teams
When you have two models in a has_many, has_one or belongs_to association, the :inverse_of option in Rails tells ActiveRecord that they're two sides of the same association. Knowing the other side of the same association Rails can optimize object loading and will reference the same object in memory, instead of loading another copy of the same record.
2. Add dependent property
In the file roles.rb we have has_many :users
We need to specify "dependent" option here.
With associations of Active Record , we can make transitions smooth of such operations(relationships) by telling Rails that there is a connection between the two models. It would make more sense here if we add "dependent :destroy" option for users. If a particular role is deleted, all the users under that role must also disappear.
3. useless assignment of a variable.
In the file role.rb, we have
The variable e is not being used anywhere. Such values are assigned to "_" so that it doesnt get assigned to a variable and uses space.
rescue StandardError => _
4. Using snake case for method names.
In file roles.rb, we have
A standard good practice is to follow snake cases for naming methods.
5. Use find_by instead of where.first
In file scored_question.rb, we have
Rails prefers "find_by" over "where" as "find_by" returns a single record as result, whereas "where" returns an array of record, to which we need to append "first" or "each" to get single record out of it.
And, there were many more code smells which needed a fix.
Files Modified for this requirement
1. app/models/http_request.rb 2. app/models/Import_error.rb 3. app/models/Institution.rb 4. app/models/Instructor.rb 5. app/models/Invitation.rb 6. app/models/Join_team_request.rb 7. app/models/Language.rb 8. app/models/Late_policy.rb 9. app/models/Logger_message.rb 10. app/models/Markup_style.rb 11. app/models/Menu_item.rb 12. app/models/Menu.rb 13. app/models/Metareview_questionnaire.rb 14. app/models/Metareview_response_map.rb 15. app/models/Missing_object_id_error.rb 16. app/models/Multiple_choice_checkbox.rb 17. app/models/Multiple_choice_radio.rb 18. app/models/Node.rb 19. app/models/Notification.rb 20. app/models/On_the_fly_calc.rb 21. app/models/Participant.rb 22. app/models/Password_reset.rb 23. app/models/Path_error.rb 24. app/models/Permission.rb 25. app/models/Plagiarism_checker_assignment_submission.rb 26. app/models/Plagiarism_checker_comparison.rb 27. app/models/question_advice.rb 28. app/models/Questionnaire_header.rb 29. app/models/Questionnaire_node.rb 30. app/models/Questionnaire.rb 31. app/models/Questionnaire_type_node.rb 32. app/models/Question.rb 33. app/models/Quiz_assignment.rb 34. app/models/Quiz_question_choice.rb 35. app/models/Quiz_questionnaire.rb 36. app/models/Quiz_question.rb 37. app/models/Quiz_response_map.rb 38. app/models/Quiz_response.rb 39. app/models/Requested_user.rb 40. app/models/Response_map.rb 41. app/models/Response.rb 42. app/models/review_assignment.rb 43. app/models/Review_comment_paste_bin.rb 44. app/models/Review_grade.rb 45. app/models/Review_questionnaire.rb 46. app/models/Review_response_map.rb 47. app/models/Role.rb 48. app/models/Rscore.rb 49. app/models/rubric.rb 50. app/models/Scale.rb 51. app/models/Scored_question.rb 52. app/models/Score_view.rb