diff --git a/app/controllers/swf_assets_controller.rb b/app/controllers/swf_assets_controller.rb index fa86ea32..c7b00657 100644 --- a/app/controllers/swf_assets_controller.rb +++ b/app/controllers/swf_assets_controller.rb @@ -13,12 +13,16 @@ class SwfAssetsController < ApplicationController end json = @swf_assets.all.group_by(&:body_id) end - elsif params[:body_id] && params[:item_ids] - swf_assets = SwfAsset.arel_table + elsif params[:pet_type_id] && params[:item_ids] + pet_type = PetType.find(params[:pet_type_id], :select => [:body_id, :species_id]) + items = Item.find(params[:item_ids], :select => [:id, :species_support_ids]) + compatible_items = items.select { |i| i.support_species?(pet_type.species) } + compatible_item_ids = compatible_items.map(&:id) + @swf_assets = SwfAsset.object_assets. - select('swf_assets.*, parents_swf_assets.parent_id'). - fitting_body_id(params[:body_id]). - for_item_ids(params[:item_ids]) + fitting_body_id(pet_type.body_id). + for_item_ids(compatible_item_ids). + with_parent_ids json = @swf_assets.map { |a| a.as_json(:parent_id => a.parent_id.to_i, :for => 'wardrobe') } elsif params[:pet_state_id] @swf_assets = PetState.find(params[:pet_state_id]).swf_assets.all @@ -34,6 +38,14 @@ class SwfAssetsController < ApplicationController if params[:ids][:object] @swf_assets += SwfAsset.object_assets.where(:remote_id => params[:ids][:object]).all end + elsif params[:body_id] && params[:item_ids] + # DEPRECATED in favor of pet_type_id and item_ids + swf_assets = SwfAsset.arel_table + @swf_assets = SwfAsset.object_assets. + select('swf_assets.*, parents_swf_assets.parent_id'). + fitting_body_id(params[:body_id]). + for_item_ids(params[:item_ids]) + json = @swf_assets.map { |a| a.as_json(:parent_id => a.parent_id.to_i, :for => 'wardrobe') } end if @swf_assets @swf_assets = @swf_assets.all unless @swf_assets.is_a? Array diff --git a/app/models/item.rb b/app/models/item.rb index 0cf42c48..11a5c314 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -148,6 +148,10 @@ class Item < ActiveRecord::Base def supported_species @supported_species ||= species_support_ids.blank? ? Species.all : species_support_ids.sort.map { |id| Species.find(id) } end + + def support_species?(species) + species_support_ids.blank? || species_support_ids.include?(species.id) + end def self.search(query, user=nil) raise SearchError, "Please provide a search query" unless query diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 8ee3cf48..fd8f7b00 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -167,6 +167,9 @@ class SwfAsset < ActiveRecord::Base joins(:parent_swf_asset_relationships). where(ParentSwfAssetRelationship.arel_table[:parent_id].in(item_ids)) } + scope :with_parent_ids, lambda { + select('swf_assets.*, parents_swf_assets.parent_id') + } def local_url '/' + File.join(PUBLIC_ASSET_DIR, local_path_within_outfit_swfs) diff --git a/config/routes.rb b/config/routes.rb index 9db81083..0f86e3e5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,10 +11,13 @@ OpenneoImpressItems::Application.routes.draw do |map| match '/item_zone_sets.json' => 'ItemZoneSets#index' + # DEPRECATED match '/bodies/:body_id/swf_assets.json' => 'swf_assets#index', :as => :body_swf_assets + match '/items/:item_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets match '/items/:item_id/bodies/:body_id/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets_for_body_id match '/pet_types/:pet_type_id/swf_assets.json' => 'swf_assets#index', :as => :pet_type_swf_assets + match '/pet_types/:pet_type_id/items/swf_assets.json' => 'swf_assets#index', :as => :item_swf_assets_for_pet_type match '/pet_states/:pet_state_id/swf_assets.json' => 'swf_assets#index', :as => :pet_state_swf_assets match '/species/:species_id/color/:color_id/pet_type.json' => 'pet_types#show' diff --git a/public/javascripts/wardrobe.js b/public/javascripts/wardrobe.js index 74991ae0..cab0d79c 100644 --- a/public/javascripts/wardrobe.js +++ b/public/javascripts/wardrobe.js @@ -742,7 +742,7 @@ function Wardrobe() { if(!item.hasAssetsFitting(pet_type)) item_ids_needed.push(id); } if(item_ids_needed.length) { - $.getJSON('/bodies/' + pet_type.body_id + '/swf_assets.json', { + $.getJSON('/pet_types/' + pet_type.id + '/items/swf_assets.json', { item_ids: item_ids_needed }, function (data) { var item;