CSC/ECE 517 Fall 2017/E1764 Refactor Grades Controller.rb

From Expertiza_Wiki
Jump to navigation Jump to search

Expertiza Background

Expertiza is an educational web application for students to submit the articles, code, websites and peer-review learning objects. It is an open source project based on Ruby on Rails and developed by students for many years and keep refactoring and fixing bugs upon peer review.

Description of the current project

This project is to refactor grades controller which calculates the grades of assignment participants' from peer review and views of listing the scores. After refactor the previous work, it is the part of TLD that use RSpec to examine the correctness of functions and do the integration test.

Files modified in current project

1. grades_controller.rb
2. grades_controller_spec.rb

Grades_Controller

The main purpose of grades controller is to allow students and instructors to see grades and reviews through Expertiza, providing functions such as update scores, view teams, make chart and calculate penalties. Some functions were written in long and redundant way which makes it hard to understand and behaves inefficient. We extract some part of functions to become a helper function to make each function more clear and easier to understand. Besides, we slightly change some variable names by using more specific words to promote the readability.

Grades_Controller_Spec

Grades_controller_spec includes 10 Rspec tests for each function inside the grades_controller.rb. Some of the tests are integration tests and the rest are unit tests.

List of changes

1. Refactor make_chart method
2. Refactor calculate_penatly_attributes method
3. Refactor assign_all_penaties method
4. L136-138, L146-150: Refactor instructor_review method
5. L109: Use find_by instead of dynamic method
6. L178: Adjust the format of sprintf
7. Finish pending test in grades_controller_spec.rb

Solutions Implemented and Delivered

  • Refactor make_chart method

This is a function to make a bar chart based on the grades of participant. The original implementation contains lots of duplicated codes, so we extract the duplicated part out as a helper function called drop_decrement_from_scores. We make the type of participant scores as parameters such as metareview, feedback and teammate.

A helper function for make_chart method.

  • Refactor calculate_penatly_attributes method

We refactor this method by using zip function to prevent from running the loop three times when passing the different parameters and promote the readability.

  • Refactor assign_all_penaties method

We refactor this method by restructuring the code to make it clear and easier to understand.

  • Refactor instructor_review method

We refactor this method by replacing 'where.first' to 'find_or_create_' and then create.

  • Use find_by instead of dynamic method

When assign questionnaires, we extract the 'find type' statement out from the for each loop, in this case, we can reduce the execution iteration. Instead of using 'find_by', we use 'where' because 'find_by' will return the first element that match the condition; however, 'where' will return all the elements match the condition.

  • Refactor format over sprintf

Instead of using sprintf function, we use the format printing here which is more flexible and concise.

Testing Details

RSpec

Finished

For the integration tests, we tested four actions in grades_controller and five scenarios in total:

1. view

  • when the current assignment varies rubric by round, it retrieves questions, calculates scores and renders grades#view page
  • when the current assignment doesn’t vary rubric by round, it calculates scores and renders grades#view page

2. view_my_scores

  • when view_my_scores page is not allow to access, it shows a flash error message and redirects to root path (/)

3. view_team

  • it renders grades#view_team page

4. edit

  • it renders grades#edit page

The output coverage is 55.61% now, and the stats are here (https://coveralls.io/builds/13874393/source?filename=app%2Fcontrollers%2Fgrades_controller.rb)

Pending

There are still some pending tests to be finished:

1. view_my_scores

  • when view_my_scores page is allowed to access

2. instructor_review

  • when review does not exist
  • when review exists

3. update

  • when total is not equal to participant's grade, it updates grades and redirects to grades#edit page
  • when total is equal to participant's grade it redirects to grades#edit page

4. save_grade_and_comment_for_submission

  • it saves grade and comment for submission and redirects to assignments#list_submissions page

Future Work

1. Pass all tests created in grades_controller_spec.rb
2. Although some tests are passed, there still a room for improving the test coverage.