Giter VIP home page Giter VIP logo

goal_tracker's People

Stargazers

 avatar  avatar

Watchers

 avatar

goal_tracker's Issues

DRY controllers

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.

Code documentation

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

Searchable concern

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.