From e59ef2bcb039dbb5b87bc395dac68ab9c7d3ca23 Mon Sep 17 00:00:00 2001 From: Adrien Date: Wed, 13 Oct 2021 18:23:06 +0200 Subject: [PATCH] feature: display types slot per pokemon add url to access pokemon details --- app/serializers/pokemon_serializer.rb | 15 ++++++++++++++- app/serializers/type_serializer.rb | 5 ----- config/environment.rb | 1 + config/environments/development.rb | 2 ++ config/routes.rb | 2 +- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/serializers/pokemon_serializer.rb b/app/serializers/pokemon_serializer.rb index 3d3fc6c..734cfd8 100644 --- a/app/serializers/pokemon_serializer.rb +++ b/app/serializers/pokemon_serializer.rb @@ -1,4 +1,6 @@ class PokemonSerializer < ActiveModel::Serializer + include Rails.application.routes.url_helpers + attribute :id attribute :name attribute :base_experience @@ -6,6 +8,17 @@ class PokemonSerializer < ActiveModel::Serializer attribute :is_default attribute :order attribute :weight + attribute :url + + has_many :types do + object.types.map do |type| + TypeSerializer.new(type).serializable_hash.merge( + slot: PokemonType.where(type_id: type.id, pokemon_id: object.id).first.slot + ) + end + end - has_many :types + def url + api_pokemon_show_url(object.id) + end end diff --git a/app/serializers/type_serializer.rb b/app/serializers/type_serializer.rb index 547686f..a5be266 100644 --- a/app/serializers/type_serializer.rb +++ b/app/serializers/type_serializer.rb @@ -1,8 +1,3 @@ class TypeSerializer < ActiveModel::Serializer attribute :name - attribute :slot - - def slot - object.pokemon_types.slot - end end diff --git a/config/environment.rb b/config/environment.rb index cac5315..a636472 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,3 +3,4 @@ # Initialize the Rails application. Rails.application.initialize! +Rails.application.default_url_options = Rails.application.config.action_mailer.default_url_options diff --git a/config/environments/development.rb b/config/environments/development.rb index 7a9f6c3..5e144a2 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -73,4 +73,6 @@ # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true + + config.action_mailer.default_url_options = { host: "localhost", port: 3000 } end diff --git a/config/routes.rb b/config/routes.rb index 8593a0d..b2646af 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,5 +4,5 @@ mount Sidekiq::Web => "/sidekiq" get '/api/pokemons', to: 'api/pokemons#index' - get '/api/pokemons/:id', to: 'api/pokemons#show' + get '/api/pokemons/:id', to: 'api/pokemons#show', as: :api_pokemon_show end