From 47be31b415f33efbe73055fd0bb297e88f758731 Mon Sep 17 00:00:00 2001 From: David Gay Date: Tue, 31 Oct 2023 02:04:58 -0400 Subject: Checkpoint kinds: comment, checkin, checkout --- app/controllers/checkpoints_controller.rb | 2 +- app/models/checkpoint.rb | 2 + app/models/run.rb | 8 ++- app/views/checkpoints/new.html.erb | 72 ++++++++++++++-------- app/views/home/index.html.erb | 5 +- app/views/runs/show.html.erb | 5 +- .../20231031053134_add_kind_to_checkpoints.rb | 5 ++ db/schema.rb | 3 +- 8 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 db/migrate/20231031053134_add_kind_to_checkpoints.rb diff --git a/app/controllers/checkpoints_controller.rb b/app/controllers/checkpoints_controller.rb index 94df2c3..ef53a6b 100644 --- a/app/controllers/checkpoints_controller.rb +++ b/app/controllers/checkpoints_controller.rb @@ -18,7 +18,7 @@ class CheckpointsController < ApplicationController protected def checkpoint_params - params.require(:checkpoint).permit(:title, :description, :save_file) + params.require(:checkpoint).permit(:title, :description, :save_file, :kind) end def set_run diff --git a/app/models/checkpoint.rb b/app/models/checkpoint.rb index c014b2e..9cdb774 100644 --- a/app/models/checkpoint.rb +++ b/app/models/checkpoint.rb @@ -1,4 +1,6 @@ class Checkpoint < ApplicationRecord + enum kind: {comment: 0, checkin: 1, checkout: 2} + belongs_to :run belongs_to :user diff --git a/app/models/run.rb b/app/models/run.rb index 7c9209e..023202b 100644 --- a/app/models/run.rb +++ b/app/models/run.rb @@ -3,5 +3,11 @@ class Run < ApplicationRecord belongs_to :user has_many :checkpoints, dependent: :destroy - validates_presence_of :title + validates :title, presence: true + + def checked_in? + last_checkpoint = checkpoints.where.not(kind: :comment).last + # If there are no non-comment checkpoints, then the run was never checked in. + last_checkpoint.nil? ? false : last_checkpoint.checkin? + end end diff --git a/app/views/checkpoints/new.html.erb b/app/views/checkpoints/new.html.erb index d122d91..712eae1 100644 --- a/app/views/checkpoints/new.html.erb +++ b/app/views/checkpoints/new.html.erb @@ -1,31 +1,55 @@ -<%= form_with model: [@run, @checkpoint] do |f| %> -
- <%= f.label :title %> - <%= f.text_field :title %> -
+
+

<%= @run.title %>

-
- <%= f.label :description %> - <%= f.text_area :description %> -
+

New checkpoint

-
- <%= f.label :save_file %> - <%= f.file_field :save_file %> -
+ <%= form_with model: [@run, @checkpoint], class: "space-y-4" do |f| %> +
+ <%= f.label :title %> + <%= f.text_field :title %> +
-
- <%= f.submit "Submit checkpoint", class: "btn-primary" %> -
+
+ <%= f.label :description %> + <%= f.text_area :description %> +
- <% if @checkpoint.errors.any? %>
-

<%= pluralize(@checkpoint.errors.count, "error") %> prohibited this checkpoint from being saved:

-
    - <% @checkpoint.errors.full_messages.each do |message| %> -
  • <%= message %>
  • - <% end %> -
+ <%= f.label :save_file %> + <%= f.file_field :save_file %>
+ +
+
+ <%= f.radio_button :kind, "comment" %> + <%= f.label :kind_comment, "I'm just leaving a comment" %> +
+ <% if @run.checked_in? %> +
+ <%= f.radio_button :kind, "checkout" %> + <%= f.label :kind_checkout, "I'm checking out this save file, lock the run until I check it back in" %> +
+ <% else %> +
+ <%= f.radio_button :kind, "checkin" %> + <%= f.label :kind_checkin, "I'm checking in this save file so another trainer can play it" %> +
+ <% end %> +
+ +
+ <%= f.submit "Save checkpoint", class: "btn-primary" %> +
+ + <% if @checkpoint.errors.any? %> +
+

<%= pluralize(@checkpoint.errors.count, "error") %> prohibited this checkpoint from being saved:

+
    + <% @checkpoint.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> <% end %> -<% end %> +
diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 457200c..a961565 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -9,7 +9,10 @@
  • At <%= checkpoint.created_at %> <%= checkpoint.user.name %> checked in a save for <%= link_to checkpoint.run.title, run_path(checkpoint.run) %> (<%= checkpoint.run.game.title %>) - <%= link_to "[save file]", rails_blob_path(checkpoint.save_file, disposition: "attachment") %>
  • + <% if checkpoint.save_file.attached? %> + <%= link_to "[save file]", rails_blob_path(checkpoint.save_file, disposition: "attachment") %> + <% end %> + <% end %> diff --git a/app/views/runs/show.html.erb b/app/views/runs/show.html.erb index eb580ad..832889a 100644 --- a/app/views/runs/show.html.erb +++ b/app/views/runs/show.html.erb @@ -10,6 +10,9 @@ diff --git a/db/migrate/20231031053134_add_kind_to_checkpoints.rb b/db/migrate/20231031053134_add_kind_to_checkpoints.rb new file mode 100644 index 0000000..2b71498 --- /dev/null +++ b/db/migrate/20231031053134_add_kind_to_checkpoints.rb @@ -0,0 +1,5 @@ +class AddKindToCheckpoints < ActiveRecord::Migration[7.1] + def change + add_column :checkpoints, :kind, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index bed04ff..b48737d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_10_31_050050) do +ActiveRecord::Schema[7.1].define(version: 2023_10_31_053134) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -49,6 +49,7 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_31_050050) do t.bigint "user_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "kind", default: 0 t.index ["run_id"], name: "index_checkpoints_on_run_id" t.index ["user_id"], name: "index_checkpoints_on_user_id" end -- cgit v1.2.3