CSC/ECE 517 Fall 2021 - E2122. Refactor impersonate controller.rb
This wiki page describes the changes made under E2138, in order to auto-generate submission directory names based on assignment names for Fall 2021, CSC/ECE 517.
Overview
About Expertiza
Expertiza is an open source project based on Ruby on Rails framework. Expertiza allows the instructor to create new assignments and customize new or existing assignments. It also allows the instructor to create a list of topics the students can sign up for. Students can form teams in Expertiza to work on various projects and assignments. Students can also peer review other students' submissions. Expertiza supports submission across various document types, including the URLs and wiki pages.
Issues for this project
- Throughout the file, the user is being initialized but never used. This is bad coding practice as it slows down runtime.
- Line 36, convert nested if to elif
- Refactor very long lines of code to make it more readable.
- Test functions and increase coverage:
- auto_complete_for_user_name
- overwrite_session test line 47-51
- test_
Project implementation
Submitted work and demonstration of project
Files involved
- app/views/assignments/edit/_general.html.erb - added function that auto generate directory name, and substitute its spacebar with underscore.
- app/models/assignment.rb - added presence and uniqueness validation to assignment.
- app/controllers/assignments_controller.rb - modify create method to check for existing assignment name & directory.
Changes made to code
views
The following code was added to app/views/assignments/edit/_general.html.erb. The purpose is to generate submission directory name from the assignment name and substitute the spacebar with underscore.
<script> ... $(function() { $("#assignment_form_assignment_name").change(function() { filename = $( "#assignment_form_assignment_name" ).val().replace(/ /g,"_").replace(/[/\\?%*:|"<>/$&!#%^@]/g, '');; $('#assignment_form_assignment_directory_path').val(filename); }); }); function autogenerate_submission(){ assignment_form.assignment.directory_path = assignment_form.assignment.name; } </script>
models
The following code was added to app/models/assignment.rb's Assignment class, to add validation for assignment name and directory.
validates :directory_path, presence: true # E2138 Validation for unique submission directory validates :directory_path, uniqueness: {scope: :course_id}
controllers
The following code was modified in app/controllers/assignments_controller.rb's create method, adding checks for assignment name and directory when creating assignment.
Before
def create @assignment_form = AssignmentForm.new(assignment_form_params) if params[:button] if @assignment_form.save @assignment_form.create_assignment_node exist_assignment = Assignment.find_by(id: @assignment_form.assignment.id) assignment_form_params[:assignment][:id] = exist_assignment.id.to_s if assignment_form_params[:assignment][:directory_path].blank? assignment_form_params[:assignment][:directory_path] = "assignment_#{assignment_form_params[:assignment][:id]}" end ques_array = assignment_form_params[:assignment_questionnaire] due_array = assignment_form_params[:due_date] ques_array.each do |cur_questionnaire| cur_questionnaire[:assignment_id] = exist_assignment.id.to_s end due_array.each do |cur_due| cur_due[:parent_id] = exist_assignment.id.to_s end assignment_form_params[:assignment_questionnaire] = ques_array assignment_form_params[:due_date] = due_array @assignment_form.update(assignment_form_params, current_user) aid = Assignment.find_by(id: @assignment_form.assignment.id).id ExpertizaLogger.info "Assignment created: #{@assignment_form.as_json}" redirect_to edit_assignment_path aid undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ") return else flash.now[:error] = "Failed to create assignment" render 'new' end else render 'new' undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ") end end
After
def create @assignment_form = AssignmentForm.new(assignment_form_params) if params[:button] # E2138 issue #3 find_existing_assignment = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id) dir_path = assignment_form_params[:assignment][:directory_path] find_existing_directory = Assignment.find_by(directory_path: dir_path, course_id: @assignment_form.assignment.course_id) if !find_existing_assignment and !find_existing_directory and @assignment_form.save #No existing names/directories @assignment_form.create_assignment_node current_assignment = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id) assignment_form_params[:assignment][:id] = current_assignment.id.to_s ques_array = assignment_form_params[:assignment_questionnaire] due_array = assignment_form_params[:due_date] ques_array.each do |cur_questionnaire| cur_questionnaire[:assignment_id] = current_assignment.id.to_s end due_array.each do |cur_due| cur_due[:parent_id] = current_assignment.id.to_s end assignment_form_params[:assignment_questionnaire] = ques_array assignment_form_params[:due_date] = due_array @assignment_form.update(assignment_form_params, current_user) aid = Assignment.find_by(name: @assignment_form.assignment.name, course_id: @assignment_form.assignment.course_id).id ExpertizaLogger.info "Assignment created: #{@assignment_form.as_json}" redirect_to edit_assignment_path aid undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ") return else flash[:error] = "Failed to create assignment." if find_existing_assignment flash[:error] << "<br> " + @assignment_form.assignment.name + " already exists as an assignment name" end if find_existing_directory flash[:error] << "<br> " + dir_path + " already exists as a submission directory name" end redirect_to "/assignments/new?private=1" end else render 'new' undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ") end end
Test Plan
Manual UI Testing
The following steps must be performed to test the project UI:
Step 1: Log in as an Instructor, with Username - instructor6, Password - password
Step 2: Create a new assignment. In this case our assignment is called "E2054 Test Assignment", under course CSC 216 Fall 2009
RSpec Testing
Team Information
Mentor: John Bumgardner (jwbumga2)
Robin Piao (lpiao)
Shengjie Guo (sguo25)
Haoze Du (hdu5)