Compare commits

...

3 Commits

Author SHA1 Message Date
Trevor Vallender a1f3de2e7a Diary entry basic functionality
Entries can be created, edited and viewed.
2023-12-29 14:27:09 +00:00
Trevor Vallender 4aac6e136c Add diary entry to exercise 2023-12-29 12:18:40 +00:00
Trevor Vallender 20ff7d1764 Add diary entries 2023-12-29 12:16:30 +00:00
21 changed files with 259 additions and 2 deletions

View File

@ -0,0 +1,57 @@
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,
# exercise
# food
)
end
end

13
app/models/diary_entry.rb Normal file
View File

@ -0,0 +1,13 @@
class DiaryEntry < ApplicationRecord
belongs_to :user
has_many :exercises
validates :date,
:pain_level,
:energy_level,
:mental_alertness_level,
:work_stress_level,
:life_stress_level,
:sleep_quality_level,
presence: true
end

View File

@ -1,3 +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

@ -0,0 +1,17 @@
class CreateDiaryEntries < ActiveRecord::Migration[7.1]
def change
create_table :diary_entries do |t|
t.text :notes, null: false, default: ''
t.date :date, null: false
t.references :user, null: false, foreign_key: true
t.integer :pain_level, null: false
t.integer :energy_level, null: false
t.integer :mental_alertness_level, null: false
t.integer :work_stress_level, null: false
t.integer :life_stress_level, null: false
t.integer :sleep_quality_level, null: false
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
class AddDiaryEntryToExercise < ActiveRecord::Migration[7.1]
def change
add_reference :exercises, :diary_entry, null: false, foreign_key: true
end
end

21
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_29_114145) do
ActiveRecord::Schema[7.1].define(version: 2023_12_29_121659) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -63,6 +63,21 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_29_114145) do
t.index ["user_id"], name: "index_blog_posts_on_user_id"
end
create_table "diary_entries", force: :cascade do |t|
t.text "notes", default: "", null: false
t.date "date", null: false
t.bigint "user_id", null: false
t.integer "pain_level", null: false
t.integer "energy_level", null: false
t.integer "mental_alertness_level", null: false
t.integer "work_stress_level", null: false
t.integer "life_stress_level", null: false
t.integer "sleep_quality_level", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_diary_entries_on_user_id"
end
create_table "exercise_types", force: :cascade do |t|
t.text "name", null: false
t.text "unit", null: false
@ -76,6 +91,8 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_29_114145) do
t.float "amount", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "diary_entry_id", null: false
t.index ["diary_entry_id"], name: "index_exercises_on_diary_entry_id"
t.index ["exercise_type_id"], name: "index_exercises_on_exercise_type_id"
end
@ -128,6 +145,8 @@ ActiveRecord::Schema[7.1].define(version: 2023_12_29_114145) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "blog_posts", "users"
add_foreign_key "diary_entries", "users"
add_foreign_key "exercises", "diary_entries"
add_foreign_key "exercises", "exercise_types"
add_foreign_key "microposts", "users"
end

21
test/fixtures/diary_entries.yml vendored Normal file
View File

@ -0,0 +1,21 @@
one:
user: trevor
date: 2023-12-29
notes: Oh, what a day
pain_level: 10
energy_level: 10
mental_alertness_level: 10
work_stress_level: 10
life_stress_level: 10
sleep_quality_level: 10
two:
user: trevor
date: 2023-12-28
notes: Wow
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

View File

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