summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/games/ptu/pokemon_controller.rb22
-rw-r--r--app/javascript/stylesheets/core.css35
-rw-r--r--app/models/ptu_pokemon.rb13
-rw-r--r--app/views/application/_flash_messages.html.erb9
-rw-r--r--app/views/games/ptu/pokemon/new.html.erb30
-rw-r--r--app/views/games/ptu/pokemon/show.html.erb32
-rw-r--r--app/views/layouts/application.html.erb1
-rw-r--r--config/routes.rb6
-rw-r--r--test/models/ptu_pokemon_test.rb4
9 files changed, 148 insertions, 4 deletions
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] %>
+ <div class="block rounded my-2 p-2 bg-red-200 text-red-800">
+ <%= flash[:error] %>
+ </div>
+<% elsif flash[:success] %>
+ <div class="block rounded my-2 p-2 bg-green-200 text-green-800">
+ <%= flash[:success] %>
+ </div>
+<% 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 @@
+<h1>New Pokemon</h1>
+
+<%= 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 @@
+<h1><%= @pokemon.name %></h1>
+
+<table>
+ <tr>
+ <td>Base HP</td>
+ <td><%= @pokemon.base_hp %></td>
+ </tr>
+ <tr>
+ <td>Base ATK</td>
+ <td><%= @pokemon.base_atk %></td>
+ </tr>
+ <tr>
+ <td>Base DEF</td>
+ <td><%= @pokemon.base_def %></td>
+ </tr>
+ <tr>
+ <td>Base SPATK</td>
+ <td><%= @pokemon.base_spatk %></td>
+ </tr>
+ <tr>
+ <td>Base SPDEF</td>
+ <td><%= @pokemon.base_spdef %></td>
+ </tr>
+ <tr>
+ <td>Base SPEED</td>
+ <td><%= @pokemon.base_speed %></td>
+ </tr>
+ <tr>
+ <td>Gender Ratio</td>
+ <td><%= @pokemon.gender_ratio_string %></td>
+ </tr>
+</table>
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 @@
<div class="flex flex-col min-h-screen">
<%= render "header" %>
<div class="container mx-auto flex-grow">
+ <%= render "flash_messages" %>
<%= yield %>
</div>
<%= 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