E1728. Remove useless partials from grades view and move view logic to grades helper.rb

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

This wiki provides details on the refactoring tasks that were undertaken as part of the continuous improvement to the Expertiza project

Background

Expertiza is a web application where students can submit and peer-review learning objects (articles, code, web sites, etc). The Expertiza project is supported by the National Science Foundation.

The application provides a complete system through which students and instructors collaborate on the learning objects as well as submit, review and grade assignments for the courses.

Motivation

By participating in the overall refactoring effort as part of the continuous improvement of Expertiza, students get an opportunity to work on a open source software project. This helps them gain exposure on the technologies used in the project as well as much needed experience in collaborating with peers as part of the software development process.

Tasks

The tasks involved as part of this refactoring effort were geared towards cleaning up the grade view logic.

Initial Task List

The initial set of tasks were:

  • For files: _scores_author_feedback.html.erb, _scores_metareview.html.erb, _scores_submitted_work.html.erb
  1. Move javascript code to assets
  • For files: _scores_header.html.erb
  1. Move logical code (such as L43-96) to helper file and assign self-explanatory method name
  • For files: _participant.html.erb, view_team.html.erb
  1. Move javascript code to assets
  2. Move logical code to helper file and assign self-explanatory method name
  3. Such as L8-22 in _participant.html.erb
  • Create test file named grades_helper_spec.rb in spec/helpers
  • Write test cases for all methods in grades_helper.rb

Revised Task List

On working with files _scores_author_feedback.html.erb, _scores_metareview.html.erb, _scores_submitted_work.html.erb, _scores_header.html.erb, the team came to find out that these files were not being used anymore in the application and the overall tasks were updated to the following:

  • Remove useless partials from grades view, such as _scores_author_feedback.html.erb, _scores_metareview.html.erb, _scores_submitted_work.html.erb, _scores_header.html.erb, etc.
  • For files _participant.html.erb, view_team.html.erb to grades_helper.rb
  1. Move javascript code to assets
  2. Move logical code to helper file and assign self-explanatory method name such as L8-22 in _participant.html.erb
  • Create test file named grades_helper_spec.rb in spec/helpers
  • Write test cases for all methods in grades_helper.rb by using factories

Refactoring Tasks

Remove useless partials from grades view, such as _scores_author_feedback.html.erb, _scores_metareview.html.erb, _scores_submitted_work.html.erb, _scores_header.html.erb, etc.

The named files were removed from the project and the deletions were committed to the repository

For files _participant.html.erb, view_team.html.erb to grades_helper.rb

  1. Move javascript code to assets

A new file was created and added to the project: app/assets/javascripts/grades/view_team.js

For the view_team.html.erb file, the javascript code was moved to view_team.js

$=jQuery;

$(function () {
    $("[data-toggle='tooltip']").tooltip();
    $("#scoresTable").tablesorter();
});

var lesser = false;
// Function to sort the columns based on the total review score
function col_sort(m) {
    lesser = !lesser
    // Swaps two columns of the table
    jQuery.moveColumn = function (table, from, to) {
        var rows = jQuery('tr', table);

        var hidden_child_row = table.find('tr.tablesorter-childRow');

        hidden_child_row.each(function () {
            inner_table = jQuery(this).find('table.tbl_questlist')
            hidden_table = inner_table.eq(0).find('tr')


            hidden_table.eq(from - 1).detach().insertBefore(hidden_table.eq(to - 1));
            if (from - to > 1) {
                hidden_table.eq(to - 1).detach().insertAfter((hidden_table.eq(from - 2)));

            }

        });


        var cols;
        rows.each(function () {
            cols = jQuery(this).children('th, td');
            cols.eq(from).detach().insertBefore(cols.eq(to));
            if (from - to > 1) {
                cols.eq(to).detach().insertAfter((cols.eq(from - 1)));

            }
        });
    }

    // Gets all the table with the class "tbl_heat"
    var tables = $("table.tbl_heat");
    // Get all the rows with the class accordion-toggle
    var tbr = tables.eq(m).find('tr.accordion-toggle');
    // Get the cells from the last row of the table
    var columns = tbr.eq(tbr.length - 1).find('td');
    // Init an array to hold the review total
    var sum_array = [];
    // iterate through the rows and calculate the total of each review
    for (var l = 2; l < columns.length - 2; l++) {
        var total = 0;
        for (var n = 0; n < tbr.length; n++) {
            var row_slice = tbr.eq(n).find('td');
            if (parseInt(row_slice[l].innerHTML) > 0) {
                total = total + parseInt(row_slice[l].innerHTML)
            }
        }
        sum_array.push(total)
    }

    // The sorting algorithm
    for (var i = 3; i < columns.length - 2; i++) {
        var j = i;
        while (j > 2 && compare(sum_array[j - 2], sum_array[j - 3], lesser)) {
            var tmp
            tmp = sum_array[j - 3]
            sum_array[j - 3] = sum_array[j - 2]
            sum_array[j - 2] = tmp
            jQuery.moveColumn($("table.tbl_heat").eq(m), j, j - 1);
            // This part is repeated since the table is updated
            tables = $("table.tbl_heat")
            tbr = tables.eq(m).find('tr.accordion-toggle');
            columns = tbr.eq(tbr.length - 1).find('td')
            j = j - 1;

        }
    }
}

// Function to return boolean based on lesser or greater operator
function compare(a, b, less) {
    if (less) {
        return a < b
    } else {
        return a > b
    }
}
  1. Move logical code to helper file and assign self-explanatory method name such as L8-22 in _participant.html.erb

Test Plans