CSC/ECE 517 Spring 2023 -E2347. Reimplement duties controller.rb and badges controller.rb: Difference between revisions
Line 182: | Line 182: | ||
==Contributors== | ==Contributors== | ||
This feature was created as part of Dr. Edward Gehringer's "CSC/ECE 517: Object-Oriented Design and Development" class, Spring 2023. The contributors | This feature was created as part of Dr. Edward Gehringer's "CSC/ECE 517: Object-Oriented Design and Development" class, Spring 2023. <br> | ||
The contributors: <br> | |||
Sanket Tangade(sstangad@ncsu.edu) <br> | |||
Kunal Patil(kpatil5@ncsu.edu) <br> | |||
Yash Sonar(ysonar@ncsu.edu) |
Revision as of 00:53, 8 April 2023
Problem Statement
The problem at hand involves the Duties and Badge modules of Expertiza. Mainly, the task is to improve the code logic and design by following the Object Oriented Design principles and methods and make the code base more efficient, easy to read and structured. We have two controller classes "duties_controller.rb" and "badge_controller.rb".
The duties_controller in the Duties module defines several actions, including create, edit, update, and delete. The create action is used to save the new duty to the database, while the edit action renders the form for editing an existing duty. The update action updates the duty in the database, and the delete action deletes a duty from the database.
On the other hand, the Badge module has a create action that creates a new Badge instance using user parameters and saves an image file if attached. It also updates the image_name attribute of the badge instance. Additionally, there is a new action that sets up a new Badge instance and a redirect_to_assignment method to redirect to the previous page visited by the user. This method is used with the new action to keep track of the page the user was on when they requested a new Badge instance.
The task is to ensure that the Duties and Badge modules function correctly and that the create, edit, update, delete, and new actions all work as intended as well as to optimize, restructure and reimplement the code by following the Object Oriented way. This will help manage duties and badges in Expertiza effectively.
Aim of this Project
The aim of this Project is:
- Re-implementing CRUD operations for each controller
- Fix bugs and minor flows in the existing code base and functionality
- Removing redundant or unclear code logic
- Writing RSpec test scripts for both the controllers
By meeting these objectives, both controllers can perform their fundamental create, read, update, and delete operations, while also improving the code's readability and functionality. Additionally, it will aid in addressing any current bugs, thereby enhancing the accuracy of the code.
Implementation
We have given two controllers, duties_controller.rb and badges_controller.rb where we have to reimplement the code using a more object-oriented and efficient way. One potential way to refactor this code in a more object-oriented and efficient manner is to extract responsibilities into smaller objects with clear roles and interfaces. This can help reduce complexity and coupling between different parts of the system, making it easier to maintain and extend over time. Additionally, adopting design patterns and principles like SOLID can help improve the overall quality and modularity of the codebase. Overall, investing time and effort in improving the design and architecture of the code can pay off in the long term by reducing technical debt and enabling faster and more effective development.
After gaining a grasp of the system's flow, we delved into the code within the controllers. Upon initial inspection, it became apparent that the controllers were managing all of the CRUD operations but there were some minor changes that can enhance it and make it more readable and easy to understand. We discerned which functionalities did not pertain to the duties_controler as well as the badges_controller and determined which redundant code should be eliminated. Our reimplementation project has addressed the following issues:
- duties_controller.rb:
Add re-implementation logic and details here
- badges_controller.rb: To reimplement the BadgesController in a more object-oriented and efficient way, you could split it into a Badge model and a BadgesController controller, following the Model-View-Controller (MVC) pattern. Earlier, all the CRUD methods as well as the dependent methods were written in the same controller class which is not much of the preferred way to write code logic in an MVC pattern and follow the principles. We planned to move the dependent and validation method to the Badge.rb model class where it would be more easier and coder-friendly to understand and work with the code in the future as well.
Here is the previous code of the "badges_controller.rb"
class BadgesController < ApplicationController include AuthorizationHelper def action_allowed? current_user_has_ta_privileges? end def new @badge = Badge.new session[:return_to] ||= request.referer end def redirect_to_assignment redirect_to session.delete(:return_to) end def create @badge = Badge.new(badge_params) image_file = params[:badge][:image_file] if image_file File.open(Rails.root.join('app', 'assets', 'images', 'badges', image_file.original_filename), 'wb') do |file| file.write(image_file.read) end @badge.image_name = image_file.original_filename else @badge.image_name = '' end respond_to do |format| if @badge.save format.html { redirect_to session.delete(:return_to), notice: 'Badge was successfully created' } else format.html { render :new } format.json { render json: @badge.errors, status: :unprocessable_entity } end end end def badge_params params.require(:badge).permit(:name, :description, :image_name) end end
Here is how we planned to implement the "Badge.rb" model class:
This model will handle the validations and image upload for the Badge object. The upload_image method can be called from the create action in the BadgesController to save the image to the file system.
class Badge < ApplicationRecord def self.upload_image(image_file) return '' unless image_file image_name = image_file.original_filename File.open(Rails.root.join('app', 'assets', 'images', 'badges', image_name), 'wb') do |file| file.write(image_file.read) end image_name end end
Similarly, we planned to re-implement the BadgesController.rb as follows:
This BadgesController now follows the RESTful conventions, which makes it easier to understand and maintain. It also leverages the Badge model to handle the validations and image upload.
class BadgesController < ApplicationController include AuthorizationHelper before_action :set_return_to, only: [:new] before_action :set_badge, only: [:show, :edit, :update, :destroy] def action_allowed? current_user_has_ta_privileges? end # GET /badges def index @badges = Badge.all end # GET /badges/new def new @badge = Badge.new end # POST /badges def create @badge = Badge.new(badge_params) @badge.image_name = Badge.upload_image(params[:badge][:image_file]) respond_to do |format| if @badge.save format.html { redirect_to redirect_to_url, notice: 'Badge was successfully created' } else format.html { render :new } format.json { render json: @badge.errors, status: :unprocessable_entity } end end end # PATCH/PUT /badges/1 def update if @badge.update(badge_params) redirect_to @badge, notice: 'Badge was successfully updated.' else render :edit end end private def set_badge @badge = Badge.find(params[:id]) end def set_return_to session[:return_to] ||= request.referer end def redirect_to_url session.delete(:return_to) || badges_url end def badge_params params.require(:badge).permit(:name, :description, :image_name) end end
Testing Methodology
Contributors
This feature was created as part of Dr. Edward Gehringer's "CSC/ECE 517: Object-Oriented Design and Development" class, Spring 2023.
The contributors:
Sanket Tangade(sstangad@ncsu.edu)
Kunal Patil(kpatil5@ncsu.edu)
Yash Sonar(ysonar@ncsu.edu)