Compare commits

...

4 Commits

Author SHA1 Message Date
Trevor Vallender 658bc90ed7 Show related microposts on learning goals 2024-01-06 11:17:36 +00:00
Trevor Vallender 6b43cfa813 Add tag association to learning goals 2024-01-06 11:10:56 +00:00
Trevor Vallender 484d10385a Add frontend logic for learning goals 2024-01-06 10:29:41 +00:00
Trevor Vallender 2c06694b4c Add learning goal model 2024-01-06 10:04:31 +00:00
22 changed files with 276 additions and 3 deletions

View File

@ -1,4 +1,4 @@
form#blog_post_form {
form#blog_post_form, form#learning_goal_form {
display: flex;
flex-direction: column;
max-width: 55em;

View File

@ -0,0 +1,15 @@
.learning_goal {
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#learning_goals {
list-style-type: none;
}

View File

@ -0,0 +1,66 @@
# frozen_string_literal: true
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 new
@learning_goal = LearningGoal.new
end
def create
@learning_goal = LearningGoal.new(learning_goal_params)
@learning_goal.user = helpers.current_user
if @learning_goal.save
redirect_to @learning_goal, notice: t(".created")
else
render :new, status: :unprocessable_entity
end
end
def show ; end
def edit; end
def update
if @learning_goal.update(learning_goal_params)
redirect_to @learning_goal, notice: t(".updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
if @learning_goal.destroy
redirect_to learning_goals_path, notice: t(".deleted")
else
render :edit, status: :unprocessable_entity
end
end
private
def learning_goal_params
params.require(:learning_goal).permit(
:title,
:starts_on,
:ends_on,
:description,
:retrospective,
:completed,
:user,
tag_ids: [],
)
end
def set_learning_goal
@learning_goal = LearningGoal.find(params[:id])
end
end

View File

@ -0,0 +1,5 @@
module LearningGoalsHelper
def learning_goal_completed_text(learning_goal)
learning_goal.completed ? t(".completed") : t(".not_completed")
end
end

View File

@ -0,0 +1,17 @@
class LearningGoal < ApplicationRecord
belongs_to :user
has_rich_text :description
has_rich_text :retrospective
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
end
end

View File

@ -4,6 +4,7 @@ class Tag < ApplicationRecord
validates :name, presence: true
has_many :microposts_tags
has_many :microposts, through: :microposts_tags
has_and_belongs_to_many :learning_goals
def to_param
name

View File

@ -1,4 +1,4 @@
<div id="@blog_post_<%= @blog_post.id %>" class="blog_post">
<div id="blog_post_<%= @blog_post.id %>" class="blog_post">
<h2><%= @blog_post.title %></h2>
<p><%= @blog_post.content %></p>
<div class="created_at">

View File

@ -0,0 +1,25 @@
<%= form_with model: @learning_goal, id: "learning_goal_form" do |f| %>
<h2><%= title %></h2>
<%= f.text_field :title, placeholder: t(".title") %>
<%= f.rich_text_area :description %>
<%= f.label :starts_on %>
<%= f.date_field :starts_on %>
<%= f.label :ends_on %>
<%= f.date_field :ends_on %>
<%= f.rich_text_area :retrospective %>
<%= f.label :completed %>
<%= f.check_box :completed %>
<%= f.label :microposts_tag %>
<%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %>
<%= f.submit button_text %>
<% 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"), @learning_goal, data: { turbo_method: :delete, turbo_confirm: t(".confirm") } %>

View File

@ -0,0 +1,14 @@
<h2><%= t(".learning_goals") %></h2>
<%= link_to t(".new"), new_learning_goal_path if logged_in? %>
<% if @learning_goals.empty? %>
<p><%= t(".empty") %></p>
<% 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 %>
<% end %>

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,21 @@
<div id="learning_goal_<%= @learning_goal.id %>" class="learning_goal">
<%= link_to t(".edit"), edit_learning_goal_path(@learning_goal) if @learning_goal.user == current_user %>
<h2><%= @learning_goal.title %></h2>
<p><%= @learning_goal.starts_on %>-<%= @learning_goal.ends_on %></p>
<h3><%= t(".description") %></h3>
<p><%= @learning_goal.description %></p>
<% if @learning_goal.completed? %>
<h3><%= t(".retrospective") %></h3>
<p><%= @learning_goal.retrospective %></p>
<% end %>
<h3><%= t(".tags") %></h3>
<ul>
<% @learning_goal.tags.each do |tag| %>
<li><%= link_to tag.name, tag %></li>
<% end %>
</ul>
<h3><%= t(".related_microposts") %></h3>
<p><%= render @learning_goal.microposts %></p>
</div>

View File

@ -0,0 +1,5 @@
en:
learning_goals:
created: Successfully created learning goal
updated: Successfully updated learning goal
deleted: Successfully deleted learning goal

View File

@ -0,0 +1,22 @@
en:
learning_goals:
index:
learning_goals: Learning Goals
empty: You have no learning goals yet.
new: New learning goal
new:
create: Create learning goal
edit:
edit: Edit learning goal
delete: Delete learning goal
confirm: Are you sure you want to delete this learning goal?
form:
title: Title
show:
completed: Learning goal is complete
not_completed: Learning goal is incomplete
edit: Edit
description: Description
retrospective: Retrospective
tags: Tags
related_microposts: Related μposts

View File

@ -13,6 +13,7 @@ Rails.application.routes.draw do
get "confirm_email", to: "email_confirmations#confirm"
resources :blog_posts
resources :learning_goals
resources :microposts
resources :tags, only: [:index, :show]
resources :diary_entries, except: [:destroy]

View File

@ -0,0 +1,13 @@
class CreateLearningGoals < ActiveRecord::Migration[7.1]
def change
create_table :learning_goals do |t|
t.string :title, null: false
t.date :starts_on
t.date :ends_on
t.boolean :completed, null: false, default: false
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
class LearningGoalTagAssociation < ActiveRecord::Migration[7.1]
def change
create_join_table :learning_goals, :tags
end
end

19
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: 2023_12_31_112357) do
ActiveRecord::Schema[7.1].define(version: 2024_01_06_103226) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -97,6 +97,22 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_31_112357) do
t.index ["exercise_type_id"], name: "index_exercises_on_exercise_type_id"
end
create_table "learning_goals", force: :cascade do |t|
t.string "title", null: false
t.date "starts_on"
t.date "ends_on"
t.boolean "completed", default: false, null: false
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_learning_goals_on_user_id"
end
create_table "learning_goals_tags", id: false, force: :cascade do |t|
t.bigint "learning_goal_id", null: false
t.bigint "tag_id", null: false
end
create_table "microposts", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "created_at", null: false
@ -143,5 +159,6 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_31_112357) do
add_foreign_key "diary_entries", "users"
add_foreign_key "exercises", "diary_entries"
add_foreign_key "exercises", "exercise_types"
add_foreign_key "learning_goals", "users"
add_foreign_key "microposts", "users"
end

View File

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

View File

@ -10,3 +10,7 @@ draft_blog_post:
record: draft (BlogPost)
name: content
body: <p>Just some text</p>
learning_goal_one:
record: linux (LearningGoal)
name: content
body: <p>Learning about Linux</p>

13
test/fixtures/learning_goals.yml vendored Normal file
View File

@ -0,0 +1,13 @@
sql:
title: SQL
starts_on: 2024-01-06
ends_on: 2024-06-06
completed: false
user: trevor
linux:
title: Linux
starts_on: 2023-03-06
ends_on: 2023-06-06
completed: true
user: trevor

View File

@ -0,0 +1,4 @@
require "test_helper"
class LearningGoalTest < ActiveSupport::TestCase
end