CSC/ECE 517 Fall 2023 - E2359. Refactor user controller.rb, user.rb, and its child classes

From Expertiza_Wiki
Jump to navigation Jump to search

Expertiza

Expertiza is a Ruby on Rails based open source project. Instructors have the ability to add new projects, assignments, etc., as well as edit existing ones. Later on, they can view student submissions and grade them. Students can also use Expertiza to organize into teams to work on different projects and assignments and submit their work. They can also review other students' submissions.

Problem Statement

Tasks that were accomplished:


Files Modified

We have recently made some improvements to the codebase by refactoring several methods in the application. These changes were made to enhance code readability, reduce redundancy, and promote best coding practices. In particular, we have streamlined methods related to listing, retrieving, and filtering data, resulting in cleaner and more maintainable code. These modifications aim to make our codebase more efficient while maintaining the same functionality as before.

Changed files:

  • app/controllers/impersonate_controller.rb
  • app/controllers/users_controller.rb
  • app/controllers/questionnaires_controller.rb
  • app/models/instructor.rb
  • app/models/ta.rb
  • app/models/user.rb
  • app/models/assignment_form.rb
  • spec/models/ta_spec.rb
  • spec/models/user_spec.rb

New Files:

  • app/helpers/anonymized_helper.rb

Refactoring

The refactoring includes the following changes to improve code quality, readability, and maintainability while preserving the functionality of our software.

  • Recognized the need for refactoring to improve code readability and maintainability.
  • Introduced a new method, get_participants_from_instructed_entities, to handle the retrieval of participants from entities (either Course or Assignment) where the given user is an instructor.
  • Replaced nested loops with more concise and efficient array operations using flat_map, reducing temporary arrays and enhancing code organization.
  • Introduced a new method, filter_participants, to handle participant filtering based on user privileges. This reduced code duplication and improved readability.
  • In the my_tas method, replaced the loop with a flat_map operation, extracting TA IDs directly from the TaMapping records. This simplified the code and improved efficiency.
  • In the list_all, list_mine, and get methods, introduced a new method, filter_by_instructor, to handle filtering records based on the instructor ID and whether private records should be included. This reduced code duplication and improved readability.
  • Throughout the refactoring process, maintained the same functionality while adding comments to explain the purpose of each method and operation.
  • Used string interpolation instead of string concatenation for better readability.
  • Removed redundant scopes in user.rb
  • Refactored validations in user.rb
  • Modified method names to reflect responsibility of the code
  • Followed DRY principle and extracted repeated methods into helpers
  • Added separate module for functionality pertaining to anonymized view.
  • Made changes to refactor check_if_input_is_valid in impersonate_controller.rb using DRY principle to make code concise and readable.
  • In user.rb renamed get_available_users method and it's function calls to get_visible_users_with_lesser_roles to make it more meaningful.
  • Removed salt_first method in user_spec.rb as it does not contribute to application and is only used in test files.
  • Moved functionality associated with anonymized view from user.rb to a new file app/helpers/anonymized_helper.rb.
instructor.rb

app/models/instructor.rb


list_all Method: The list_all method retrieves instances of a given type where the user is either the instructor or the instance is not private. It uses an object_type.where query with an SQL condition for instructor and privacy.

list_mine Method: The list_mine method retrieves instances of a given type where the user is the instructor. It uses a similar object_type.where query with an SQL condition for instructor.

get Method: The get method retrieves a specific instance of a given type by its ID, considering the instructor or privacy conditions. It uses the object_type.find_by method with these conditions.

my_tas Method: The my_tas method retrieves a list of Teaching Assistant (TA) IDs associated with courses instructed by the given user. It has been refactored to use flat_map and a more efficient approach to collect TA IDs.

get_user_list Method: The get_user_list method retrieves a list of users who are participants in the courses and assignments where the given user is an instructor. The code has been refactored to provide a more efficient and organized approach.

get_participants_from_instructed_entities Method: This method is introduced to get participants from entities where the user is an instructor. It's used in get_user_list for both courses and assignments.

filter_participants Method: A new method to filter participants based on user privileges. It selects participants whose role's privileges are all included in the user's role's privileges.

ta.rb

app/models/ta.rb courses_assisted_with Method: The courses_assisted_with method has been updated for better readability. It now directly returns an array of courses associated with the TA, using a more concise TaMapping.where and Course.find combination.

instructor_or_co_ta? Method: The instructor_or_co_ta? method has been enhanced for readability. It checks if the TA is either the instructor or co-TA for a given questionnaire. The code has been refactored for better structure.


--- list_all and list_mine Methods: Both methods have been improved for better consistency and readability. The SQL queries have been updated for a more consistent and concise format.


---

---

---

assignment_form.rb

assignment_form.rb


ta_spec.rb

spec/models/ta_spec.rb

---

app/controllers/users_controller.rb

Testing Plan

Next Steps

Team

Mentor
  • Devashish Vachhani
Members
  • Doddaguni, Sachin R
  • Mahesh, Amogh
  • Villar, Sergio Vargas

References

  1. Expertiza
  2. OSS Projects on Expertiza