CSC/ECE 517 Fall 2014/oss E1463 vpd: Difference between revisions
Line 42: | Line 42: | ||
#Used path helpers where possible | #Used path helpers where possible | ||
#Removed instance variables and used local variables instead. This should be better for scope reasons. Note that there are still 1-2 instance variables used in the view, `@show` and `@assignment` which don't appear to be used anywhere. (`@show` doesn't seem to ever be set, `@assignment` seems to only be used locally) However, we didn't touch this because it didn't deal directly with our changes to the controller. | #Removed instance variables and used local variables instead. This should be better for scope reasons. Note that there are still 1-2 instance variables used in the view, `@show` and `@assignment` which don't appear to be used anywhere. (`@show` doesn't seem to ever be set, `@assignment` seems to only be used locally) However, we didn't touch this because it didn't deal directly with our changes to the controller. | ||
#Introduced some local variables as params to the view which allow dropdowns to keep their selected option after the page reloads (the logic was there before, but it was using instance variables which were never set!) | #Introduced some local variables as params to the view which allow dropdowns to keep their selected option after the page reloads (the logic was there before, but it was using instance variables which were never set!). This is for the search/filtering options on the "Manage..." screen. | ||
==1) Changing List to Index== | ==1) Changing List to Index== |
Revision as of 04:47, 6 November 2014
E1463: Refactoring TreeDisplayController
Project Links
Our Expertiza Fork on AWS
Github Repo
Expertize Pull Request
Project Running on AWS
Setup - How To View Our Expertiza Fork Running on AWS
NOTE: The scope of our project was only to refactor select code. There are no feature additions or changes, so it should work just like production Expertiza.
1) Go to our Expertiza project running on AWS
2) Login as Admin
User: user2
Password: password
3) Click on "Manage..." if you are not automatically directed to the tree display.
Project Description
For this project, our team refactored the TreeDisplayController class in the Expertiza OSS project. This class provides access to questionnaires, review rubrics, author feedback, courses, assignments, and course evaluations. The tree display lists all of these categories with the ability for the user to "drill down" into the subcategories or just expand/collapse as needed.
Example Image Of Tree Display:
Refactoring TreeDisplayController Tasks
As part of the refactoring task, we had to address the following issues for enhancing code readability and maintainability. The tasks included:
- Changing List to Index using a RESTful approach.
- Combined all of the goto methods
- Use of routing helpers
- Ensuring that instantiation of instance variables is minimized.
In the end, we enhanced this list a bit and resulted in doing the following:
- Changed `list` method to `index`
- Removed `drill` - combined with `index`
- Removed all of the `goto` methods and had them go through `index` instead because they are essentially just filtered versions of a listing (and the previous implementation just redirected each `goto` back to `list` anyway)
- Used path helpers where possible
- Removed instance variables and used local variables instead. This should be better for scope reasons. Note that there are still 1-2 instance variables used in the view, `@show` and `@assignment` which don't appear to be used anywhere. (`@show` doesn't seem to ever be set, `@assignment` seems to only be used locally) However, we didn't touch this because it didn't deal directly with our changes to the controller.
- Introduced some local variables as params to the view which allow dropdowns to keep their selected option after the page reloads (the logic was there before, but it was using instance variables which were never set!). This is for the search/filtering options on the "Manage..." screen.
1) Changing List to Index
In order to make the codebase more RESTful, we have replaced all the controller action redirections with a tree_display_index_path which enormously reduces the code content and as well as provides an organized approach to action redirections.
As seen below, we were able to remove list from Routes.rb:
resources :tree_display do collection do get ':action' - post 'list' end end
2) Combined All of the Goto Methods
We also combined all of the goto methods into one, in accordance with the DRY principle. This removed a significant amount of duplicate code.
Shown below are 3 of the 10 very similar methods we removed from tree_display_controller.rb:
- # direct access to questionnaires - def goto_questionnaires - node_object = TreeFolder.find_by_name('Questionnaires') - session[:root] = FolderNode.find_by_node_object_id(node_object.id).id - redirect_to :controller => 'tree_display', :action => 'list' - end - - # direct access to review rubrics - def goto_review_rubrics - node_object = TreeFolder.find_by_name('Review') - session[:root] = FolderNode.find_by_node_object_id(node_object.id).id - redirect_to :controller => 'tree_display', :action => 'list' - end - - # direct access to metareview rubrics - def goto_metareview_rubrics - node_object = TreeFolder.find_by_name('Metareview') - session[:root] = FolderNode.find_by_node_object_id(node_object.id).id - redirect_to :controller => 'tree_display', :action => 'list' - end
These methods were replaced by a variable containing a list mapping all the session variable strings to database labels. The goto_assignments method was replaced with an index method to accept this variable, rather then calling each goto method individually.
Added variable:
+ @@Groups = { + 'Questionnaires' => 'Questionnaires', + 'Review rubrics' => 'Review', + 'Metareview rubrics' => 'Metareview', + 'Teammate review rubrics' => 'Teammate Review', + 'Author feedbacks' => 'Author Feedback', + 'Global survey' => 'Global Survey', + 'Surveys' => 'Survey', + 'Course evaluations' => 'Course Evaluation', + 'Courses' => 'Courses', + 'Bookmarkrating' => 'Bookmarkrating', + 'Assignments' => 'Assignments' + }
Removed goto_assignments method:
- # direct access to assignments - def goto_assignments - node_object = TreeFolder.find_by_name('Assignments') - session[:root] = FolderNode.find_by_node_object_id(node_object.id).id - redirect_to :controller => 'tree_display', :action => 'list' - end
Added index method:
+ def index + session[:root] = params[:root] + group = getGroup session[:menu] + node_object = TreeFolder.find_by_name(group) + if not group.blank? and not node_object.blank? + session[:root] = FolderNode.find_by_node_object_id(node_object.id).id + end
3) Use Routing Helpers
We refactored the tree display controller class to use route helpers rather than calling ":action => 'list', :controller => 'tree_display'", or something similar. This involved many edits in the tree_display_controller.rb file and related controller and view files. As seen below, we utilized routing helpers and replaced ':controller=>'course', :action=>'new', :private => 0 with new_course_path(private: 0).
_courses_folder_actions.html.erb changes:
<ul> <div> <%= link_to image_tag('/assets/tree_view/add-public-24.png'), - { :controller=>'course', :action=>'new', :private => 0 }, - { :title => 'Create Public Course', id: 'create-public-course' } %> + new_course_path(private: 0), + { title: 'Create Public Course', id: 'create-public-course' } %> <%= link_to image_tag('/assets/tree_view/add-private-24.png'), - { :controller=>'course', :action=>'new', :private => 1 }, - { :title => 'Create Private Course', id: 'create-private-course' } %> + new_course_path(private: 1), + { title: 'Create Private Course', id: 'create-private-course' } %> </div> </ul> </li> </ul>
Another example of routing helpers in _bread_crumbs.html.erb.
Before:
<%= link_to curr_node.get_name, :controller => 'tree_display', :action => 'drill', :root => curr_node.id %>
After:
<%= link_to curr_node.get_name, tree_display_index_path(root: curr_node.id)%>
4) Minimizing the count of Instance Variables
Instance Variables in Ruby
An instance variable has a name beginning with @, and its scope is confined to whatever object self refers to. Two different objects, even if they belong to the same class, are allowed to have different values for their instance variables. From outside the object, instance variables cannot be altered or even observed (i.e., ruby's instance variables are never public) except by whatever methods are explicitly provided by the programmer. As with globals, instance variables have the nil value until they are initialized.
Instance variables do not need to be declared. This indicates a flexible object structure; in fact, each instance variable is dynamically appended to an object when it is first assigned.
We have observed that in Expertiza, there were several code files related to the Tree Display controller which were observed to be using multiple instance variables which could be reduced. Our team aimed to limit the instantiation of such variables to only necessary places in our code.
The following was added to the index function of tree_display_controller.rb in order to replace the use of instance variables with local variables:
locals search: @search, sortvar: @sortvar, sortorder: @sortorder, root_node: @root_node, child_nodes: @child_nodes, filternode: params[:filternode], searchnode: params[:searchnode]
Many of the view files were changed to make use of the local variables.
Here is an example of how _bread_crumbs.html.erb was changed. Note the removal of the "@" characters.
Before:
<% if @root_node %> <%= link_to "Return to top", tree_display_index_path %> <% curr_node = @root_node while curr_node.parent_id curr_node = Node.find(curr_node.parent_id) %> <%= link_to curr_node.get_name, tree_display_index_path(root: curr_node.id)%> <% end %> <%= @root_node.get_name %><% end %>
After:
<% if root_node %> <%= link_to "Return to top", tree_display_index_path %> <% curr_node = root_node while curr_node.parent_id curr_node = Node.find(curr_node.parent_id) %> <%= link_to curr_node.get_name, tree_display_index_path(root: curr_node.id)%> <% end %> <%= root_node.get_name %><% end %>
Conclusion
In conclusion, we were able to successfully refactor the tree_display_controller and related controllers and views. We improved and expanded the use of RESTful and DRY design choices, implemented the use of routing helpers, ensured that instantiation of instance variables is minimized, and overall improved the quality and efficiency of the Expertiza codebase.