CSC/ECE 517 Spring 2021 - E2107. Refactor grades controller.rb
Introduction
Problem Statement
Expertiza has a functionality that allows students and instructors to receive and give grades, respectively. This functionality is implemented in the grades_controller.rb file. This controller currently violates some essential Rails design principles including having too much calculation and having methods' names that violate Ruby naming convention.
Project Task
Here are the refactoring steps needed to be taken in order to fix the grades_controller.rb .
- Create a new private method to encapsulate the population of the view model for all the necessary data (lines 102-107).
- Simplify the logic surrounding the error flashing. The nested logic seems unnecessary (lines 190-192).
- In line 300:
- Increase readability by fixing whitespace according to the common Ruby Programming guidelines.
- Return is not needed here.
- Remove space between ‘!’ and its argument.
- Use meaningful variable names and break up the expression.
- Move these functions into grades_helper.rb and change their names to be more Ruby-like:
- calculate_all_penalties
- calculate_penalty_attributes
- mean (also cease the usage of the assignment)
- calculate_average_vector
- build_score_vector
- Remove_negative_scores_and_build_charts
- The previous programmer used update_attribute, which is concerning, as validations are ignored (Line 153 and 219).
Refactoring Grades Controller
- Comment changed that was wrong ("head" into "heat")
- Line 102-107 (New Private Method made to encapsulate this)
The private method is called instead
Private method details
- Line 190-192
Simplify Logic
- Line 300
Boolean expression clean up with variables and white spaces/return statement removed
- Lines 153/219
153 function removed
219 function removed
Moved the calculate_all_penalties function from grades_controller.rb to grades_helper.rb and renamed it penalties.
Moved the calculate_penalty_attributes function from grades_controller.rb to grades_helper.rb and renamed it attributes.
Moved the mean function from grades_controller.rb to grades_helper.rb and refactored the code inside.
Moved the calculate_average_vector function from grades_controller.rb to grades_helper.rb and renamed it vector.
Moved the build_score_vector function from grades_controller.rb to grades_helper.rb and renamed it score_vector.
Moved the Remove_negative_scores_and_build_charts function from grades_controller.rb to grades_helper.rb and renamed it charts.
Testing Code
To test this controller, run the following command to verify the changes do not break any core functions.
rspec spec/controllers/grades_controller_spec.rb
When you run the program and get to login page. Use credentials below for instructor.
username: Instructor6 password: password
A good student to impersonate for testing is
student7366
Important views for using refactored functions
app/views/student_task/view.html.erb
app/views/grades/_teammate_reviews.html.erb
app/views/grades/_author_feedbacks.html.erb
app/views/grades/_tabbed_participant.html.erb
app/views/grades/_scores.html.erb
app/views/grades/_teammate.html.erb
app/views/grades/_reviews.html.erb
app/views/grades/_review_table.html.erb
app/views/grades/_metareviews.html.erb
app/views/grades/_participant.html.erb
app/views/grades/_tabbed_reviews.html.erb
app/views/grades/_submissions.html.erb
app/views/student_task/view.html.erb
app/views/grades/_view_heatgrid.html.erb
app/views/grades/view_team.html.erb
app/views/grades/_heat_grid_tab.html.erb
app/views/grades/_participant.html.erb
app/views/assignments/list_submissions.html.erb