CRUD in place for todos

This commit is contained in:
Trevor Vallender 2024-01-07 13:50:53 +00:00
parent 084f965b25
commit 80c2841e3d
17 changed files with 144 additions and 4 deletions

View File

@ -0,0 +1,51 @@
class TodosController < ApplicationController
before_action :set_todo, only: [:edit, :update, :destroy]
def new
@todo = Todo.new
end
def create
@todo = Todo.new(todo_params)
if @todo.save
redirect_to @todo.learning_goal, notice: t(".created")
else
render :new, status: :unprocessable_entity
end
end
def edit ; end
def update
if @todo.update(todo_params)
redirect_to @todo.learning_goal, notice: t(".updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
learning_goal = @todo.learning_goal
if @todo.destroy
redirect_to learning_goal, notice: t(".deleted")
else
render :edit, status: :unprocessable_entity
end
end
private
def set_todo
@todo = Todo.find(params[:id])
end
def todo_params
params.require(:todo).permit(
:learning_goal_id,
:description,
:done,
:title,
:due,
)
end
end

View File

@ -0,0 +1,2 @@
module TodosHelper
end

View File

@ -1,5 +1,8 @@
class LearningGoal < ApplicationRecord class LearningGoal < ApplicationRecord
belongs_to :user belongs_to :user
has_many :todos
has_and_belongs_to_many :tags
accepts_nested_attributes_for :tags
has_rich_text :description has_rich_text :description
has_rich_text :retrospective has_rich_text :retrospective
@ -7,12 +10,9 @@ class LearningGoal < ApplicationRecord
validates :title, presence: true validates :title, presence: true
has_and_belongs_to_many :tags
accepts_nested_attributes_for :tags
def microposts def microposts
microposts = Micropost.none microposts = Micropost.none
tags.each { |tag| microposts = microposts.or(tag.microposts) } tags.each { |tag| microposts = microposts.or(tag.microposts) }
microposts microposts.uniq
end end
end end

View File

@ -1,4 +1,6 @@
class Todo < ApplicationRecord class Todo < ApplicationRecord
belongs_to :learning_goal belongs_to :learning_goal
validates :done, inclusion: { in: [true, false] } validates :done, inclusion: { in: [true, false] }
has_rich_text :description
has_one :user, through: :learning_goal
end end

View File

@ -22,6 +22,7 @@
<li><%= link_to t(".microposts"), microposts_path %></li> <li><%= link_to t(".microposts"), microposts_path %></li>
<% if logged_in? %> <% if logged_in? %>
<li><%= link_to t(".diary_entries"), diary_entries_path %></li> <li><%= link_to t(".diary_entries"), diary_entries_path %></li>
<li><%= link_to t(".learning_goals"), learning_goals_path %></li>
<li><%= link_to t(".profile"), user_path(current_user) %></li> <li><%= link_to t(".profile"), user_path(current_user) %></li>
<li><%= link_to t(".log_out"), log_out_path, data: { turbo_method: :delete } %></li> <li><%= link_to t(".log_out"), log_out_path, data: { turbo_method: :delete } %></li>
<% end %> <% end %>

View File

@ -1,6 +1,7 @@
<h2><%= t(".learning_goals") %></h2> <h2><%= t(".learning_goals") %></h2>
<%= link_to t(".new"), new_learning_goal_path if logged_in? %> <%= link_to t(".new"), new_learning_goal_path if logged_in? %>
<%= link_to t(".new_todo"), new_todo_path if logged_in? %>
<% if @learning_goals.empty? %> <% if @learning_goals.empty? %>
<p><%= t(".empty") %></p> <p><%= t(".empty") %></p>

View File

@ -9,6 +9,13 @@
<p><%= @learning_goal.retrospective %></p> <p><%= @learning_goal.retrospective %></p>
<% end %> <% end %>
<h3><%= t(".todos") %></h3>
<ul>
<% @learning_goal.todos.each do |todo| %>
<li><%= render todo %></li>
<% end %>
</ul>
<h3><%= t(".tags") %></h3> <h3><%= t(".tags") %></h3>
<ul> <ul>
<% @learning_goal.tags.each do |tag| %> <% @learning_goal.tags.each do |tag| %>

View File

@ -0,0 +1,18 @@
<%= form_with model: @todo, id: "todo_form" do |f| %>
<h2><%= title %></h2>
<%= f.text_field :title, placeholder: t(".title") %>
<%= f.label :learning_goal %>
<%= f.collection_select :learning_goal_id, LearningGoal.all, :id, :title %>
<%= f.rich_text_area :description %>
<%= f.label :due %>
<%= f.date_field :due %>
<%= f.label :done %>
<%= f.check_box :done %>
<%= f.submit button_text %>
<% end %>

View File

@ -0,0 +1,10 @@
<input type="checkbox" class="todo" disabled
<% if todo.done? %>
checked
<% end %>>
<%= todo.title %>
<%= todo.description %>
<% if todo.user == current_user %>
<%= link_to t(".edit"), edit_todo_path(todo) %>
<%= link_to t(".delete"), todo, data: { turbo_method: :delete, turbo_confirm: t(".confirm") } %>
<% end %>

View File

@ -0,0 +1,10 @@
<% @title = t(".edit") %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".edit"),
title: t(".edit"),
} %>
<%= link_to t(".delete"), @todo, data: { turbo_method: :delete, turbo_confirm: t(".confirm") } %>

View File

@ -0,0 +1,8 @@
<% @title = t(".create") %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".create"),
title: t(".create"),
} %>

View File

@ -0,0 +1,5 @@
en:
todos:
created: Successfully created todo
updated: Successfully updated todo
deleted: Successfully deleted todo

View File

@ -10,3 +10,4 @@ en:
blog_posts: Blog blog_posts: Blog
microposts: μposts microposts: μposts
diary_entries: Diary diary_entries: Diary
learning_goals: Learning Goals

View File

@ -4,6 +4,7 @@ en:
learning_goals: Learning Goals learning_goals: Learning Goals
empty: You have no learning goals yet. empty: You have no learning goals yet.
new: New learning goal new: New learning goal
new_todo: New todo
new: new:
create: Create learning goal create: Create learning goal
edit: edit:
@ -19,4 +20,5 @@ en:
description: Description description: Description
retrospective: Retrospective retrospective: Retrospective
tags: Tags tags: Tags
todos: Todos
related_microposts: Related μposts related_microposts: Related μposts

View File

@ -0,0 +1,14 @@
en:
todos:
new:
create: Create todo
edit:
edit: Edit todo
delete: Delete todo
confirm: Are you sure you want to delete this todo?
form:
title: Title
todo:
edit: Edit todo
delete: Delete todo
confirm: Are you sure you want to delete this todo?

View File

@ -16,6 +16,7 @@ Rails.application.routes.draw do
resources :learning_goals resources :learning_goals
resources :microposts resources :microposts
resources :tags, only: [:index, :show] resources :tags, only: [:index, :show]
resources :todos, only: [:new, :create, :edit, :update, :destroy]
resources :diary_entries, except: [:destroy] resources :diary_entries, except: [:destroy]
get "up" => "rails/health#show", as: :rails_health_check get "up" => "rails/health#show", as: :rails_health_check

View File

@ -0,0 +1,7 @@
require "test_helper"
class TodosControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end