diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb new file mode 100644 index 0000000..2d70cab --- /dev/null +++ b/app/controllers/todos_controller.rb @@ -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 diff --git a/app/helpers/todos_helper.rb b/app/helpers/todos_helper.rb new file mode 100644 index 0000000..65ab195 --- /dev/null +++ b/app/helpers/todos_helper.rb @@ -0,0 +1,2 @@ +module TodosHelper +end diff --git a/app/models/learning_goal.rb b/app/models/learning_goal.rb index bab7dec..554aaac 100644 --- a/app/models/learning_goal.rb +++ b/app/models/learning_goal.rb @@ -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 diff --git a/app/models/todo.rb b/app/models/todo.rb index af053c1..3b66da3 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 8d4e11f..c1ba737 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,6 +22,7 @@
  • <%= link_to t(".microposts"), microposts_path %>
  • <% if logged_in? %>
  • <%= link_to t(".diary_entries"), diary_entries_path %>
  • +
  • <%= link_to t(".learning_goals"), learning_goals_path %>
  • <%= link_to t(".profile"), user_path(current_user) %>
  • <%= link_to t(".log_out"), log_out_path, data: { turbo_method: :delete } %>
  • <% end %> diff --git a/app/views/learning_goals/index.html.erb b/app/views/learning_goals/index.html.erb index 83821ad..ac3a3a3 100644 --- a/app/views/learning_goals/index.html.erb +++ b/app/views/learning_goals/index.html.erb @@ -1,6 +1,7 @@

    <%= t(".learning_goals") %>

    <%= 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? %>

    <%= t(".empty") %>

    diff --git a/app/views/learning_goals/show.html.erb b/app/views/learning_goals/show.html.erb index 71507e5..d5b3241 100644 --- a/app/views/learning_goals/show.html.erb +++ b/app/views/learning_goals/show.html.erb @@ -9,6 +9,13 @@

    <%= @learning_goal.retrospective %>

    <% end %> +

    <%= t(".todos") %>

    + +

    <%= t(".tags") %>