Compare commits

..

3 Commits

Author SHA1 Message Date
Trevor Vallender 01e08c2656 Group learning goals 2024-01-07 14:17:47 +00:00
Trevor Vallender 80c2841e3d CRUD in place for todos 2024-01-07 13:59:55 +00:00
Trevor Vallender 084f965b25 Add Todos for learning goals 2024-01-07 13:22:21 +00:00
25 changed files with 278 additions and 20 deletions

View File

@ -13,3 +13,14 @@
ul#learning_goals {
list-style-type: none;
}
ul.todos {
list-style-type: none;
}
ul.tags {
list-style-type: none;
> li {
display: inline;
}
}

View File

@ -0,0 +1,44 @@
form#blog_post_form, form#learning_goal_form, form#todo_form {
display: flex;
flex-direction: column;
max-width: 55em;
> h2 {
width: 100%;
}
> trix-toolbar {
max-width: 95%;
}
> trix-editor {
width: 95%;
> ul {
list-style-type: disc;
}
}
input[type=text] {
width: 95%;
}
> input[type=submit] {
width: 95%;
}
}
.blog_post {
margin: 1em auto;
padding: .5em;
background-color: var(--inset-background-color);
border: 1px solid var(--border-color);
> .created_at {
text-align: right;
font-size: .8em;
}
}
ul#blog_posts {
list-style-type: none;
}

View File

@ -4,11 +4,7 @@ class LearningGoalsController < ApplicationController
skip_before_action :require_login, only: [:index, :show]
before_action :set_learning_goal, only: [:show, :edit, :update, :destroy]
def index
@learning_goals = LearningGoal.all
.order(created_at: :desc)
.page(params[:page])
end
def index ; end
def new
@learning_goal = LearningGoal.new

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,17 +1,21 @@
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
validates :completed, inclusion: { in: [true, false] }
validates :title, presence: true
has_and_belongs_to_many :tags
accepts_nested_attributes_for :tags
scope :current, -> { where(starts_on: ..Date.today, ends_on: Date.today..) }
scope :past, -> { where(ends_on: ..Date.today) }
scope :future, -> { where(starts_on: Date.today..) }
def microposts
microposts = Micropost.none
tags.each { |tag| microposts = microposts.or(tag.microposts) }
microposts
microposts.uniq
end
end

6
app/models/todo.rb Normal file
View File

@ -0,0 +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

View File

@ -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 %>

View File

@ -0,0 +1,4 @@
<div id="learning_goal_<%= learning_goal.id %>" class="learning_goal">
<h2><%= link_to learning_goal.title, learning_goal %></h2>
<%= learning_goal.description %>
</div>

View File

@ -1,14 +1,25 @@
<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>
<h3><%= t(".current") %></h3>
<% if LearningGoal.current.any? %>
<%= render LearningGoal.current %>
<% else %>
<ul id="learning_goals">
<% @learning_goals.each do |learning_goal| %>
<li><%= link_to learning_goal.title, learning_goal %></li>
<% end %>
</ul>
<%= paginate @learning_goals %>
<p><%= t(".no_current_goals") %></p>
<% end %>
<h3><%= t(".future") %></h3>
<% if LearningGoal.future.any? %>
<%= render LearningGoal.future %>
<% else %>
<p><%= t(".no_future_goals") %></p>
<% end %>
<h3><%= t(".past") %></h3>
<% if LearningGoal.past.any? %>
<%= render LearningGoal.past %>
<% else %>
<p><%= t(".no_past_goals") %></p>
<% end %>

View File

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

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 %>
<% 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 %>
<%= todo.description %>

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
microposts: μposts
diary_entries: Diary
learning_goals: Learning Goals

View File

@ -2,8 +2,14 @@ en:
learning_goals:
index:
learning_goals: Learning Goals
empty: You have no learning goals yet.
new: New learning goal
new_todo: New todo
current: Current
future: Future
past: Past
no_current_goals: You have no current goals
no_future_goals: You have no future goals
no_past_goals: You have no past goals
new:
create: Create learning goal
edit:
@ -19,4 +25,5 @@ en:
description: Description
retrospective: Retrospective
tags: Tags
todos: Todos
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
delete: Delete
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 :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

View File

@ -0,0 +1,12 @@
class CreateTodos < ActiveRecord::Migration[7.1]
def change
create_table :todos do |t|
t.belongs_to :learning_goal, null: false, foreign_key: true
t.boolean :done, null: false, default: false
t.string :title, null: false, default: ''
t.date :due
t.timestamps
end
end
end

13
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_01_06_103226) do
ActiveRecord::Schema[7.1].define(version: 2024_01_07_131738) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -140,6 +140,16 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_06_103226) do
t.datetime "updated_at", null: false
end
create_table "todos", force: :cascade do |t|
t.bigint "learning_goal_id", null: false
t.boolean "done", default: false, null: false
t.string "title", default: "", null: false
t.date "due"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["learning_goal_id"], name: "index_todos_on_learning_goal_id"
end
create_table "users", force: :cascade do |t|
t.string "username", null: false
t.string "password_digest", null: false
@ -161,4 +171,5 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_06_103226) do
add_foreign_key "exercises", "exercise_types"
add_foreign_key "learning_goals", "users"
add_foreign_key "microposts", "users"
add_foreign_key "todos", "learning_goals"
end

View File

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

10
test/fixtures/todos.yml vendored Normal file
View File

@ -0,0 +1,10 @@
one:
learning_goal: sql
done: false
title: Read SQL for Mere Mortals
due: <%= 3.years.from_now %>
two:
learning_goal: linux
done: true
title: Install Debian

7
test/models/todo_test.rb Normal file
View File

@ -0,0 +1,7 @@
require "test_helper"
class TodoTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end