Skip to content

Commit

Permalink
feature: display types slot per pokemon
Browse files Browse the repository at this point in the history
add url to access pokemon details
  • Loading branch information
AdrienGiboire committed Oct 13, 2021
1 parent b3f8527 commit e59ef2b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
15 changes: 14 additions & 1 deletion app/serializers/pokemon_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
class PokemonSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers

attribute :id
attribute :name
attribute :base_experience
attribute :height
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
5 changes: 0 additions & 5 deletions app/serializers/type_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
class TypeSerializer < ActiveModel::Serializer
attribute :name
attribute :slot

def slot
object.pokemon_types.slot
end
end
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

# Initialize the Rails application.
Rails.application.initialize!
Rails.application.default_url_options = Rails.application.config.action_mailer.default_url_options
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e59ef2b

Please sign in to comment.