From 484d10385a56cc3df0fc5070d7d25aa43b0b3876 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Sat, 6 Jan 2024 10:29:41 +0000 Subject: [PATCH] Add frontend logic for learning goals --- app/assets/stylesheets/blog_posts.css | 2 +- app/assets/stylesheets/learning_goals.css | 15 +++++ app/controllers/learning_goals_controller.rb | 65 +++++++++++++++++++ app/helpers/learning_goals_helper.rb | 5 ++ app/models/learning_goal.rb | 3 +- app/views/blog_posts/show.html.erb | 2 +- app/views/learning_goals/_form.html.erb | 22 +++++++ app/views/learning_goals/edit.html.erb | 10 +++ app/views/learning_goals/index.html.erb | 14 ++++ app/views/learning_goals/new.html.erb | 8 +++ app/views/learning_goals/show.html.erb | 11 ++++ .../locales/controllers/learning_goals/en.yml | 5 ++ config/locales/views/learning_goals/en.yml | 20 ++++++ config/routes.rb | 1 + .../learning_goals_controller_test.rb | 7 ++ 15 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 app/assets/stylesheets/learning_goals.css create mode 100644 app/controllers/learning_goals_controller.rb create mode 100644 app/helpers/learning_goals_helper.rb create mode 100644 app/views/learning_goals/_form.html.erb create mode 100644 app/views/learning_goals/edit.html.erb create mode 100644 app/views/learning_goals/index.html.erb create mode 100644 app/views/learning_goals/new.html.erb create mode 100644 app/views/learning_goals/show.html.erb create mode 100644 config/locales/controllers/learning_goals/en.yml create mode 100644 config/locales/views/learning_goals/en.yml create mode 100644 test/controllers/learning_goals_controller_test.rb diff --git a/app/assets/stylesheets/blog_posts.css b/app/assets/stylesheets/blog_posts.css index ecb1bef..2ae143a 100644 --- a/app/assets/stylesheets/blog_posts.css +++ b/app/assets/stylesheets/blog_posts.css @@ -1,4 +1,4 @@ -form#blog_post_form { +form#blog_post_form, form#learning_goal_form { display: flex; flex-direction: column; max-width: 55em; diff --git a/app/assets/stylesheets/learning_goals.css b/app/assets/stylesheets/learning_goals.css new file mode 100644 index 0000000..b6d428d --- /dev/null +++ b/app/assets/stylesheets/learning_goals.css @@ -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; +} diff --git a/app/controllers/learning_goals_controller.rb b/app/controllers/learning_goals_controller.rb new file mode 100644 index 0000000..5d8d981 --- /dev/null +++ b/app/controllers/learning_goals_controller.rb @@ -0,0 +1,65 @@ +# 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, + ) + end + + def set_learning_goal + @learning_goal = LearningGoal.find(params[:id]) + end +end diff --git a/app/helpers/learning_goals_helper.rb b/app/helpers/learning_goals_helper.rb new file mode 100644 index 0000000..d36c8ac --- /dev/null +++ b/app/helpers/learning_goals_helper.rb @@ -0,0 +1,5 @@ +module LearningGoalsHelper + def learning_goal_completed_text(learning_goal) + learning_goal.completed ? t(".completed") : t(".not_completed") + end +end diff --git a/app/models/learning_goal.rb b/app/models/learning_goal.rb index 6aae83c..879fdf3 100644 --- a/app/models/learning_goal.rb +++ b/app/models/learning_goal.rb @@ -1,7 +1,8 @@ class LearningGoal < ApplicationRecord belongs_to :user - has_rich_text :description, :retrospective + has_rich_text :description + has_rich_text :retrospective validates :title, presence: true end diff --git a/app/views/blog_posts/show.html.erb b/app/views/blog_posts/show.html.erb index 44e9ff0..7346df1 100644 --- a/app/views/blog_posts/show.html.erb +++ b/app/views/blog_posts/show.html.erb @@ -1,4 +1,4 @@ -
+

<%= @blog_post.title %>

<%= @blog_post.content %>

diff --git a/app/views/learning_goals/_form.html.erb b/app/views/learning_goals/_form.html.erb new file mode 100644 index 0000000..0c06f0b --- /dev/null +++ b/app/views/learning_goals/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_with model: @learning_goal, id: "learning_goal_form" do |f| %> +

<%= title %>

+ + <%= 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.submit button_text %> +<% end %> + + diff --git a/app/views/learning_goals/edit.html.erb b/app/views/learning_goals/edit.html.erb new file mode 100644 index 0000000..36f1ace --- /dev/null +++ b/app/views/learning_goals/edit.html.erb @@ -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") } %> diff --git a/app/views/learning_goals/index.html.erb b/app/views/learning_goals/index.html.erb new file mode 100644 index 0000000..83821ad --- /dev/null +++ b/app/views/learning_goals/index.html.erb @@ -0,0 +1,14 @@ +

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

+ +<%= link_to t(".new"), new_learning_goal_path if logged_in? %> + +<% if @learning_goals.empty? %> +

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

+<% else %> +
    + <% @learning_goals.each do |learning_goal| %> +
  • <%= link_to learning_goal.title, learning_goal %>
  • + <% end %> +
+ <%= paginate @learning_goals %> +<% end %> diff --git a/app/views/learning_goals/new.html.erb b/app/views/learning_goals/new.html.erb new file mode 100644 index 0000000..e4e2ca0 --- /dev/null +++ b/app/views/learning_goals/new.html.erb @@ -0,0 +1,8 @@ +<% @title = t(".create") %> + +<%= render partial: "form", + locals: { + user: @user, + button_text: t(".create"), + title: t(".create"), + } %> diff --git a/app/views/learning_goals/show.html.erb b/app/views/learning_goals/show.html.erb new file mode 100644 index 0000000..798d5cf --- /dev/null +++ b/app/views/learning_goals/show.html.erb @@ -0,0 +1,11 @@ +
+

<%= @learning_goal.title %>

+

<%= @learning_goal.starts_on %>-<%= @learning_goal.ends_on %>

+

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

+

<%= @learning_goal.description %>

+ <% if @learning_goal.completed? %> +

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

+

<%= @learning_goal.retrospective %>

+ <% end %> + <%= link_to t(".edit"), edit_learning_goal_path(@learning_goal) if @learning_goal.user == current_user %> +
diff --git a/config/locales/controllers/learning_goals/en.yml b/config/locales/controllers/learning_goals/en.yml new file mode 100644 index 0000000..3e0d553 --- /dev/null +++ b/config/locales/controllers/learning_goals/en.yml @@ -0,0 +1,5 @@ +en: + learning_goals: + created: Successfully created learning goal + updated: Successfully updated learning goal + deleted: Successfully deleted learning goal diff --git a/config/locales/views/learning_goals/en.yml b/config/locales/views/learning_goals/en.yml new file mode 100644 index 0000000..45686e9 --- /dev/null +++ b/config/locales/views/learning_goals/en.yml @@ -0,0 +1,20 @@ +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 diff --git a/config/routes.rb b/config/routes.rb index 891be87..b8292cf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/test/controllers/learning_goals_controller_test.rb b/test/controllers/learning_goals_controller_test.rb new file mode 100644 index 0000000..cf1ae1c --- /dev/null +++ b/test/controllers/learning_goals_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class LearningGoalsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end