From 72d4bc65ae9dc10d97aa361e17d99394adeace5a Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 28 Jan 2024 07:30:06 -0800 Subject: [PATCH] API endpoints to load alt styles as JSON, and filter by species Gonna use this in the app for the alt style picker! Still deciding on the UI details. --- app/controllers/alt_styles_controller.rb | 12 +++++++++++- app/models/species.rb | 1 + config/routes.rb | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/alt_styles_controller.rb b/app/controllers/alt_styles_controller.rb index 2f4a6b57..46f9f7fd 100644 --- a/app/controllers/alt_styles_controller.rb +++ b/app/controllers/alt_styles_controller.rb @@ -1,6 +1,16 @@ class AltStylesController < ApplicationController def index @alt_styles = AltStyle.includes(:species, :color, :swf_assets). - order(:species_id, :color_id).all + order(:species_id, :color_id) + + if params[:species_id] + @species = Species.find(params[:species_id]) + @alt_styles = @alt_styles.merge(@species.alt_styles) + end + + respond_to do |format| + format.html { render } + format.json { render json: @alt_styles } + end end end diff --git a/app/models/species.rb b/app/models/species.rb index f6f08e18..f8de694c 100644 --- a/app/models/species.rb +++ b/app/models/species.rb @@ -1,6 +1,7 @@ class Species < ApplicationRecord translates # TODO: Remove once we're all done with translations! has_many :pet_types + has_many :alt_styles scope :alphabetical, -> { order(:name) } diff --git a/config/routes.rb b/config/routes.rb index 99912fb3..d7bf4e29 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,6 +31,7 @@ OpenneoImpressItems::Application.routes.draw do resources :colors, only: [] do get :pet_type, to: 'pet_types#show' end + resources :alt_styles, path: 'alt-styles', only: [:index] end resources :colors, only: [] do resources :pet_types, only: [:index]