CRUD in place for todos
This commit is contained in:
parent
084f965b25
commit
80c2841e3d
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
module TodosHelper
|
||||
end
|
|
@ -1,5 +1,8 @@
|
|||
class LearningGoal < ApplicationRecord
|
||||
belongs_to :user
|
||||
has_many :todos
|
||||
has_and_belongs_to_many :tags
|
||||
accepts_nested_attributes_for :tags
|
||||
|
||||
has_rich_text :description
|
||||
has_rich_text :retrospective
|
||||
|
@ -7,12 +10,9 @@ class LearningGoal < ApplicationRecord
|
|||
|
||||
validates :title, presence: true
|
||||
|
||||
has_and_belongs_to_many :tags
|
||||
accepts_nested_attributes_for :tags
|
||||
|
||||
def microposts
|
||||
microposts = Micropost.none
|
||||
tags.each { |tag| microposts = microposts.or(tag.microposts) }
|
||||
microposts
|
||||
microposts.uniq
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class Todo < ApplicationRecord
|
||||
belongs_to :learning_goal
|
||||
validates :done, inclusion: { in: [true, false] }
|
||||
has_rich_text :description
|
||||
has_one :user, through: :learning_goal
|
||||
end
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
<li><%= link_to t(".microposts"), microposts_path %></li>
|
||||
<% if logged_in? %>
|
||||
<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(".log_out"), log_out_path, data: { turbo_method: :delete } %></li>
|
||||
<% end %>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<h2><%= t(".learning_goals") %></h2>
|
||||
|
||||
<%= 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? %>
|
||||
<p><%= t(".empty") %></p>
|
||||
|
|
|
@ -9,6 +9,13 @@
|
|||
<p><%= @learning_goal.retrospective %></p>
|
||||
<% end %>
|
||||
|
||||
<h3><%= t(".todos") %></h3>
|
||||
<ul>
|
||||
<% @learning_goal.todos.each do |todo| %>
|
||||
<li><%= render todo %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<h3><%= t(".tags") %></h3>
|
||||
<ul>
|
||||
<% @learning_goal.tags.each do |tag| %>
|
||||
|
|
|
@ -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 %>
|
|
@ -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 %>
|
|
@ -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") } %>
|
|
@ -0,0 +1,8 @@
|
|||
<% @title = t(".create") %>
|
||||
|
||||
<%= render partial: "form",
|
||||
locals: {
|
||||
user: @user,
|
||||
button_text: t(".create"),
|
||||
title: t(".create"),
|
||||
} %>
|
|
@ -0,0 +1,5 @@
|
|||
en:
|
||||
todos:
|
||||
created: Successfully created todo
|
||||
updated: Successfully updated todo
|
||||
deleted: Successfully deleted todo
|
|
@ -10,3 +10,4 @@ en:
|
|||
blog_posts: Blog
|
||||
microposts: μposts
|
||||
diary_entries: Diary
|
||||
learning_goals: Learning Goals
|
||||
|
|
|
@ -4,6 +4,7 @@ en:
|
|||
learning_goals: Learning Goals
|
||||
empty: You have no learning goals yet.
|
||||
new: New learning goal
|
||||
new_todo: New todo
|
||||
new:
|
||||
create: Create learning goal
|
||||
edit:
|
||||
|
@ -19,4 +20,5 @@ en:
|
|||
description: Description
|
||||
retrospective: Retrospective
|
||||
tags: Tags
|
||||
todos: Todos
|
||||
related_microposts: Related μposts
|
||||
|
|
|
@ -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?
|
|
@ -16,6 +16,7 @@ Rails.application.routes.draw do
|
|||
resources :learning_goals
|
||||
resources :microposts
|
||||
resources :tags, only: [:index, :show]
|
||||
resources :todos, only: [:new, :create, :edit, :update, :destroy]
|
||||
resources :diary_entries, except: [:destroy]
|
||||
|
||||
get "up" => "rails/health#show", as: :rails_health_check
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class TodosControllerTest < ActionDispatch::IntegrationTest
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in New Issue