From 0c5cccbecfc8b71fb318c275f142306e19bb7e43 Mon Sep 17 00:00:00 2001 From: David Gay Date: Fri, 3 Nov 2023 01:03:48 -0400 Subject: Pokedex entry recording and viewing on runs#show --- app/controllers/pokedex_entries_controller.rb | 36 ++++++++++ app/helpers/pokedex_entries_helper.rb | 2 + app/models/pokemon.rb | 4 ++ app/views/pokedex/show.html.erb | 2 +- app/views/pokedex_entries/index.html.erb | 20 ++++++ app/views/pokedex_entries/new.html.erb | 25 +++++++ app/views/runs/show.html.erb | 80 ++++++++++++---------- config/routes.rb | 1 + .../controllers/pokedex_entries_controller_test.rb | 7 ++ 9 files changed, 141 insertions(+), 36 deletions(-) create mode 100644 app/controllers/pokedex_entries_controller.rb create mode 100644 app/helpers/pokedex_entries_helper.rb create mode 100644 app/views/pokedex_entries/index.html.erb create mode 100644 app/views/pokedex_entries/new.html.erb create mode 100644 test/controllers/pokedex_entries_controller_test.rb diff --git a/app/controllers/pokedex_entries_controller.rb b/app/controllers/pokedex_entries_controller.rb new file mode 100644 index 0000000..952f0cc --- /dev/null +++ b/app/controllers/pokedex_entries_controller.rb @@ -0,0 +1,36 @@ +class PokedexEntriesController < ApplicationController + before_action :set_run + before_action :only_allow_if_checked_out, only: [:create] + + def index + @pokedex_entries = PokedexEntry.where(run: @run).order(:created_at).reverse + end + + def new + @pokedex_entry = PokedexEntry.new + end + + def create + @pokedex_entry = current_user.pokedex_entries.build(pokedex_entry_params) + @pokedex_entry.run_id = params[:run_id] + if @pokedex_entry.save + redirect_to run_pokedex_entries_path(@run) + else + render :new, status: :unprocessable_entity + end + end + +protected + + def pokedex_entry_params + params.require(:pokedex_entry).permit(:pokemon_id, :recorded_at, :shiny) + end + + def set_run + @run = Run.find(params[:run_id]) + end + + def only_allow_if_checked_out + redirect_to run_path(@run) unless @run.checked_out_user == current_user + end +end diff --git a/app/helpers/pokedex_entries_helper.rb b/app/helpers/pokedex_entries_helper.rb new file mode 100644 index 0000000..87a593f --- /dev/null +++ b/app/helpers/pokedex_entries_helper.rb @@ -0,0 +1,2 @@ +module PokedexEntriesHelper +end diff --git a/app/models/pokemon.rb b/app/models/pokemon.rb index 293281a..ec9ff9f 100644 --- a/app/models/pokemon.rb +++ b/app/models/pokemon.rb @@ -2,4 +2,8 @@ class Pokemon < ApplicationRecord has_many :pokedex_entries, dependent: :restrict_with_error validates :pokedex_num, :name, presence: true + + def sprite_path(shiny: false) + "sprites/pokemon/#{"shiny/" if shiny}#{pokedex_num}.png" + end end diff --git a/app/views/pokedex/show.html.erb b/app/views/pokedex/show.html.erb index f920b19..e463d58 100644 --- a/app/views/pokedex/show.html.erb +++ b/app/views/pokedex/show.html.erb @@ -8,7 +8,7 @@ border border-orange-900 rounded shadow">
No. <%= pokemon.pokedex_num %>
- <%= image_tag "sprites/pokemon/#{pokemon.pokedex_num}.png" %> + <%= image_tag pokemon.sprite_path %>
<%= pokemon.name %>
diff --git a/app/views/pokedex_entries/index.html.erb b/app/views/pokedex_entries/index.html.erb new file mode 100644 index 0000000..e31cf2d --- /dev/null +++ b/app/views/pokedex_entries/index.html.erb @@ -0,0 +1,20 @@ +<%= turbo_frame_tag "pokedex_entries" do %> + <%= link_to "Record an entry", new_run_pokedex_entry_path(@run) %> + +
+ <% @pokedex_entries.each do |entry| %> +
+
+ <%= image_tag entry.pokemon.sprite_path(shiny: entry.shiny?) %> +
+
+
<%= "Shiny " if entry.shiny? %><%= entry.pokemon.name %>
+
Recorded by + <%= link_to entry.user.name || "???", user_path(entry.user) %> @ <%= entry.recorded_at %> +
+
+
+ <% end %> +
+<% end %> diff --git a/app/views/pokedex_entries/new.html.erb b/app/views/pokedex_entries/new.html.erb new file mode 100644 index 0000000..03bcbc8 --- /dev/null +++ b/app/views/pokedex_entries/new.html.erb @@ -0,0 +1,25 @@ +<%= turbo_frame_tag "pokedex_entries" do %> + <%= form_with model: [@run, @pokedex_entry], class: "space-y-4" do |form| %> +
+ <%= form.label :pokemon_id %> + <%= form.collection_select :pokemon_id, Pokemon.order(:pokedex_num), :id, + ->(pokemon) { "#{pokemon.pokedex_num} - #{pokemon.name}" }, prompt: "Choose...", + required: true %> +
+ +
+ <%= form.label :recorded_at %> + <%= form.datetime_local_field :recorded_at, value: Time.zone.now, required: true %> +
+ +
+ <%= form.label :shiny %> + <%= form.check_box :shiny %> +
+ +
+ <%= form.submit "Got 'em!", class: "btn btn-primary" %> + <%= link_to "Cancel", run_pokedex_entries_path %> +
+ <% end %> +<% end %> diff --git a/app/views/runs/show.html.erb b/app/views/runs/show.html.erb index e080783..cca5029 100644 --- a/app/views/runs/show.html.erb +++ b/app/views/runs/show.html.erb @@ -21,44 +21,54 @@ <% end %> -

Checkpoints

-
<%= link_to "New checkpoint", new_run_checkpoint_path(@run) %>
+
+
+

Checkpoints

+
<%= link_to "New checkpoint", new_run_checkpoint_path(@run) %>
- <% @run.checkpoints.order(:created_at).reverse.each do |checkpoint| %> -
-
-
-
- <% case checkpoint.kind.to_sym %> - <% when :comment %> - 💬 - <% when :checkin %> - 📬 - <% when :checkout %> - 📭 - <% else %> - <%# Should never happen. %> - <% end %> -
-
- <%= checkpoint.kind %> - <% if checkpoint.save_file.attached? %> - <%= link_to "Download save file", rails_blob_path(checkpoint.save_file, disposition: "attachment") %> + <% @run.checkpoints.order(:created_at).reverse.each do |checkpoint| %> +
+
+
+
+ <% case checkpoint.kind.to_sym %> + <% when :comment %> + 💬 + <% when :checkin %> + 📬 + <% when :checkout %> + 📭 + <% else %> + <%# Should never happen. %> + <% end %> +
+
+ <%= checkpoint.kind %> + <% if checkpoint.save_file.attached? %> + <%= link_to "Download save file", rails_blob_path(checkpoint.save_file, disposition: "attachment") %> + <% end %> +
+ +
+
+
+
+ <%= time_ago_in_words(checkpoint.created_at) %> ago by + <%= link_to checkpoint.user.name || "???", user_path(checkpoint.user) %> + @ <%= checkpoint.created_at %> +
+ <% if checkpoint.comment %> +
<%= checkpoint.comment %>
<% end %>
- -
-
-
- <%= time_ago_in_words(checkpoint.created_at) %> ago by - <%= link_to checkpoint.user.name || "???", user_path(checkpoint.user) %> - @ <%= checkpoint.created_at %> -
- <% if checkpoint.comment %> -
<%= checkpoint.comment %>
- <% end %> -
+ <% end %>
- <% end %> +
+ <% if @run.checked_out_user == current_user %> +

Pokédex Entries

+ <%= turbo_frame_tag "pokedex_entries", src: run_pokedex_entries_url(@run) %> + <% end %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 207321e..7eb1fb0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ Rails.application.routes.draw do resource :pokedex, controller: :pokedex, only: :show resources :runs do resources :checkpoints + resources :pokedex_entries end resources :users, except: [:destroy] end diff --git a/test/controllers/pokedex_entries_controller_test.rb b/test/controllers/pokedex_entries_controller_test.rb new file mode 100644 index 0000000..3cd6194 --- /dev/null +++ b/test/controllers/pokedex_entries_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PokedexEntriesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end -- cgit v1.2.3