killswitch0 / goal_tracker Goto Github PK
View Code? Open in Web Editor NEWThe "Goal Tracker" web app helps users set, achieve goals, manage tasks, build habits efficiently, and join challenges to share progress.
The "Goal Tracker" web app helps users set, achieve goals, manage tasks, build habits efficiently, and join challenges to share progress.
Reviewing tasks_controller.rb
(https://github.com/Killswitch0/goal_tracker/blob/main/app/controllers/tasks_controller.rb), we could notice we use the same code block 5 times in different actions:
@goal = Goal.find(params[:goal_id])
Another one 4 times:
@task = Task.find(params[:id])
Suggestion:
We could move both of them into separate setter methods that are using conditional variable initialization ||=
(https://rubystyle.guide/#double-pipe-for-uninit):
private def goal
@goal ||= Goal.find(params[:goal_id])
end
private def task
@task ||= Task.find(params[:id])
end
We can utilize those methods instead of repeating the same code multiple times. Additionally, we can place them in before_action callbacks to set the variables automatically without explicitly invoking those methods in the actions. Additionally, we could make them helper methods, allowing access to them from the views.
We can apply this concept across all controllers within the project.
As another suggestion, it would be very helpful for you to write a documentation for each code unit.
You could use something like https://github.com/lsegal/yard to systematize it.
For example your actions would look like this:
# Checks if the habit was completed today.
#
# @return [Boolean]
def completed_today?
completion_dates.created_today.exists? && self.completed?
end
...
# GET /goals/:goal_id/habits/new
def new
@habit = Habit.new
end
# GET /goals/:goal_id/habits/:id/edit
def edit
@habit = Habit.find(params[:id])
end
# POST /goals/:goal_id/habits
def create
@habit = @goal.habits.build(habit_params)
@habit.user = current_user
...
Code documentation is crucial for ensuring clear understanding, promoting collaboration, and simplifying maintenance (it just helps everyone).
We have two exactly same methods in habit.rb
and goal.rb
:
def self.search(search, user)
where('lower(name) LIKE ? AND user_id = ?', "%#{search.downcase}%", "#{user.id}") if search
end
To follow the DRY principle, we could move this definition to the searchable.rb
concern (app/models/concerns
), which would look like that:
# frozen_string_literal: true
module Searchable
extend ActiveSupport::Concern
class_methods do
def search(search, user)
return unless search
where('lower(name) LIKE ? AND user_id = ?', "%#{search.downcase}%", user.id)
end
end
end
Also, please review the refactored method using the "GuardClause" (https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/GuardClause)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.