From c4d64e40a5e6f9fe6ca099b8af8c8c128ca64c9d Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Fri, 29 Dec 2023 14:27:09 +0000 Subject: [PATCH] Diary entry basic functionality Entries can be created, edited and viewed. --- app/controllers/diary_entries_controller.rb | 55 +++++++++++++++++++ app/models/diary_entry.rb | 1 + app/models/exercise.rb | 2 +- app/models/exercise_type.rb | 2 + app/views/diary_entries/_diary_entry.html.erb | 4 ++ app/views/diary_entries/_form.html.erb | 29 ++++++++++ app/views/diary_entries/_levels.html.erb | 22 ++++++++ app/views/diary_entries/edit.html.erb | 8 +++ app/views/diary_entries/index.html.erb | 13 +++++ app/views/diary_entries/new.html.erb | 8 +++ app/views/diary_entries/show.html.erb | 4 ++ app/views/layouts/application.html.erb | 1 + config/locales/views/diary_entries/en.yml | 25 +++++++++ config/locales/views/layouts/application.yml | 1 + config/routes.rb | 1 + test/fixtures/diary_entries.yml | 8 +-- test/fixtures/exercises.yml | 2 + 17 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 app/controllers/diary_entries_controller.rb create mode 100644 app/views/diary_entries/_diary_entry.html.erb create mode 100644 app/views/diary_entries/_form.html.erb create mode 100644 app/views/diary_entries/_levels.html.erb create mode 100644 app/views/diary_entries/edit.html.erb create mode 100644 app/views/diary_entries/index.html.erb create mode 100644 app/views/diary_entries/new.html.erb create mode 100644 app/views/diary_entries/show.html.erb create mode 100644 config/locales/views/diary_entries/en.yml diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb new file mode 100644 index 0000000..a251642 --- /dev/null +++ b/app/controllers/diary_entries_controller.rb @@ -0,0 +1,55 @@ +class DiaryEntriesController < ApplicationController + before_action :set_diary_entry, only: [:show, :edit, :update, :destroy] + + def index + @diary_entries = DiaryEntry.all + .order(date: :desc) + .page(params[:page]) + end + + def show; end + + def new + @diary_entry = DiaryEntry.new(date: Date.today) + end + + def create + @diary_entry = DiaryEntry.new(diary_entry_params) + @diary_entry.user = helpers.current_user + + if @diary_entry.save! + redirect_to @diary_entry, notice: t(".created") + else + render :new, status: :unprocessable_entity + end + end + + def edit; end + + def update + if @diary_entry.update(diary_entry_params) + redirect_to @diary_entry, notice: t(".updated") + else + render :edit, status: :unprocessable_entity + end + end + + private + + def set_diary_entry + @diary_entry = DiaryEntry.find(params[:id]) + end + + def diary_entry_params + params.require(:diary_entry).permit( + :notes, + :date, + :pain_level, + :energy_level, + :mental_alertness_level, + :work_stress_level, + :life_stress_level, + :sleep_quality_level, + ) + end +end diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index 532de4e..f0f7ae0 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -8,5 +8,6 @@ class DiaryEntry < ApplicationRecord :mental_alertness_level, :work_stress_level, :life_stress_level, + :sleep_quality_level, presence: true end diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 95182b4..5d66622 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -1,4 +1,4 @@ class Exercise < ApplicationRecord - belongs_to :type + belongs_to :exercise_type belongs_to :diary_entry end diff --git a/app/models/exercise_type.rb b/app/models/exercise_type.rb index 26441fa..8172bf8 100644 --- a/app/models/exercise_type.rb +++ b/app/models/exercise_type.rb @@ -2,4 +2,6 @@ class ExerciseType < ApplicationRecord validates :name, :unit, presence: true + + has_many :exercises end diff --git a/app/views/diary_entries/_diary_entry.html.erb b/app/views/diary_entries/_diary_entry.html.erb new file mode 100644 index 0000000..e7319fc --- /dev/null +++ b/app/views/diary_entries/_diary_entry.html.erb @@ -0,0 +1,4 @@ +
+

<%= link_to diary_entry.date, diary_entry %>

+ <%= render partial: "levels", locals: { diary_entry: diary_entry } %> +
diff --git a/app/views/diary_entries/_form.html.erb b/app/views/diary_entries/_form.html.erb new file mode 100644 index 0000000..44d6136 --- /dev/null +++ b/app/views/diary_entries/_form.html.erb @@ -0,0 +1,29 @@ +<%= form_with model: @diary_entry, id: "diary_entry_form" do |f| %> +

<%= title %>

+ + <%= f.label :date %> + <%= f.date_field :date %> + + <%= f.label :pain_level %> + <%= f.number_field :pain_level %> + + <%= f.label :energy_level %> + <%= f.number_field :energy_level %> + + <%= f.label :mental_alertness_level %> + <%= f.number_field :mental_alertness_level %> + + <%= f.label :work_stress_level %> + <%= f.number_field :work_stress_level %> + + <%= f.label :life_stress_level %> + <%= f.number_field :life_stress_level %> + + <%= f.label :sleep_quality_level %> + <%= f.number_field :sleep_quality_level %> + + <%= f.label :notes %> + <%= f.text_area :notes %> + + <%= f.submit button_text %> +<% end %> diff --git a/app/views/diary_entries/_levels.html.erb b/app/views/diary_entries/_levels.html.erb new file mode 100644 index 0000000..82d0640 --- /dev/null +++ b/app/views/diary_entries/_levels.html.erb @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + +
<%= t(".pain_level") %><%= t(".energy_level") %><%= t(".mental_alertness_level") %><%= t(".work_stress_level") %><%= t(".life_stress_level") %><%= t(".sleep_quality_level") %>
<%= diary_entry.pain_level %><%= diary_entry.energy_level %><%= diary_entry.mental_alertness_level %><%= diary_entry.work_stress_level %><%= diary_entry.life_stress_level %><%= diary_entry.sleep_quality_level %>
diff --git a/app/views/diary_entries/edit.html.erb b/app/views/diary_entries/edit.html.erb new file mode 100644 index 0000000..7f629ad --- /dev/null +++ b/app/views/diary_entries/edit.html.erb @@ -0,0 +1,8 @@ +<% @title = t(".edit") %> + +<%= render partial: "form", + locals: { + button_text: t(".edit"), + title: t(".edit"), + } %> + diff --git a/app/views/diary_entries/index.html.erb b/app/views/diary_entries/index.html.erb new file mode 100644 index 0000000..801013d --- /dev/null +++ b/app/views/diary_entries/index.html.erb @@ -0,0 +1,13 @@ +

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

+<%= link_to t(".new"), new_diary_entry_path %> + +<% if @diary_entries.empty? %> +

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

+<% else %> + + <%= paginate @diary_entries %> +<% end %> diff --git a/app/views/diary_entries/new.html.erb b/app/views/diary_entries/new.html.erb new file mode 100644 index 0000000..4e655a7 --- /dev/null +++ b/app/views/diary_entries/new.html.erb @@ -0,0 +1,8 @@ +<% @title = t(".create") %> + +<%= render partial: "form", + locals: { + button_text: t(".create"), + title: t(".create"), + } %> + diff --git a/app/views/diary_entries/show.html.erb b/app/views/diary_entries/show.html.erb new file mode 100644 index 0000000..afc7d99 --- /dev/null +++ b/app/views/diary_entries/show.html.erb @@ -0,0 +1,4 @@ +<%= @diary_entry.notes %> +

<%= link_to @diary_entry.date, @diary_entry %>

+<%= render partial: "levels", locals: { diary_entry: @diary_entry } %> +<%= link_to t(".edit"), edit_diary_entry_path(@diary_entry) %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3a956cf..8d4e11f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -21,6 +21,7 @@
  • <%= link_to t(".blog_posts"), blog_posts_path %>
  • <%= link_to t(".microposts"), microposts_path %>
  • <% if logged_in? %> +
  • <%= link_to t(".diary_entries"), diary_entries_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/config/locales/views/diary_entries/en.yml b/config/locales/views/diary_entries/en.yml new file mode 100644 index 0000000..2644b7a --- /dev/null +++ b/config/locales/views/diary_entries/en.yml @@ -0,0 +1,25 @@ +en: + diary_entries: + index: + diary_entries: Diary Entries + empty: You have no diary entries yet. + new: New diary entry + new: + create: Create diary entry + edit: + edit: Edit diary entry + delete: Delete diary entry + confirm: Are you sure you want to delete this diary entry? + create: + created: Diary entry created + show: + edit: Edit + update: + updated: Diary entry updated + levels: + pain_level: Pain + energy_level: Energy + mental_alertness_level: Mental Alertness + work_stress_level: Work Stress + life_stress_level: Life Stress + sleep_quality_level: Sleep Quality diff --git a/config/locales/views/layouts/application.yml b/config/locales/views/layouts/application.yml index fbafb7f..4bbd3c9 100644 --- a/config/locales/views/layouts/application.yml +++ b/config/locales/views/layouts/application.yml @@ -9,3 +9,4 @@ en: register: Register blog_posts: Blog microposts: μposts + diary_entries: Diary diff --git a/config/routes.rb b/config/routes.rb index 88e2e0c..891be87 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,7 @@ Rails.application.routes.draw do resources :blog_posts resources :microposts resources :tags, only: [:index, :show] + resources :diary_entries, except: [:destroy] get "up" => "rails/health#show", as: :rails_health_check end diff --git a/test/fixtures/diary_entries.yml b/test/fixtures/diary_entries.yml index 9dd43f5..59e368d 100644 --- a/test/fixtures/diary_entries.yml +++ b/test/fixtures/diary_entries.yml @@ -1,21 +1,21 @@ one: - user: tsvallender + user: trevor date: 2023-12-29 notes: Oh, what a day - exercise: one pain_level: 10 energy_level: 10 mental_alertness_level: 10 work_stress_level: 10 life_stress_level: 10 + sleep_quality_level: 10 two: - user: tsvallender + user: trevor date: 2023-12-28 notes: Wow - exercise: two pain_level: 1 energy_level: 1 mental_alertness_level: 1 work_stress_level: 1 life_stress_level: 1 + sleep_quality_level: 10 diff --git a/test/fixtures/exercises.yml b/test/fixtures/exercises.yml index 2adc35f..185768a 100644 --- a/test/fixtures/exercises.yml +++ b/test/fixtures/exercises.yml @@ -2,8 +2,10 @@ one: exercise_type: running at: 2023-12-29 11:41:45 amount: 5 + diary_entry: one two: exercise_type: cycling at: 2023-12-29 11:41:45 amount: 10.5 + diary_entry: two