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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 17: Line 17:
=== Problem 1: Refactor the create method  in assignment_controller.rb file ===
=== 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.
  * '''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.
<pre>
# 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
</pre>


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

Revision as of 19:01, 11 November 2022

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]

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.
# 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.

References

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