summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/pokedex_controller.rb8
-rw-r--r--app/models/user.rb4
-rw-r--r--app/views/layouts/application.html.erb2
-rw-r--r--app/views/pokedex/show.html.erb20
-rw-r--r--app/views/users/show.html.erb2
5 files changed, 30 insertions, 6 deletions
diff --git a/app/controllers/pokedex_controller.rb b/app/controllers/pokedex_controller.rb
index 062373e..21ee44a 100644
--- a/app/controllers/pokedex_controller.rb
+++ b/app/controllers/pokedex_controller.rb
@@ -1,3 +1,11 @@
class PokedexController < ApplicationController
+ before_action :set_user
+
def index; end
+
+protected
+
+ def set_user
+ @user = User.find(params[:user_id])
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 4ad0a1e..6b0f462 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -7,7 +7,7 @@ class User < ApplicationRecord
has_many :pokedex_entries, dependent: :destroy
has_many :runs, dependent: :restrict_with_error
- def pokedex_captured_count
- pokedex_entries.joins(:pokemon).select("distinct pokemons.pokedex_num").count
+ def captured_pokemon
+ Pokemon.joins(:pokedex_entries).where(pokedex_entries: {user_id: id}).distinct
end
end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 2332cd5..073a2f7 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -23,7 +23,7 @@
</div>
<div class="space-x-8 nav-links">
<% if user_signed_in? %>
- <%= link_to "Pokédex", pokedex_path %>
+ <%= link_to "Pokédex", pokedex_path(user_id: current_user.id) %>
<%= link_to "Trainer", user_path(current_user) %>
<%= link_to "Sign out", destroy_user_session_path %>
<% else %>
diff --git a/app/views/pokedex/show.html.erb b/app/views/pokedex/show.html.erb
index e463d58..992c7c8 100644
--- a/app/views/pokedex/show.html.erb
+++ b/app/views/pokedex/show.html.erb
@@ -1,11 +1,27 @@
<div class="space-y-4">
<h1>Pokédex</h1>
+ <div class="flex space-x-4 items-center">
+ <%= form_with url: pokedex_path do |f| %>
+ <%= f.collection_select :user_id, User.order(:name), :id,
+ ->(user) { user.name || "No. #{user.id}" }, {selected: params[:user_id], include_blank: "Show full Pokédex"}
+ %>
+ <% end %>
+ <% if @user %>
+ <span>has captured <%= pluralize(@user.captured_pokemon.count, "specie") %> of pokémon.</span>
+ <% end %>
+ </div>
+
<div class="flex flex-wrap gap-4">
<% Pokemon.order(:pokedex_num).each do |pokemon| %>
<% break if pokemon.pokedex_num.to_i > 386 # Up through gen 3. %>
- <div class="flex flex-col flex-auto items-center w-[100px] bg-white
- border border-orange-900 rounded shadow">
+ <div class="flex flex-col flex-auto items-center p-1 w-[100px] bg-white border border-orange-900 rounded shadow
+ <% if @user && !@user.captured_pokemon.include?(pokemon) %>
+ grayscale opacity-50
+ <% else %>
+ ring ring-orange-500 ring-opacity-50
+ <% end %>
+ ">
<div class="flex-1 text-display text-center">No. <%= pokemon.pokedex_num %></div>
<div class="flex-grow flex flex-col justify-around">
<%= image_tag pokemon.sprite_path %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index f11b01f..9d253ec 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -6,7 +6,7 @@
</div>
<div>
<p class="text-lg">NAME: <%= @user.name || "???" %>
- <p>POKéDEX: <%= @user.pokedex_captured_count %></p>
+ <p><%= link_to "POKéDEX: #{@user.captured_pokemon.count}", pokedex_path(user_id: @user.id) %></p>
<p>TIME: <%= distance_of_time_in_words_to_now(@user.created_at) %></p>
</div>
<% if @user == current_user %>