CSC/ECE 517 Fall 2022 - E2273.Refactor delayed mailer.rb and scheduled task.rb

From Expertiza_Wiki
Revision as of 00:18, 13 November 2022 by Vrcholay (talk | contribs)
Jump to navigation Jump to search

Background

Expertiza uses Sidekiq gem for asynchronous processing of email tasks. It has a queue system to hold and then process jobs. Sidekiq’s queue replaces DelayedMailer’s queue. The previous team that worked on this also created a method perform() to gather email IDs of all participants in an assignment and send them an email reminder. Some test cases exist for this work.

History: Previous projects

* E2253 [1]
* E2144 [2]

About Assignment Controller

This class is responsible for the creation and implementation of assignment forms. This includes due dates, questionnaires and assigning reviews.

Previous Information can be found here E2144

Files Involved (some changed should not have been changed):

  • app/controllers/assignments_controller.rb
  • app/mailers/mail_worker.rb
  • app/models/assignment_form.rb
  • config/sidekiq.yml
  • spec/models/assignment_form_spec.rb
  • spec/sidekiq_mail_worker_spec.rb
  • spec/spec_helper.rb

Problems and planned changes

We are planning to accomplish the following tasks in this project:

Problem 1: Refactor the create method in assignment_controller.rb file

* Proposed Solution: Split the whole function into multiple methods performing specific tasks. Change the name of the variables to meaningful names in the method.
* Solution Implemented: Refactored the create method to split tasks into different methods and renamed a variable to correctly reflect its purpose.
  # creates a new assignment via the assignment form
  def create
    @assignment_form = AssignmentForm.new(assignment_form_params)
    if params[:button]
      # first check if assignment name exists, if not then create new, otherwise don't create new assignment
      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 && !find_existing_directory && @assignment_form.save # No existing names/directories
        assignment_creation_success
        # return
      else
        assignemnt_creation_failure(find_existing_assignment, dir_path, find_existing_directory)
      end
    else
      render 'new'
      undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
    end
  end
  # create a new assignment via the assigment form if the new assignment name and directory doesn't exist
  def assignment_creation_success
    @assignment_form.create_assignment_node
    exist_assignment = Assignment.find(@assignment_form.assignment.id)
    assignment_form_params[:assignment][:id] = exist_assignment.id.to_s
    if assignment_form_params[:assignment][:directory_path].blank? # No existing assignment for assignment form
      assignment_form_params[:assignment][:directory_path] = "assignment_#{assignment_form_params[:assignment][:id]}"
    end
    assignment_form_params[:assignment_questionnaire] = assign_questionnaire_array(exist_assignment)
    assignment_form_params[:due_date] = assign_due_date_array(exist_assignment)
    @assignment_form.update(assignment_form_params, current_user)
    assignment_id = Assignment.find(@assignment_form.assignment.id).id
    ExpertizaLogger.info "Assignment created: #{@assignment_form.as_json}"
    redirect_to edit_assignment_path assignment_id
    undo_link("Assignment \"#{@assignment_form.assignment.name}\" has been created successfully. ")
  end

  # raises a flash error messgae when it fails to create an assignment and redirect to assignement/new page 
  def assignemnt_creation_failure(existing_assignment, dir_path, existing_directory)
    flash[:error] = 'Failed to create assignment.'
    if existing_assignment
      flash[:error] << '<br>  ' + @assignment_form.assignment.name + ' already exists as an assignment name'
    end
    if existing_directory
      flash[:error] << '<br>  ' + dir_path + ' already exists as a submission directory name'
    end
    redirect_to '/assignments/new?private=1'
  end

Problem 2: Refactor the edit_params_setting method in assignment_controller.rb file

* Proposed Solution: Split the whole function into multiple methods performing specific tasks.

Problem 3: Increase the test coverage for sidekiq_mail_worker_spec.rb

* Proposed Solution: Add more tests to increase test coverage.

Automated Testing using RSPEC

The current version of expertiza included tests for the assignments_form and the mail_worker, but they were far from extensive. Thus we added a few tests of our own.

To run the tests, run the following commands from the expertiza directory

rspec ./spec/models/assignment_form_spec.rb

rspec ./spec/workers/sidekiq_mail_worker_spec.rb

Testing from UI

No changes were made to the functionality of the project, however you can access it through the setup below.

To Test via UI

1) Login with the login above

2) Click on Manage...

3) Click on Assignments

4) On the right side click the +

5) Once you've added a name, scroll to the bottom and click create

6) Repeat steps 2 and 3, to view the assignment added to the list

References

  1. Expertiza on GitHub
  2. GitHub Project Repository Fork
  3. E2273 Pull Request
  4. The live Expertiza website