CSC/ECE 517 Spring 2020 - E2006. Refactor Tree Display Controller: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 11: Line 11:


===Problem Statement===
===Problem Statement===
[[https://docs.google.com/document/d/1_-rgdE4Fh_Fz_Jhxw1Qj4vehbu84qiUKAiLWGcFUl5A/edit#heading=h.h37uqoqtoks4 E2006. Refactor tree_display_controller.rb]]
[https://docs.google.com/document/d/1_-rgdE4Fh_Fz_Jhxw1Qj4vehbu84qiUKAiLWGcFUl5A/edit#heading=h.h37uqoqtoks4 E2006. Refactor tree_display_controller.rb]


===Background===
===Background===

Revision as of 00:17, 24 March 2020

E2006. Refactor tree_display_controller.rb

About Expertiza

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

Project Description

Problem Statement

E2006. Refactor tree_display_controller.rb

Background

The tree_display_controller is responsible for rendering hierarchical lists in the instructor user interface. Here is a sample instructor homepage:

The image below shows that specific questionnaires are nested under different questionnaire categories. This means that the node for a questionnaire has a parent_id that points to a node for a questionnaire category. Similarly, listings of courses and their assignments are hierarchical.

The various rubrics, as well as surveys, are types of questionnaires. So the parent_id of a particular rubric will be the type of rubric (e.g., teammate review) that it is.

The tree_display_controller was 379 lines long, and contained 45 methods. Many of those methods had similar names and were performing very similar functions. In an effort to save code and avoid DRY issues this file was refactored.

This effort reduced the controller to less than 200 lines, 20 public methods (15 of which are necessary scaffold for menu navigation), and 6 private methods.

Goals

From the problem background three main goals were derived to focus on in the refactor of tree display controller:

Remove unnecessary methods

Search the 45 methods defined in the controller to see where and why they are called. Remove outdated and unused methods, and see where it is fit to merge or replace methods.

Save code and avoid DRY issues

In addition to removing unnecessary methods, code should be reduced wherever possible to make the controller smaller and easier to read.

Improve understanding

For all necessary, complex methods in the controller, comments should be added to improve understanding of what methods do. This way, future reviewers won't have to search for implementations to understand the functionality when working on other issues.

Proposed Changes

Make all file scope methods private

Code that is only called within the tree_display_controller.rb was moved to the private scope. This makes the code easier to read and easier to understand what is getting called outside the controller. Assign private designation to internal methods

Combine folder_node_ng_getter with children_node_ng

folder_node_ng_getter was being called by the view to return FolderNode.get. The resulting json of this call was then passed, without modification, into children_node_ng. Since this was the only place folder_node_ng_getter was being called it was combined with children_node_ng to make things easier to read when when viewing controller code. Refactor children_node_ng

Refactor children_node_ng

children_node_ng was a misleading name. The method has been renamed get_folder_contents since it returns the contents of each top level folder (assignments, courses, and questionnaires) in json format. In addition to the name change, the get_folder_contents method was refactored to include the behavior of folder_node_ng_getter. This addition simplifies the code because the method no longer needs to convert the object that was converted to json back to an object. A single call serialize_folder_to_json makes the code easier to read instead of chasing nested function calls through initialize_fnode_update_children which ultimately leads to the same result. Another key change was changing the method from a post to a get, to better represent what it was doing. Changes to tree_display.jsx and routes.rb had to be made to account for the name change and method type change. Refactor children_node_ng

Collapse 'goto' methods

In the legacy code found in tree_display_controller.rb, 60 lines are dedicated to the 12 methods related to traversing the menu system. 11 of the methods call the goto_controller() method with different values. Those 11 methods were condensed into single line methods with one overarching comment to describe them. This change makes the code easier to read, since each of the 11 methods condensed carry out the same functionality, and easier to understand since we have added in a better comment for understanding what the code does (Goal 3). This also saved 41 lines of code in the controller (Goal 2). Compact gotos

Remove unused functions

By searching for the implementations of all functions in the controller we were able to recognize some of them as being unused and remove them (Goal 1). Others became unused once refactoring was completed.

  • filter()
  • filter_node_is_qan()
  • courses_assignments_obj()
  • confirm_notifications_access()
  • child_nodes_from_params()
  • res_node_for_child()
  • bridge_to_is_available()
  • update_tmp_obj()
  • action()

Changes to tree_display_controller.rb

The filter and filter_node_is_qan methods were both removed because they were only used in the page_header partial for tree display, which was removed approximately 3 years prior.

  def filter	
    qid = 'filter+'	
    search = params[:filter_string]	
    filter_node = params[:filternode]	
    if filter_node == 'QAN'                       # Questionaire Assignment Name	
      qid = filter_node_is_qan(search, qid)	
    elsif filter_node == 'ACN'                    # Assignment Course Name	
      session[:root] = 2	
      qid << search	
    end	
    qid	
  end
  # if filter node is 'QAN', get the corresponding assignment questionnaires	
  def filter_node_is_qan(search, qid)	
    assignment = Assignment.find_by(name: search)	
    if assignment	
      assignment_questionnaires = AssignmentQuestionnaire.where(assignment_id: assignment.id)	
      if assignment_questionnaires	
        assignment_questionnaires.each { |q| qid << "#{q.questionnaire_id}+" }	
        session[:root] = 1	
      end	
    end	
    qid	
  end

The course_assignments_obj method is not called anywhere else in the app.

  def courses_assignments_obj(node_type, tmp_object, node)	
    update_tmp_obj(tmp_object, node)	
    # tmpObject["private"] = node.get_private	
    instructor_id = node.get_instructor_id	
    ## if current user's role is TA for a course, then that course will be listed under his course listing.	
    update_in_ta_course_listing(instructor_id, node, tmp_object)	
    update_instructor(tmp_object, instructor_id)	
    update_is_available(tmp_object, instructor_id, node)	
    assignments_method(node, tmp_object) if node_type == "Assignments"	
  end

The confirm_notifications_access method is not called anywhere else in the app.

  def confirm_notifications_access	
    redirect_to controller: :notifications, action: :list if current_user.try(:student?)	
  end

The child_nodes_from_params method is not called anywhere else in the app

  # finding out child_nodes from params	
  def child_nodes_from_params(child_nodes)	
    if child_nodes.is_a? String and !child_nodes.empty?	
      JSON.parse(child_nodes)	
    else	
      child_nodes	
    end	
  end

The res_node_for_child method is not called anywhere else in the app.

  # getting result nodes for child	
  # Changes to this method were done as part of E1788_OSS_project_Maroon_Heatmap_fixes	
  #	
  # courses_assignments_obj method makes a call to update_in_ta_course_listing which	
  # separates out courses based on if he/she is the TA for the course passed	
  # by marking private to be true in that case	
  #	
  # this also ensures that instructors (who are not ta) would have update_in_ta_course_listing	
  # not changing the private value if he/she is not TA which was set to true for all courses before filtering	
  # in update_tmp_obj in courses_assignments_obj	
  #	
  # below objects/variable names were part of the project as before and	
  # refactoring could have affected other functionalities too, so it was avoided in this fix	
  #	
  # fix comment end	
  #	
  def res_node_for_child(tmp_res)	
    res = {}	
    tmp_res.each_key do |node_type|	
      res[node_type] = []	
      tmp_res[node_type].each do |node|	
        tmp_object = {	
          "nodeinfo" => node,	
          "name" => node.get_name,	
          "type" => node.type	
        }	
        courses_assignments_obj(node_type, tmp_object, node) if %w[Courses Assignments].include? node_type	
        res[node_type] << tmp_object	
      end	
    end	
    res	
  end

The bridge_to_is_available method is not called anywhere else in the app.

  def bridge_to_is_available	
    user = session[:user]	
    owner_id = params[:owner_id]	
    is_available(user, owner_id)	
  end

The update_tmp_obj method was used only once in the app, and it was in the tree display controller. We decided that since the purpose of the method was aligned with the purpose of the serialize_to_json method which called it, there was no reason to write a new method to call, especially since the update_tmp_obj method seemed like it was only making minor changes to the json.

  def update_tmp_obj(tmp_object, node)	
    tmp = {	
      "directory" => node.get_directory,	
      "creation_date" => node.get_creation_date,	
      "updated_date" => node.get_modified_date,	
      "institution" => Institution.where(id: node.retrieve_institution_id),	
      "private" => node.get_instructor_id == session[:user].id	
    }	
    tmp_object.merge!(tmp)	
  end

Changes to routes.rb

We removed the call for bridge_to_is_available and found that the action method was also unused.

Assume an object with FolderNode properties is passed to children_node_2_ng

Populate JSON object within serialize methods

Code Coverage

To be completed.

Team Information

Brooks Anderson

Rick Holloway

PJ Loheide

Special thanks to our mentor and professor: Dr. Edward Gehringer

References

To be completed.