From 13df3e5aadae04b322bff4cb0279b299e2c92a41 Mon Sep 17 00:00:00 2001 From: David Gay Date: Mon, 30 Oct 2023 02:00:16 -0400 Subject: Start and view runs --- app/controllers/application_controller.rb | 1 + app/controllers/home_controller.rb | 2 ++ app/controllers/runs_controller.rb | 29 +++++++++++++++++++++++++++++ app/helpers/run_helper.rb | 2 ++ app/models/checkpoint.rb | 2 ++ app/models/game.rb | 2 +- app/models/run.rb | 2 +- app/views/layouts/application.html.erb | 2 +- app/views/runs/new.html.erb | 31 +++++++++++++++++++++++++++++++ app/views/runs/show.html.erb | 4 ++++ config/routes.rb | 2 ++ db/seeds.rb | 14 ++++++++++++++ test/controllers/runs_controller_test.rb | 4 ++++ 13 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 app/controllers/runs_controller.rb create mode 100644 app/helpers/run_helper.rb create mode 100644 app/views/runs/new.html.erb create mode 100644 app/views/runs/show.html.erb create mode 100644 test/controllers/runs_controller_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..6b4dcfa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::Base + before_action :authenticate_user! end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 6d3fe90..44156ce 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,3 +1,5 @@ class HomeController < ApplicationController + skip_before_action :authenticate_user! + def index; end end diff --git a/app/controllers/runs_controller.rb b/app/controllers/runs_controller.rb new file mode 100644 index 0000000..303c0e3 --- /dev/null +++ b/app/controllers/runs_controller.rb @@ -0,0 +1,29 @@ +class RunsController < ApplicationController + def index + @runs = Run.all + end + + def show + @run = Run.find(params[:id]) + end + + def new + @run = Run.new + end + + def create + @run = Run.new(run_params) + @run.user = current_user + if @run.save + redirect_to run_path(@run), notice: "Created runs." + else + render :new, status: :unprocessable_entity + end + end + +protected + + def run_params + params.require(:run).permit(:title, :description, :game_id) + end +end diff --git a/app/helpers/run_helper.rb b/app/helpers/run_helper.rb new file mode 100644 index 0000000..1e2a3a7 --- /dev/null +++ b/app/helpers/run_helper.rb @@ -0,0 +1,2 @@ +module RunHelper +end diff --git a/app/models/checkpoint.rb b/app/models/checkpoint.rb index 2a89128..c014b2e 100644 --- a/app/models/checkpoint.rb +++ b/app/models/checkpoint.rb @@ -1,4 +1,6 @@ class Checkpoint < ApplicationRecord belongs_to :run belongs_to :user + + has_one_attached :save_file end diff --git a/app/models/game.rb b/app/models/game.rb index bdf72ba..9da939f 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,5 +1,5 @@ class Game < ApplicationRecord has_many :runs, dependent: :restrict_with_error - validate :presence, [:title] + validates_presence_of :title end diff --git a/app/models/run.rb b/app/models/run.rb index 91090b4..7c9209e 100644 --- a/app/models/run.rb +++ b/app/models/run.rb @@ -3,5 +3,5 @@ class Run < ApplicationRecord belongs_to :user has_many :checkpoints, dependent: :destroy - validate :presence, [:title] + validates_presence_of :title end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index abe5895..46b1f5b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,7 +12,7 @@ -
+

<%= notice %>

<%= alert %>

diff --git a/app/views/runs/new.html.erb b/app/views/runs/new.html.erb new file mode 100644 index 0000000..e8b7b6e --- /dev/null +++ b/app/views/runs/new.html.erb @@ -0,0 +1,31 @@ +<%= form_with model: @run do |f| %> +
+ <%= f.label :game_id, "Game" %> + <%= f.collection_select :game_id, Game.all, :id, :title, {prompt: "Choose..."} %> +
+ +
+ <%= f.label :title %> + <%= f.text_field :title %> +
+ +
+ <%= f.label :description %> + <%= f.text_area :description %> +
+ +
+ <%= f.submit "Start run" %> +
+ + <% if @run.errors.any? %> +
+

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

+
    + <% @run.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> +<% end %> diff --git a/app/views/runs/show.html.erb b/app/views/runs/show.html.erb new file mode 100644 index 0000000..adb41e4 --- /dev/null +++ b/app/views/runs/show.html.erb @@ -0,0 +1,4 @@ +

<%= @run.title %>

+

<%= @run.game.title %>

+ +

Run started by: <%= @run.user.email %>

diff --git a/config/routes.rb b/config/routes.rb index c6cd35b..a267aa8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,4 +8,6 @@ Rails.application.routes.draw do # Defines the root path route ("/") root "home#index" + + resources :runs end diff --git a/db/seeds.rb b/db/seeds.rb index 4fbd6ed..ce6f747 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,3 +7,17 @@ # ["Action", "Comedy", "Drama", "Horror"].each do |genre_name| # MovieGenre.find_or_create_by!(name: genre_name) # end + +[ + "Pokémon Red", "Pokémon Blue", "Pokémon Yellow", "Pokémon Gold", "Pokémon Silver", + "Pokémon Crystal", "Pokémon Ruby", "Pokémon Sapphire", "Pokémon Emerald", + "Pokémon FireRed", "Pokémon LeafGreen", "Pokémon Diamond", "Pokémon Pearl", + "Pokémon Platinum", "Pokémon HeartGold", "Pokémon SoulSilver", "Pokémon Black", + "Pokémon White", "Pokémon Black 2", "Pokémon White 2", "Pokémon X", "Pokémon Y", + "Pokémon Omega Ruby", "Pokémon Alpha Sapphire", "Pokémon Sun", "Pokémon Moon", + "Pokémon Ultra Sun", "Pokémon Ultra Moon", "Pokémon Let's Go, Pikachu!", + "Pokémon Let's Go, Eevee!", "Pokémon Sword", "Pokémon Shield", + "Pokémon Brilliant Diamond", "Pokémon Shining Pearl", "Pokémon Legends: Arceus" +].each do |title| + Game.create(title:) +end diff --git a/test/controllers/runs_controller_test.rb b/test/controllers/runs_controller_test.rb new file mode 100644 index 0000000..fc14be9 --- /dev/null +++ b/test/controllers/runs_controller_test.rb @@ -0,0 +1,4 @@ +require "test_helper" + +class RunsControllerTest < ActionDispatch::IntegrationTest +end -- cgit v1.2.3