Diary entry basic functionality

Entries can be created, edited and viewed.
This commit is contained in:
Trevor Vallender 2023-12-29 14:27:09 +00:00
parent 4aac6e136c
commit c4d64e40a5
17 changed files with 181 additions and 5 deletions

View File

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

View File

@ -8,5 +8,6 @@ class DiaryEntry < ApplicationRecord
:mental_alertness_level,
:work_stress_level,
:life_stress_level,
:sleep_quality_level,
presence: true
end

View File

@ -1,4 +1,4 @@
class Exercise < ApplicationRecord
belongs_to :type
belongs_to :exercise_type
belongs_to :diary_entry
end

View File

@ -2,4 +2,6 @@ class ExerciseType < ApplicationRecord
validates :name,
:unit,
presence: true
has_many :exercises
end

View File

@ -0,0 +1,4 @@
<div id="diary_entry_<%= diary_entry.id %>" class="diary_entry">
<h2><%= link_to diary_entry.date, diary_entry %></h2>
<%= render partial: "levels", locals: { diary_entry: diary_entry } %>
</div>

View File

@ -0,0 +1,29 @@
<%= form_with model: @diary_entry, id: "diary_entry_form" do |f| %>
<h2><%= title %></h2>
<%= 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 %>

View File

@ -0,0 +1,22 @@
<table>
<thead>
<tr>
<th><%= t(".pain_level") %></th>
<th><%= t(".energy_level") %></th>
<th><%= t(".mental_alertness_level") %></th>
<th><%= t(".work_stress_level") %></th>
<th><%= t(".life_stress_level") %></th>
<th><%= t(".sleep_quality_level") %></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= diary_entry.pain_level %></td>
<td><%= diary_entry.energy_level %></td>
<td><%= diary_entry.mental_alertness_level %></td>
<td><%= diary_entry.work_stress_level %></td>
<td><%= diary_entry.life_stress_level %></td>
<td><%= diary_entry.sleep_quality_level %></td>
</tr>
</tbody>
</table>

View File

@ -0,0 +1,8 @@
<% @title = t(".edit") %>
<%= render partial: "form",
locals: {
button_text: t(".edit"),
title: t(".edit"),
} %>

View File

@ -0,0 +1,13 @@
<h2><%= t(".diary_entries") %></h2>
<%= link_to t(".new"), new_diary_entry_path %>
<% if @diary_entries.empty? %>
<p><%= t(".empty") %></p>
<% else %>
<ul id="diary_entries">
<% @diary_entries.each do |diary_entry| %>
<li><%= render diary_entry %></li>
<% end %>
</ul>
<%= paginate @diary_entries %>
<% end %>

View File

@ -0,0 +1,8 @@
<% @title = t(".create") %>
<%= render partial: "form",
locals: {
button_text: t(".create"),
title: t(".create"),
} %>

View File

@ -0,0 +1,4 @@
<%= @diary_entry.notes %>
<h2><%= link_to @diary_entry.date, @diary_entry %></h2>
<%= render partial: "levels", locals: { diary_entry: @diary_entry } %>
<%= link_to t(".edit"), edit_diary_entry_path(@diary_entry) %>

View File

@ -21,6 +21,7 @@
<li><%= link_to t(".blog_posts"), blog_posts_path %></li>
<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(".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,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

View File

@ -9,3 +9,4 @@ en:
register: Register
blog_posts: Blog
microposts: μposts
diary_entries: Diary

View File

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

View File

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

View File

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