From 4ed2a36d48b6677d7ddb137e073a985d6391b8b4 Mon Sep 17 00:00:00 2001 From: David Gay Date: Tue, 6 Apr 2021 21:56:44 -0400 Subject: Super-bare-bones pokemon adding and viewing --- app/controllers/games/ptu/pokemon_controller.rb | 22 ++++++++++++++++ app/javascript/stylesheets/core.css | 35 +++++++++++++++++++++++++ app/models/ptu_pokemon.rb | 13 +++++++++ app/views/application/_flash_messages.html.erb | 9 +++++++ app/views/games/ptu/pokemon/new.html.erb | 30 +++++++++++++++++++++ app/views/games/ptu/pokemon/show.html.erb | 32 ++++++++++++++++++++++ app/views/layouts/application.html.erb | 1 + config/routes.rb | 6 +++++ test/models/ptu_pokemon_test.rb | 4 --- 9 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 app/views/application/_flash_messages.html.erb create mode 100644 app/views/games/ptu/pokemon/new.html.erb create mode 100644 app/views/games/ptu/pokemon/show.html.erb diff --git a/app/controllers/games/ptu/pokemon_controller.rb b/app/controllers/games/ptu/pokemon_controller.rb index 8290a73..485f03d 100644 --- a/app/controllers/games/ptu/pokemon_controller.rb +++ b/app/controllers/games/ptu/pokemon_controller.rb @@ -1,4 +1,26 @@ class Games::Ptu::PokemonController < ApplicationController def show + @pokemon = PtuPokemon.find(params[:id]) end + + def new + @pokemon = PtuPokemon.new + end + + def create + @pokemon = PtuPokemon.new(pokemon_params) + if @pokemon.save + flash[:success] = "Created Pokémon #{@pokemon.name}." + redirect_to games_ptu_pokemon_path(@pokemon) + else + flash[:error] = "Failed to create Pokémon." + redirect_to new_games_ptu_pokemon_path + end + end + + private + def pokemon_params + params.require(:ptu_pokemon).permit(:name, :base_hp, :base_atk, :base_def, :base_spatk, :base_spdef, + :base_speed, :male_chance, :notes) + end end diff --git a/app/javascript/stylesheets/core.css b/app/javascript/stylesheets/core.css index a174cfb..15b234b 100644 --- a/app/javascript/stylesheets/core.css +++ b/app/javascript/stylesheets/core.css @@ -8,3 +8,38 @@ a:hover { p { margin-top: 1rem; } + +label { + @apply mb-2; +} + +input { + @apply border border-black p-1; +} +input:focus { + @apply outline-none border; +} + +button, [type="button"], [type="reset"], [type="submit"] { + @apply py-1 px-2 cursor-pointer; +} +button:hover, [type="button"]:hover, [type="reset"]:hover, [type="submit"]:hover { + @apply bg-gray-300; +} + +button:focus, [type="button"]:focus, [type="reset"]:focus, [type="submit"]:focus { +} + +select { + @apply border border-black p-1; +} +select:focus { + @apply border-gray-700; +} + +textarea { + @apply border border-black p-1; +} +textarea:focus { + @apply border-gray-700; +} diff --git a/app/models/ptu_pokemon.rb b/app/models/ptu_pokemon.rb index 1c57500..c34d32a 100644 --- a/app/models/ptu_pokemon.rb +++ b/app/models/ptu_pokemon.rb @@ -1,4 +1,17 @@ class PtuPokemon < ApplicationRecord + validates :name, uniqueness: true validates :name, :base_hp, :base_atk, :base_def, :base_spatk, :base_spdef, :base_speed, presence: true validates :male_chance, inclusion: 0..100, allow_nil: true + + def female_chance + (100.0 - self.male_chance) if self.male_chance + end + + def gender_ratio_string + if self.male_chance + "Male #{self.male_chance}% / Female #{self.female_chance}%" + else + "No gender" + end + end end diff --git a/app/views/application/_flash_messages.html.erb b/app/views/application/_flash_messages.html.erb new file mode 100644 index 0000000..7db1762 --- /dev/null +++ b/app/views/application/_flash_messages.html.erb @@ -0,0 +1,9 @@ +<% if flash[:error] %> +
+ <%= flash[:error] %> +
+<% elsif flash[:success] %> +
+ <%= flash[:success] %> +
+<% end %> diff --git a/app/views/games/ptu/pokemon/new.html.erb b/app/views/games/ptu/pokemon/new.html.erb new file mode 100644 index 0000000..7ba0426 --- /dev/null +++ b/app/views/games/ptu/pokemon/new.html.erb @@ -0,0 +1,30 @@ +

New Pokemon

+ +<%= form_with model: @pokemon, url: games_ptu_pokemon_index_path do |f| %> + <%= f.label :name %> + <%= f.text_field :name, required: true %> + + <%= f.label :base_hp, "Base HP" %> + <%= f.number_field :base_hp, min: 1, required: true %> + + <%= f.label :base_atk, "Base ATK" %> + <%= f.number_field :base_atk, min: 1, required: true %> + + <%= f.label :base_def, "Base DEF" %> + <%= f.number_field :base_def, min: 1, required: true %> + + <%= f.label :base_spatk, "Base SPATK" %> + <%= f.number_field :base_spatk, min: 1, required: true %> + + <%= f.label :base_spdef, "Base SPDEF" %> + <%= f.number_field :base_spdef, min: 1, required: true %> + + <%= f.label :base_speed, "Base SPEED" %> + <%= f.number_field :base_speed, min: 1, required: true %> + + <%= f.label :male_chance, "Male Chance" %> + <%= f.number_field :male_chance, min: 0, step: 0.01 %> % + + <%= f.submit "Save" %> + +<% end %> diff --git a/app/views/games/ptu/pokemon/show.html.erb b/app/views/games/ptu/pokemon/show.html.erb new file mode 100644 index 0000000..e7b80ce --- /dev/null +++ b/app/views/games/ptu/pokemon/show.html.erb @@ -0,0 +1,32 @@ +

<%= @pokemon.name %>

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Base HP<%= @pokemon.base_hp %>
Base ATK<%= @pokemon.base_atk %>
Base DEF<%= @pokemon.base_def %>
Base SPATK<%= @pokemon.base_spatk %>
Base SPDEF<%= @pokemon.base_spdef %>
Base SPEED<%= @pokemon.base_speed %>
Gender Ratio<%= @pokemon.gender_ratio_string %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ac331aa..2be4dc3 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,6 +14,7 @@
<%= render "header" %>
+ <%= render "flash_messages" %> <%= yield %>
<%= render "footer" %> diff --git a/config/routes.rb b/config/routes.rb index 0cdd7e0..45d70ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,10 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "home#index", as: :home + + namespace :games do + namespace :ptu do + resources :pokemon, only: [:show, :new, :create] + end + end end diff --git a/test/models/ptu_pokemon_test.rb b/test/models/ptu_pokemon_test.rb index 0b4aa2f..1fcdfa2 100644 --- a/test/models/ptu_pokemon_test.rb +++ b/test/models/ptu_pokemon_test.rb @@ -7,8 +7,4 @@ class PtuPokemonTest < ActiveSupport::TestCase base_speed: 6) end end - - test "can't set a male chance greater than 100" do - assert_not ptu_pokemons(:one).update(male_chance: 150.20) - end end -- cgit v1.2.3